-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMen010AvoidMagicNumbers.cs
286 lines (236 loc) · 9.58 KB
/
Men010AvoidMagicNumbers.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
namespace Menees.Analyzers;
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class Men010AvoidMagicNumbers : Analyzer
{
#region Public Constants
public const string DiagnosticId = "MEN010";
#endregion
#region Private Data Members
private static readonly LocalizableString Title =
new LocalizableResourceString(nameof(Resources.Men010Title), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString MessageFormat =
new LocalizableResourceString(nameof(Resources.Men010MessageFormat), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString Description =
new LocalizableResourceString(nameof(Resources.Men010Description), Resources.ResourceManager, typeof(Resources));
private static readonly DiagnosticDescriptor Rule =
new(DiagnosticId, Title, MessageFormat, Rules.Design, Rules.InfoSeverity, Rules.EnabledByDefault, Description);
#endregion
#region Public Properties
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
#endregion
#region Public Methods
public override void Initialize(AnalysisContext context)
{
base.Initialize(context);
context.RegisterSyntaxTreeActionHonorExclusions(this, this.HandleSyntaxTree);
}
#endregion
#region Private Methods
private static bool InAllowedDeclarationContext(LiteralExpressionSyntax literalExpression, Settings settings)
{
bool result = literalExpression.Ancestors()
.Any(ancestor =>
{
bool allowed = false;
switch (ancestor.Kind())
{
case SyntaxKind.LocalDeclarationStatement:
allowed = ((LocalDeclarationStatementSyntax)ancestor).Modifiers.Any(SyntaxKind.ConstKeyword);
break;
case SyntaxKind.FieldDeclaration:
// Allow any static readonly field to use unnamed numeric literals in expressions because sometimes
// arrays of numeric literals are needed (e.g., Crc32Code) or special instances are created (e.g.,
// Color.FromArgb(225, 255, 255), new DateTime(1970, 1, 1)). Also, allow fields that are set to
// single, non-calculated constants. In these cases the field name is good enough documentation.
FieldDeclarationSyntax fieldDeclaration = (FieldDeclarationSyntax)ancestor;
SyntaxTokenList fieldModifiers = fieldDeclaration.Modifiers;
allowed = fieldModifiers.Any(SyntaxKind.ConstKeyword)
|| (fieldModifiers.Any(SyntaxKind.StaticKeyword) && fieldModifiers.Any(SyntaxKind.ReadOnlyKeyword))
|| HasParentChain(
literalExpression,
SyntaxKind.EqualsValueClause,
SyntaxKind.VariableDeclarator,
SyntaxKind.VariableDeclaration,
SyntaxKind.FieldDeclaration);
break;
case SyntaxKind.PropertyDeclaration:
// Allow property initializers that are set to single, non-calculated constants. The property name is good enough.
allowed = HasParentChain(literalExpression, SyntaxKind.EqualsValueClause, SyntaxKind.PropertyDeclaration);
break;
case SyntaxKind.BracketedArgumentList:
// Allow small numeric literals as indexer arguments (e.g., for sequential item access). Since 0, 1,
// and 2 are allowed by default, it's common to see other small sequential indexes used too.
// Note: ArrayRankSpecifier (e.g., in new string[7]) is a different syntax kind, so this won't allow
// magic numbers for array ranks (just indexes).
byte indexValue;
allowed = HasParentChain(literalExpression, SyntaxKind.Argument, SyntaxKind.BracketedArgumentList)
&& byte.TryParse(literalExpression.Token.Text, out indexValue);
break;
case SyntaxKind.EnumMemberDeclaration:
case SyntaxKind.AttributeArgument:
allowed = true;
break;
case SyntaxKind.MethodDeclaration:
// Unit test methods typically use lots of numeric literals for test cases, and they don't need to be named constants.
allowed = IsUnitTestMethod((BaseMethodDeclarationSyntax)ancestor, settings);
break;
case SyntaxKind.ClassDeclaration:
// Unit test classes typically use lots of numeric literals for test cases, and they don't need to be named constants.
allowed = IsUnitTestClass((ClassDeclarationSyntax)ancestor, settings);
break;
case SyntaxKind.InvocationExpression:
allowed = IsAllowedInvocation(literalExpression, settings);
break;
case SyntaxKind.IndexExpression:
case SyntaxKind.RangeExpression:
allowed = literalExpression.Parent == ancestor;
break;
case SyntaxKind.SimpleAssignmentExpression:
allowed = ancestor is AssignmentExpressionSyntax assignment
&& assignment.Left is MemberAccessExpressionSyntax member
&& settings.IsAllowedNumericLiteralCaller(member.Name.Identifier.Text);
break;
}
return allowed;
});
return result;
}
private static bool IsAllowedInvocation(LiteralExpressionSyntax literalExpression, Settings settings)
{
bool result = false;
if (HasParentChain(literalExpression, SyntaxKind.Argument, SyntaxKind.ArgumentList, SyntaxKind.InvocationExpression))
{
ArgumentListSyntax? argList = literalExpression?.Parent?.Parent as ArgumentListSyntax;
if (argList?.Arguments.Count == 1)
{
if (argList.Parent is InvocationExpressionSyntax invocation && invocation.Expression != null)
{
string? invokedMemberName = null;
switch (invocation.Expression.Kind())
{
case SyntaxKind.IdentifierName:
// A direct reference to an inherited or declared member with no this or base qualifier.
IdentifierNameSyntax? identifier = invocation.Expression as IdentifierNameSyntax;
invokedMemberName = identifier?.Identifier.ValueText;
break;
case SyntaxKind.SimpleMemberAccessExpression:
case SyntaxKind.PointerMemberAccessExpression:
// A reference like item.Method or item->Method.
MemberAccessExpressionSyntax? access = invocation.Expression as MemberAccessExpressionSyntax;
invokedMemberName = access?.Name?.Identifier.ValueText;
break;
}
if (!string.IsNullOrEmpty(invokedMemberName) && invokedMemberName != null && literalExpression != null)
{
// Allow cases like item.GetXxx(n) for n in [0,255] to handle cases like IDataRecord.Get and Array.Get accessors.
if (invokedMemberName.StartsWith("Get"))
{
result = Settings.TryParseIntegerLiteral(literalExpression.Token.ValueText, out byte _);
}
else
{
result = settings.IsAllowedNumericLiteralCaller(invokedMemberName);
}
}
}
}
}
return result;
}
private static bool IsUnitTestClass(ClassDeclarationSyntax classSyntax, Settings settings)
=> HasUnitTestAttribute(classSyntax.AttributeLists, settings.TestClassAttributeNames);
private static bool IsUnitTestMethod(BaseMethodDeclarationSyntax method, Settings settings)
=> HasUnitTestAttribute(method.AttributeLists, settings.TestMethodAttributeNames);
private static bool HasUnitTestAttribute(SyntaxList<AttributeListSyntax> attributeLists, ISet<string> unitTestAttributeNames)
{
// If settings is configured with no unit test attributes, then we can skip this test.
bool result = unitTestAttributeNames.Count > 0
&& attributeLists.HasIndicatorAttribute(unitTestAttributeNames);
return result;
}
private static bool HasParentChain(
LiteralExpressionSyntax literalExpression,
SyntaxKind level1,
SyntaxKind level2)
{
bool result = false;
SyntaxNode? parent = literalExpression.Parent;
if (parent?.Kind() == level1)
{
parent = parent.Parent;
if (parent?.Kind() == level2)
{
result = true;
}
}
return result;
}
private static bool HasParentChain(
LiteralExpressionSyntax literalExpression,
SyntaxKind level1,
SyntaxKind level2,
SyntaxKind level3)
{
bool result = false;
SyntaxNode? parent = literalExpression.Parent;
if (parent?.Kind() == level1)
{
parent = parent.Parent;
if (parent?.Kind() == level2)
{
parent = parent.Parent;
if (parent?.Kind() == level3)
{
result = true;
}
}
}
return result;
}
private static bool HasParentChain(
LiteralExpressionSyntax literalExpression,
SyntaxKind level1,
SyntaxKind level2,
SyntaxKind level3,
SyntaxKind level4)
{
bool result = false;
SyntaxNode? parent = literalExpression.Parent;
if (parent?.Kind() == level1)
{
parent = parent.Parent;
if (parent?.Kind() == level2)
{
parent = parent.Parent;
if (parent?.Kind() == level3)
{
parent = parent.Parent;
if (parent?.Kind() == level4)
{
result = true;
}
}
}
}
return result;
}
private void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
{
SyntaxNode root = context.Tree.GetRoot(context.CancellationToken);
// Note: This makes no attempt to push a unary minus into the numeric literal.
// That's not how C# syntax works, and it would be inconsistent from other unary
// operators (e.g., + and ~). Also, if we tried to allow N but not -N, then a user
// could always get around it by writing an expression like -(N).
IEnumerable<LiteralExpressionSyntax> magicNumberExpressions = root.DescendantNodesAndSelf()
.Where(node => node.IsKind(SyntaxKind.NumericLiteralExpression))
.Cast<LiteralExpressionSyntax>()
.Where(literal => !this.Settings.IsAllowedNumericLiteral(literal.Token.Text)
&& !InAllowedDeclarationContext(literal, this.Settings));
foreach (LiteralExpressionSyntax expression in magicNumberExpressions)
{
SyntaxToken literal = expression.Token;
context.ReportDiagnostic(Diagnostic.Create(Rule, literal.GetLocation(), literal.Text));
}
}
#endregion
}