-
Notifications
You must be signed in to change notification settings - Fork 9
/
SimpleAssignmentAnalyzer.cs
154 lines (136 loc) · 6.42 KB
/
SimpleAssignmentAnalyzer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
namespace Gu.Analyzers;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using Gu.Roslyn.AnalyzerExtensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal class SimpleAssignmentAnalyzer : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(
Descriptors.GU0010DoNotAssignSameValue,
Descriptors.GU0012NullCheckParameter,
Descriptors.GU0015DoNotAssignMoreThanOnce);
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterSyntaxNodeAction(c => Handle(c), SyntaxKind.SimpleAssignmentExpression);
}
private static void Handle(SyntaxNodeAnalysisContext context)
{
if (!context.IsExcludedFromAnalysis() &&
context.Node is AssignmentExpressionSyntax { Left: { } left, Right: { } right } assignment &&
context.SemanticModel.TryGetSymbol(left, context.CancellationToken, out var leftSymbol))
{
if (context.SemanticModel.TryGetSymbol(right, context.CancellationToken, out var rightSymbol) &&
AreSame(left, right) &&
assignment.FirstAncestorOrSelf<InitializerExpressionSyntax>() is null &&
SymbolComparer.Equal(leftSymbol, rightSymbol))
{
context.ReportDiagnostic(Diagnostic.Create(Descriptors.GU0010DoNotAssignSameValue, assignment.GetLocation()));
}
if (right is IdentifierNameSyntax identifier &&
context.ContainingSymbol is IMethodSymbol method &&
method.DeclaredAccessibility.IsEither(Accessibility.Internal, Accessibility.Protected, Accessibility.Public) &&
method.Parameters.TryFirst(x => x.Name == identifier.Identifier.ValueText, out var parameter) &&
parameter is { Type.IsReferenceType: true, HasExplicitDefaultValue: false } &&
assignment.TryFirstAncestor(out BaseMethodDeclarationSyntax? containingMethod) &&
!NullCheck.IsChecked(parameter, containingMethod, context.SemanticModel, context.CancellationToken))
{
context.ReportDiagnostic(Diagnostic.Create(Descriptors.GU0012NullCheckParameter, assignment.Right.GetLocation()));
}
if (IsRedundantAssignment(leftSymbol, assignment, context))
{
context.ReportDiagnostic(Diagnostic.Create(Descriptors.GU0015DoNotAssignMoreThanOnce, assignment.GetLocation()));
}
}
}
private static bool AreSame(ExpressionSyntax left, ExpressionSyntax right)
{
if (TryGetIdentifierName(left, out var leftName) ^ TryGetIdentifierName(right, out var rightName))
{
return false;
}
if (leftName != null)
{
return leftName.Identifier.ValueText == rightName!.Identifier.ValueText;
}
return left is MemberAccessExpressionSyntax { Expression: { } le, Name: { } ln } &&
right is MemberAccessExpressionSyntax { Expression: { } re, Name: { } rn } &&
AreSame(ln, rn) &&
AreSame(le, re);
}
private static bool TryGetIdentifierName(ExpressionSyntax expression, [NotNullWhen(true)] out IdentifierNameSyntax? result)
{
switch (expression)
{
case IdentifierNameSyntax identifierName:
result = identifierName;
return true;
case MemberAccessExpressionSyntax { Expression: ThisExpressionSyntax _, Name: { } name }:
return TryGetIdentifierName(name, out result);
default:
result = null;
return false;
}
}
private static bool IsRedundantAssignment(ISymbol left, AssignmentExpressionSyntax assignment, SyntaxNodeAnalysisContext context)
{
if (left is IDiscardSymbol ||
assignment.TryFirstAncestor<ObjectCreationExpressionSyntax>(out _))
{
return false;
}
if (assignment.TryFirstAncestor<MemberDeclarationSyntax>(out var member))
{
if (member is not ConstructorDeclarationSyntax &&
context.SemanticModel.TryGetType(assignment.Left, context.CancellationToken, out var type) &&
FieldOrProperty.TryCreate(left, out _))
{
if (type == KnownSymbols.Boolean ||
type.TypeKind == TypeKind.Enum)
{
return false;
}
}
using var walker = AssignmentExecutionWalker.For(left, member, SearchScope.Member, context.SemanticModel, context.CancellationToken);
foreach (var candidate in walker.Assignments)
{
if (candidate == assignment)
{
continue;
}
if (!MemberPath.Equals(candidate.Left, assignment.Left))
{
continue;
}
if (candidate.IsExecutedBefore(assignment) == ExecutedBefore.Yes)
{
if (left is IParameterSymbol { RefKind: RefKind.Out } &&
assignment.TryFirstAncestor<BlockSyntax>(out var assignmentBlock) &&
candidate.TryFirstAncestor<BlockSyntax>(out var candidateBlock) &&
(candidateBlock.Contains(assignmentBlock) ||
candidateBlock.Statements.Last() is ReturnStatementSyntax))
{
return false;
}
using var nameWalker = IdentifierNameWalker.Borrow(assignment.Right);
foreach (var name in nameWalker.IdentifierNames)
{
if (left.Name == name.Identifier.ValueText &&
context.SemanticModel.TryGetSymbol(name, context.CancellationToken, out var symbol) &&
SymbolComparer.Equal(symbol, left))
{
return false;
}
}
return true;
}
}
}
return false;
}
}