-
Notifications
You must be signed in to change notification settings - Fork 222
/
MemberInitializedToDefault.cs
164 lines (144 loc) · 7.43 KB
/
MemberInitializedToDefault.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
/*
* SonarAnalyzer for .NET
* Copyright (C) 2015-2023 SonarSource SA
* mailto: contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
namespace SonarAnalyzer.Rules.CSharp
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class MemberInitializedToDefault : SonarDiagnosticAnalyzer
{
internal const string DiagnosticId = "S3052";
private const string MessageFormat = "Remove this initialization to '{0}', the compiler will do that for you.";
private const string Zero = "Zero";
private static readonly CSharpExpressionNumericConverter ExpressionNumericConverter = new CSharpExpressionNumericConverter();
private static readonly DiagnosticDescriptor Rule = DescriptorFactory.Create(DiagnosticId, MessageFormat);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);
protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterNodeAction(CheckField, SyntaxKind.FieldDeclaration);
context.RegisterNodeAction(CheckEvent, SyntaxKind.EventFieldDeclaration);
context.RegisterNodeAction(CheckAutoProperty, SyntaxKind.PropertyDeclaration);
}
private static void CheckAutoProperty(SonarSyntaxNodeReportingContext context)
{
var propertyDeclaration = (PropertyDeclarationSyntax)context.Node;
if (propertyDeclaration.Initializer == null
|| !propertyDeclaration.IsAutoProperty())
{
return;
}
var propertySymbol = context.SemanticModel.GetDeclaredSymbol(propertyDeclaration);
if (propertySymbol != null
&& IsDefaultValueInitializer(propertyDeclaration.Initializer, propertySymbol.Type))
{
context.ReportIssue(Diagnostic.Create(Rule, propertyDeclaration.Initializer.GetLocation(), propertySymbol.Name));
}
}
private static void CheckEvent(SonarSyntaxNodeReportingContext context)
{
var field = (EventFieldDeclarationSyntax)context.Node;
foreach (var eventDeclaration in field.Declaration.Variables.Where(v => v.Initializer != null))
{
if (!(context.SemanticModel.GetDeclaredSymbol(eventDeclaration) is IEventSymbol eventSymbol))
{
continue;
}
if (IsDefaultValueInitializer(eventDeclaration.Initializer, eventSymbol.Type))
{
context.ReportIssue(Diagnostic.Create(Rule, eventDeclaration.Initializer.GetLocation(), eventSymbol.Name));
return;
}
}
}
private static void CheckField(SonarSyntaxNodeReportingContext context)
{
var field = (FieldDeclarationSyntax)context.Node;
foreach (var variableDeclarator in field.Declaration.Variables.Where(v => v.Initializer != null))
{
if (context.SemanticModel.GetDeclaredSymbol(variableDeclarator) is IFieldSymbol {IsConst: false} fieldSymbol
&& IsDefaultValueInitializer(variableDeclarator.Initializer, fieldSymbol.Type))
{
context.ReportIssue(Diagnostic.Create(Rule, variableDeclarator.Initializer.GetLocation(), fieldSymbol.Name));
}
}
}
internal static bool IsDefaultValueInitializer(EqualsValueClauseSyntax initializer, ITypeSymbol type) =>
IsDefaultExpressionInitializer(initializer)
|| IsReferenceTypeNullInitializer(initializer, type)
|| IsValueTypeDefaultValueInitializer(initializer, type);
private static bool IsDefaultExpressionInitializer(EqualsValueClauseSyntax initializer) =>
initializer.Value is DefaultExpressionSyntax;
private static bool IsReferenceTypeNullInitializer(EqualsValueClauseSyntax initializer, ITypeSymbol type) =>
type.IsReferenceType
&& CSharpEquivalenceChecker.AreEquivalent(CSharpSyntaxHelper.NullLiteralExpression, initializer.Value);
private static bool IsValueTypeDefaultValueInitializer(EqualsValueClauseSyntax initializer, ITypeSymbol type)
{
if (!type.IsValueType)
{
return false;
}
switch (type.SpecialType)
{
case SpecialType.System_Boolean:
return CSharpEquivalenceChecker.AreEquivalent(initializer.Value, CSharpSyntaxHelper.FalseLiteralExpression);
case SpecialType.System_Decimal:
case SpecialType.System_Double:
case SpecialType.System_Single:
{
return ExpressionNumericConverter.TryGetConstantDoubleValue(initializer.Value, out var constantValue) &&
Math.Abs(constantValue - default(double)) < double.Epsilon;
}
case SpecialType.System_Char:
case SpecialType.System_Byte:
case SpecialType.System_Int16:
case SpecialType.System_Int32:
case SpecialType.System_Int64:
case SpecialType.System_SByte:
case SpecialType.System_UInt16:
case SpecialType.System_UInt32:
case SpecialType.System_UInt64:
case SpecialType.System_IntPtr:
case SpecialType.System_UIntPtr:
{
if (initializer.Value is MemberAccessExpressionSyntax memberAccess
&& memberAccess.Name.Identifier.Text == Zero)
{
return true;
}
else if (ObjectCreationFactory.TryCreate(initializer.Value) is { } objectCreation)
{
var argCount = objectCreation.ArgumentList?.Arguments.Count;
if (argCount == null || argCount == 0)
{
return true;
}
return ExpressionNumericConverter.TryGetConstantIntValue(objectCreation.ArgumentList.Arguments.First().Expression, out var ctorParameter)
&& ctorParameter == default;
}
else
{
return ExpressionNumericConverter.TryGetConstantIntValue(initializer.Value, out var constantValue)
&& constantValue == default;
}
}
default:
return false;
}
}
}
}