Skip to content

Commit 284b7ac

Browse files
sbomervitek-karas
andauthored
Fix some analyzer warnings (#2386)
These were causing dotnet format to produce warnings because they didn't have code fixes available. There are still some IDE0060 rule violations left over that I am not fixing here because they occur in test projects (and I didn't want to silence all such warnings in the test projects). Co-authored-by: vitek-karas <[email protected]>
1 parent 5eee321 commit 284b7ac

File tree

15 files changed

+42
-39
lines changed

15 files changed

+42
-39
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ dotnet_diagnostic.CA2252.severity = none
140140
# CA1822: Mark members as static
141141
dotnet_diagnostic.CA1822.severity = none
142142

143+
# IDE0060: Remove unused parameter
144+
dotnet_diagnostic.IDE0060.severity = none
145+
143146
[external**]
144147
dotnet_analyzer_diagnostic.severity = none
145148
generated_code = true

lint.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
@echo off
2-
powershell -ExecutionPolicy ByPass -NoProfile -command "Set-Location %~dp0; & """%~dp0eng\dotnet.ps1""" ""format illink.sln --exclude src/analyzer src/tuner external %*"""
2+
powershell -ExecutionPolicy ByPass -NoProfile -command "Set-Location %~dp0; & """%~dp0eng\dotnet.ps1""" ""format illink.sln --exclude external %*"""

lint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ while [[ -h $source ]]; do
1313
done
1414

1515
scriptroot="$( cd -P "$( dirname "$source" )" && pwd )"
16-
"$scriptroot/eng/dotnet.sh" format illink.sln --exclude src/analyzer src/tuner external $@
16+
"$scriptroot/eng/dotnet.sh" format illink.sln --exclude external $@

src/analyzer/ConsoleDependencyGraph.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void ShowDependencies (VertexData vertex)
9696
int pi = 0, childIdx;
9797

9898
do {
99-
childIdx = childVertex.parentIndexes [pi];
99+
childIdx = childVertex.parentIndexes[pi];
100100
pi++;
101101
} while (visited.Contains (childIdx) && pi < childVertex.parentIndexes.Count);
102102

@@ -148,13 +148,13 @@ public void ShowStat (bool verbose = false)
148148
Header ("Statistics");
149149
if (verbose) {
150150
foreach (var key in counts.Keys)
151-
Console.WriteLine ("Vertex type:\t{0}{1}count:{2}", key, Tabs (key), counts [key]);
151+
Console.WriteLine ("Vertex type:\t{0}{1}count:{2}", key, Tabs (key), counts[key]);
152152
} else {
153-
Console.WriteLine ("Assemblies:\t{0}", counts ["Assembly"]);
154-
Console.WriteLine ("Modules:\t{0}", counts ["Module"]);
155-
Console.WriteLine ("Types:\t\t{0}", counts ["TypeDef"]);
156-
Console.WriteLine ("Fields:\t\t{0}", counts ["Field"]);
157-
Console.WriteLine ("Methods:\t{0}", counts ["Method"]);
153+
Console.WriteLine ("Assemblies:\t{0}", counts["Assembly"]);
154+
Console.WriteLine ("Modules:\t{0}", counts["Module"]);
155+
Console.WriteLine ("Types:\t\t{0}", counts["TypeDef"]);
156+
Console.WriteLine ("Fields:\t\t{0}", counts["Field"]);
157+
Console.WriteLine ("Methods:\t{0}", counts["Method"]);
158158
}
159159

160160
Console.WriteLine ();

src/analyzer/LinkerAnalyzerCore/DependencyGraph.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
namespace LinkerAnalyzer.Core
1616
{
17-
public class VertexData {
17+
public class VertexData
18+
{
1819
public string value;
1920
public List<int> parentIndexes;
2021
public int index;
@@ -51,7 +52,8 @@ public void Load (string filename)
5152
}
5253
}
5354

54-
void Load (GZipStream zipStream) {
55+
void Load (GZipStream zipStream)
56+
{
5557
using (XmlReader reader = XmlReader.Create (zipStream)) {
5658
while (reader.Read ()) {
5759
switch (reader.NodeType) {
@@ -88,7 +90,7 @@ public VertexData Vertex (string vertexName, bool create = false)
8890
VertexData vertex;
8991

9092
try {
91-
vertex = vertices [indexes [vertexName]];
93+
vertex = vertices[indexes[vertexName]];
9294
} catch (KeyNotFoundException) {
9395
if (create) {
9496
int index = vertices.Count;
@@ -97,9 +99,9 @@ public VertexData Vertex (string vertexName, bool create = false)
9799
indexes.Add (vertexName, index);
98100
string prefix = vertexName.Substring (0, vertexName.IndexOf (':'));
99101
if (counts.ContainsKey (prefix))
100-
counts [prefix]++;
102+
counts[prefix]++;
101103
else
102-
counts [prefix] = 1;
104+
counts[prefix] = 1;
103105
//Console.WriteLine ("prefix " + prefix + " count " + counts[prefix]);
104106
if (prefix == "TypeDef") {
105107
Types.Add (vertex);
@@ -113,7 +115,7 @@ public VertexData Vertex (string vertexName, bool create = false)
113115

114116
public VertexData Vertex (int index)
115117
{
116-
return vertices [index];
118+
return vertices[index];
117119
}
118120

119121
IEnumerable<Tuple<VertexData, int>> AddDependencies (VertexData vertex, HashSet<int> reachedVertices, int depth)

src/analyzer/LinkerAnalyzerCore/SpaceAnalyzer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ int GetMethodSize (MethodDefinition method)
7575
var key = GetKey (method);
7676

7777
if (sizes.ContainsKey (key))
78-
return sizes [key];
78+
return sizes[key];
7979

8080
var msize = method.Body.CodeSize;
8181
msize += method.Name.Length;
@@ -125,7 +125,7 @@ public void LoadAssemblies (bool verbose = true)
125125
else
126126
Console.Write (".");
127127

128-
ReaderParameters parameters = new ReaderParameters () { ReadingMode = ReadingMode.Immediate, AssemblyResolver = resolver};
128+
ReaderParameters parameters = new ReaderParameters () { ReadingMode = ReadingMode.Immediate, AssemblyResolver = resolver };
129129
var assembly = AssemblyDefinition.ReadAssembly (file, parameters);
130130
assemblies.Add (assembly);
131131
foreach (var module in assembly.Modules) {
@@ -146,7 +146,7 @@ public void LoadAssemblies (bool verbose = true)
146146
public int GetSize (VertexData vertex)
147147
{
148148
if (sizes.ContainsKey (vertex.value))
149-
return sizes [vertex.value];
149+
return sizes[vertex.value];
150150
return 0;
151151
}
152152
}

src/analyzer/Main.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
// Copyright 2015 Xamarin Inc (http://www.xamarin.com).
88
//
99
using System;
10-
using Mono.Options;
1110
using LinkerAnalyzer.Core;
11+
using Mono.Options;
1212

1313
namespace LinkerAnalyzer
1414
{
@@ -56,7 +56,7 @@ static void Main (string[] args)
5656
return;
5757
}
5858

59-
string dependencyFile = args [args.Length - 1];
59+
string dependencyFile = args[args.Length - 1];
6060

6161
ConsoleDependencyGraph deps = new ConsoleDependencyGraph () { Tree = reduceToTree, FlatDeps = flatDeps };
6262
deps.Load (dependencyFile);

test/ILLink.RoslynAnalyzer.Tests/CompilationExtensions.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@ public static MetadataReference EmitToImageReference (
1818
this Compilation comp,
1919
EmitOptions? options = null,
2020
bool embedInteropTypes = false,
21-
ImmutableArray<string> aliases = default,
22-
DiagnosticDescriptor[]? expectedWarnings = null) => EmitToPortableExecutableReference (comp, options, embedInteropTypes, aliases, expectedWarnings);
21+
ImmutableArray<string> aliases = default) => EmitToPortableExecutableReference (comp, options, embedInteropTypes, aliases);
2322

2423
public static PortableExecutableReference EmitToPortableExecutableReference (
2524
this Compilation comp,
2625
EmitOptions? options = null,
2726
bool embedInteropTypes = false,
28-
ImmutableArray<string> aliases = default,
29-
DiagnosticDescriptor[]? expectedWarnings = null)
27+
ImmutableArray<string> aliases = default)
3028
{
31-
var image = comp.EmitToArray (options, expectedWarnings: expectedWarnings);
29+
var image = comp.EmitToArray (options);
3230
if (comp.Options.OutputKind == OutputKind.NetModule) {
3331
return ModuleMetadata.CreateFromImage (image).GetReference (display: comp.MakeSourceModuleName ());
3432
} else {
@@ -39,7 +37,6 @@ public static PortableExecutableReference EmitToPortableExecutableReference (
3937
internal static ImmutableArray<byte> EmitToArray (
4038
this Compilation compilation,
4139
EmitOptions? options = null,
42-
DiagnosticDescriptor[]? expectedWarnings = null,
4340
Stream? pdbStream = null,
4441
IMethodSymbol? debugEntryPoint = null,
4542
Stream? sourceLinkStream = null,

test/ILLink.RoslynAnalyzer.Tests/TestCaseCompilation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ internal static class TestCaseCompilation
5656
return (new CompilationWithAnalyzers (comp, SupportedDiagnosticAnalyzers, compWithAnalyzerOptions), comp.GetSemanticModel (src));
5757
}
5858

59-
public static async Task<Compilation> GetCompilation (string source, IEnumerable<MetadataReference>? additionalReferences = null, IEnumerable<SyntaxTree>? additionalSources = null)
59+
public static async Task<Compilation> GetCompilation (string source, IEnumerable<MetadataReference>? additionalReferences = null)
6060
=> (await CreateCompilation (source, additionalReferences: additionalReferences ?? Array.Empty<MetadataReference> ())).Compilation.Compilation;
6161

6262
class SimpleAnalyzerOptions : AnalyzerConfigOptionsProvider

test/ILLink.RoslynAnalyzer.Tests/TestChecker.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Microsoft.CodeAnalysis;
1111
using Microsoft.CodeAnalysis.CSharp;
1212
using Microsoft.CodeAnalysis.CSharp.Syntax;
13-
using Microsoft.CodeAnalysis.Diagnostics;
1413
using Microsoft.CodeAnalysis.Text;
1514
using Mono.Linker.Tests.Cases.Expectations.Assertions;
1615
using Xunit;
@@ -182,13 +181,15 @@ static ProducedBy GetProducedBy (ExpressionSyntax expression)
182181
var producedBy = (ProducedBy) 0x0;
183182
switch (expression) {
184183
case BinaryExpressionSyntax binaryExpressionSyntax:
185-
Enum.TryParse<ProducedBy> ((binaryExpressionSyntax.Left as MemberAccessExpressionSyntax)!.Name.Identifier.ValueText, out var besProducedBy);
184+
if (!Enum.TryParse<ProducedBy> ((binaryExpressionSyntax.Left as MemberAccessExpressionSyntax)!.Name.Identifier.ValueText, out var besProducedBy))
185+
throw new ArgumentException ("Expression must be a ProducedBy value", nameof (expression));
186186
producedBy |= besProducedBy;
187187
producedBy |= GetProducedBy (binaryExpressionSyntax.Right);
188188
break;
189189

190190
case MemberAccessExpressionSyntax memberAccessExpressionSyntax:
191-
Enum.TryParse<ProducedBy> (memberAccessExpressionSyntax.Name.Identifier.ValueText, out var maeProducedBy);
191+
if (!Enum.TryParse<ProducedBy> (memberAccessExpressionSyntax.Name.Identifier.ValueText, out var maeProducedBy))
192+
throw new ArgumentException ("Expression must be a ProducedBy value", nameof (expression));
192193
producedBy |= maeProducedBy;
193194
break;
194195

@@ -282,15 +283,15 @@ private bool TryValidateLogContainsAttribute (AttributeSyntax attribute, List<Di
282283
}
283284
}
284285

285-
missingDiagnosticMessage = $"Could not find text:\n{text}\nIn diagnostics:\n{(string.Join (Environment.NewLine, _diagnostics))}";
286+
missingDiagnosticMessage = $"Could not find text:\n{text}\nIn diagnostics:\n{string.Join (Environment.NewLine, _diagnostics)}";
286287
return false;
287288
}
288289

289-
private void ValidateLogDoesNotContainAttribute (AttributeSyntax attribute, IReadOnlyList<Diagnostic> diagnosticMessages)
290+
private static void ValidateLogDoesNotContainAttribute (AttributeSyntax attribute, IReadOnlyList<Diagnostic> diagnosticMessages)
290291
{
291292
var arg = Assert.Single (LinkerTestBase.GetAttributeArguments (attribute));
292293
var text = LinkerTestBase.GetStringFromExpression (arg.Value);
293-
foreach (var diagnostic in _diagnostics)
294+
foreach (var diagnostic in diagnosticMessages)
294295
Assert.DoesNotContain (text, diagnostic.GetMessage ());
295296
}
296297

0 commit comments

Comments
 (0)