Skip to content

Commit 3fcaed1

Browse files
committed
Fix warnings.
1 parent 5532430 commit 3fcaed1

6 files changed

Lines changed: 113 additions & 23 deletions

File tree

AspNetCoreAnalyzers/Analyzers/AttributeAnalyzer.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ context.ContainingSymbol is IMethodSymbol method &&
3636
{
3737
using (var pairs = GetPairs(template, method))
3838
{
39-
if (pairs.TrySingle(x => x.FromTemplate == null, out var withMethodParameter) &&
40-
methodDeclaration.TryFindParameter(withMethodParameter.FromMethodSymbol.Name, out var parameterSyntax) &&
41-
pairs.TrySingle(x => x.FromMethodSymbol == null, out var withTemplateParameter) &&
42-
withTemplateParameter.FromTemplate is TemplateParameter templateParameter)
39+
if (pairs.TrySingle(x => x.Route == null, out var withMethodParameter) &&
40+
methodDeclaration.TryFindParameter(withMethodParameter.Symbol.Name, out var parameterSyntax) &&
41+
pairs.TrySingle(x => x.Symbol == null, out var withTemplateParameter) &&
42+
withTemplateParameter.Route is TemplateParameter templateParameter)
4343
{
4444
context.ReportDiagnostic(
4545
Diagnostic.Create(
@@ -55,19 +55,19 @@ context.ContainingSymbol is IMethodSymbol method &&
5555
templateParameter.Name.GetLocation(),
5656
ImmutableDictionary<string, string>.Empty.Add(
5757
nameof(Text),
58-
withMethodParameter.FromMethodSymbol.Name)));
58+
withMethodParameter.Symbol.Name)));
5959
}
60-
else if (pairs.Count(x => x.FromTemplate == null) > 1 &&
61-
pairs.Count(x => x.FromMethodSymbol == null) > 1)
60+
else if (pairs.Count(x => x.Route == null) > 1 &&
61+
pairs.Count(x => x.Symbol == null) > 1)
6262
{
6363
context.ReportDiagnostic(
6464
Diagnostic.Create(
6565
ASP001ParameterName.Descriptor,
6666
methodDeclaration.ParameterList.GetLocation()));
6767
}
6868

69-
if (pairs.TryFirst(x => x.FromMethodSymbol == null, out _) &&
70-
!pairs.TryFirst(x => x.FromTemplate == null, out _))
69+
if (pairs.TryFirst(x => x.Symbol == null, out _) &&
70+
!pairs.TryFirst(x => x.Route == null, out _))
7171
{
7272
context.ReportDiagnostic(
7373
Diagnostic.Create(
@@ -78,7 +78,7 @@ context.ContainingSymbol is IMethodSymbol method &&
7878
foreach (var pair in pairs)
7979
{
8080
if (HasWrongType(pair, out var typeName) &&
81-
methodDeclaration.TryFindParameter(pair.FromMethodSymbol?.Name, out parameterSyntax))
81+
methodDeclaration.TryFindParameter(pair.Symbol?.Name, out parameterSyntax))
8282
{
8383
context.ReportDiagnostic(
8484
Diagnostic.Create(
@@ -173,7 +173,7 @@ private static PooledList<ParameterPair> GetPairs(UrlTemplate template, IMethodS
173173
foreach (var component in template.Path)
174174
{
175175
if (component.Parameter is TemplateParameter templateParameter &&
176-
list.All(x => x.FromTemplate != templateParameter))
176+
list.All(x => x.Route != templateParameter))
177177
{
178178
list.Add(new ParameterPair(templateParameter, null));
179179
}
@@ -184,8 +184,8 @@ private static PooledList<ParameterPair> GetPairs(UrlTemplate template, IMethodS
184184

185185
private static bool HasWrongType(ParameterPair pair, out string correctType)
186186
{
187-
if (pair.FromTemplate?.Constraints is ImmutableArray<RouteConstraint> constraints &&
188-
pair.FromMethodSymbol is IParameterSymbol parameter)
187+
if (pair.Route?.Constraints is ImmutableArray<RouteConstraint> constraints &&
188+
pair.Symbol is IParameterSymbol parameter)
189189
{
190190
foreach (var constraint in constraints)
191191
{

AspNetCoreAnalyzers/CodeFixes/ParameterNameFix.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class ParameterNameFix : CodeFixProvider
1818
public override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(
1919
ASP001ParameterName.DiagnosticId);
2020

21+
public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
22+
2123
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
2224
{
2325
var syntaxRoot = await context.Document.GetSyntaxRootAsync(context.CancellationToken)
Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,50 @@
11
namespace AspNetCoreAnalyzers
22
{
3+
using System;
34
using System.Diagnostics;
45
using Microsoft.CodeAnalysis;
56

6-
[DebuggerDisplay("{this.FromTemplate?.Name ?? this.FromMethodSymbol.Name}")]
7-
public struct ParameterPair
7+
[DebuggerDisplay("{this.Route?.Name ?? this.Symbol.Name}")]
8+
public struct ParameterPair : IEquatable<ParameterPair>
89
{
9-
public ParameterPair(TemplateParameter? fromTemplate, IParameterSymbol fromMethodSymbol)
10+
public ParameterPair(TemplateParameter? route, IParameterSymbol symbol)
1011
{
11-
this.FromTemplate = fromTemplate;
12-
this.FromMethodSymbol = fromMethodSymbol;
12+
this.Route = route;
13+
this.Symbol = symbol;
1314
}
1415

15-
public TemplateParameter? FromTemplate { get; }
16+
public TemplateParameter? Route { get; }
1617

17-
public IParameterSymbol FromMethodSymbol { get; }
18+
public IParameterSymbol Symbol { get; }
19+
20+
public static bool operator ==(ParameterPair left, ParameterPair right)
21+
{
22+
return left.Equals(right);
23+
}
24+
25+
public static bool operator !=(ParameterPair left, ParameterPair right)
26+
{
27+
return !left.Equals(right);
28+
}
29+
30+
public bool Equals(ParameterPair other)
31+
{
32+
return this.Route.Equals(other.Route) &&
33+
Equals(this.Symbol, other.Symbol);
34+
}
35+
36+
public override bool Equals(object obj)
37+
{
38+
return obj is ParameterPair other &&
39+
this.Equals(other);
40+
}
41+
42+
public override int GetHashCode()
43+
{
44+
unchecked
45+
{
46+
return (this.Route.GetHashCode() * 397) ^ (this.Symbol != null ? this.Symbol.GetHashCode() : 0);
47+
}
48+
}
1849
}
1950
}

AspNetCoreAnalyzers/Helpers/PathSegment.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
namespace AspNetCoreAnalyzers
22
{
3+
using System;
34
using System.Diagnostics;
45

56
[DebuggerDisplay("{this.Span.ToString()}")]
6-
public struct PathSegment
7+
public struct PathSegment : IEquatable<PathSegment>
78
{
89
public PathSegment(StringLiteral literal, int start, int end)
910
{
@@ -17,6 +18,16 @@ public PathSegment(StringLiteral literal, int start, int end)
1718

1819
public TemplateParameter? Parameter { get; }
1920

21+
public static bool operator ==(PathSegment left, PathSegment right)
22+
{
23+
return left.Equals(right);
24+
}
25+
26+
public static bool operator !=(PathSegment left, PathSegment right)
27+
{
28+
return !left.Equals(right);
29+
}
30+
2031
public static bool TryRead(StringLiteral literal, int start, out PathSegment segment)
2132
{
2233
// https://tools.ietf.org/html/rfc3986
@@ -78,5 +89,21 @@ public static bool TryRead(StringLiteral literal, int start, out PathSegment seg
7889
segment = default(PathSegment);
7990
return false;
8091
}
92+
93+
public bool Equals(PathSegment other)
94+
{
95+
return this.Span.Equals(other.Span);
96+
}
97+
98+
public override bool Equals(object obj)
99+
{
100+
return obj is PathSegment other &&
101+
this.Equals(other);
102+
}
103+
104+
public override int GetHashCode()
105+
{
106+
return this.Span.GetHashCode();
107+
}
81108
}
82109
}

AspNetCoreAnalyzers/Helpers/StringLiteral.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
namespace AspNetCoreAnalyzers
22
{
3+
using System;
34
using System.Diagnostics;
45
using Microsoft.CodeAnalysis;
56
using Microsoft.CodeAnalysis.CSharp.Syntax;
67
using Microsoft.CodeAnalysis.Text;
78

89
[DebuggerDisplay("{this.Text}")]
9-
public struct StringLiteral
10+
public struct StringLiteral : IEquatable<StringLiteral>
1011
{
1112
private readonly LiteralExpressionSyntax literalExpression;
1213

@@ -38,6 +39,16 @@ public bool IsVerbatim
3839

3940
public string ValueText => this.literalExpression.Token.ValueText;
4041

42+
public static bool operator ==(StringLiteral left, StringLiteral right)
43+
{
44+
return left.Equals(right);
45+
}
46+
47+
public static bool operator !=(StringLiteral left, StringLiteral right)
48+
{
49+
return !left.Equals(right);
50+
}
51+
4152
public Location GetLocation(TextSpan textSpan)
4253
{
4354
var text = this.literalExpression.Token.Text;
@@ -86,5 +97,25 @@ int GetIndex(int pos)
8697
return index;
8798
}
8899
}
100+
101+
public bool Equals(StringLiteral other)
102+
{
103+
return this.literalExpression.Equals(other.literalExpression);
104+
}
105+
106+
public override bool Equals(object obj)
107+
{
108+
if (ReferenceEquals(null, obj))
109+
{
110+
return false;
111+
}
112+
113+
return obj is StringLiteral other && this.Equals(other);
114+
}
115+
116+
public override int GetHashCode()
117+
{
118+
return this.literalExpression.GetHashCode();
119+
}
89120
}
90121
}

appveyor.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ before_build:
66
configuration: Release
77
build:
88
verbosity: minimal
9-

0 commit comments

Comments
 (0)