-
Notifications
You must be signed in to change notification settings - Fork 222
/
PrivateStaticMethodUsedOnlyByNestedClass.cs
206 lines (171 loc) · 9.19 KB
/
PrivateStaticMethodUsedOnlyByNestedClass.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
/*
* 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.
*/
using SonarAnalyzer.CFG.Helpers;
namespace SonarAnalyzer.Rules.CSharp;
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class PrivateStaticMethodUsedOnlyByNestedClass : SonarDiagnosticAnalyzer
{
private const string DiagnosticId = "S3398";
private const string MessageFormat = "Move this method inside '{0}'.";
private static readonly SyntaxKind[] AnalyzedSyntaxKinds = new[]
{
SyntaxKind.ClassDeclaration,
SyntaxKind.StructDeclaration,
SyntaxKind.InterfaceDeclaration,
SyntaxKindEx.RecordClassDeclaration,
SyntaxKindEx.RecordStructDeclaration
};
private static readonly DiagnosticDescriptor Rule = DescriptorFactory.Create(DiagnosticId, MessageFormat);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
protected override void Initialize(SonarAnalysisContext context) =>
context.RegisterNodeAction(c =>
{
var outerType = (TypeDeclarationSyntax)c.Node;
if (!IsPartial(outerType)
&& HasNestedTypeDeclarations(outerType)
&& PrivateStaticMethodsOf(outerType) is { Length: > 0 } candidates)
{
var methodReferences = TypesWhichUseTheMethods(candidates, outerType, c.SemanticModel);
foreach (var reference in methodReferences)
{
var typeToMoveInto = LowestCommonAncestorOrSelf(reference.Types);
if (typeToMoveInto != outerType)
{
var nestedTypeName = typeToMoveInto.Identifier.ValueText;
c.ReportIssue(Diagnostic.Create(Rule, reference.Method.Identifier.GetLocation(), nestedTypeName));
}
}
}
},
AnalyzedSyntaxKinds);
private static bool IsPartial(TypeDeclarationSyntax type) =>
type.Modifiers.Any(x => x.IsKind(SyntaxKind.PartialKeyword));
private static bool HasNestedTypeDeclarations(TypeDeclarationSyntax type) =>
type.Members
.OfType<TypeDeclarationSyntax>()
.Any();
private static MethodDeclarationSyntax[] PrivateStaticMethodsOf(TypeDeclarationSyntax type) =>
type.Members
.OfType<MethodDeclarationSyntax>()
.Where(x => IsPrivateAndStatic(x, type))
.ToArray();
private static bool IsPrivateAndStatic(MethodDeclarationSyntax method, TypeDeclarationSyntax containingType)
{
return method.Modifiers.Any(x => x.IsKind(SyntaxKind.StaticKeyword))
&& (IsExplicitlyPrivate() || IsImplicityPrivate());
bool IsExplicitlyPrivate() =>
HasAnyModifier(method, SyntaxKind.PrivateKeyword) && !HasAnyModifier(method, SyntaxKind.ProtectedKeyword);
// The default accessibility for record class members is private, but for record structs (like all structs) it's internal.
bool IsImplicityPrivate() =>
!HasAnyModifier(method, SyntaxKind.PublicKeyword, SyntaxKind.ProtectedKeyword, SyntaxKind.InternalKeyword)
&& IsClassOrRecordClassOrInterfaceDeclaration(containingType);
static bool IsClassOrRecordClassOrInterfaceDeclaration(TypeDeclarationSyntax type) =>
type is ClassDeclarationSyntax or InterfaceDeclarationSyntax
|| (RecordDeclarationSyntaxWrapper.IsInstance(type) && !((RecordDeclarationSyntaxWrapper)type).ClassOrStructKeyword.IsKind(SyntaxKind.StructKeyword));
static bool HasAnyModifier(MethodDeclarationSyntax method, params SyntaxKind[] modifiers) =>
method.Modifiers.Any(x => x.IsAnyKind(modifiers));
}
private static TypeDeclarationSyntax LowestCommonAncestorOrSelf(IEnumerable<TypeDeclarationSyntax> declaredTypes)
{
var typeHierarchyFromTopToBottom = declaredTypes.Select(PathFromTop);
var minPathLength = typeHierarchyFromTopToBottom.Select(x => x.Length).Min();
var firstPath = typeHierarchyFromTopToBottom.First();
var lastCommonPathIndex = 0;
for (int i = 0; i < minPathLength; i++)
{
var isPartOfCommonPath = typeHierarchyFromTopToBottom.All(x => x[i] == firstPath[i]);
if (isPartOfCommonPath)
{
lastCommonPathIndex = i;
}
else
{
break;
}
}
return firstPath[lastCommonPathIndex];
static TypeDeclarationSyntax[] PathFromTop(SyntaxNode node) =>
node.AncestorsAndSelf()
.OfType<TypeDeclarationSyntax>()
.Distinct()
.Reverse()
.ToArray();
}
private static IEnumerable<MethodUsedByTypes> TypesWhichUseTheMethods(
IEnumerable<MethodDeclarationSyntax> methods, TypeDeclarationSyntax outerType, SemanticModel model)
{
var collector = new PotentialMethodReferenceCollector(methods);
collector.Visit(outerType);
return collector.PotentialMethodReferences
.Where(x => !OnlyUsedByOuterType(x))
.Select(DeclaredTypesWhichActuallyUseTheMethod)
.Where(x => x.Types.Any())
.ToArray();
MethodUsedByTypes DeclaredTypesWhichActuallyUseTheMethod(MethodWithPotentialReferences m)
{
var methodSymbol = model.GetDeclaredSymbol(m.Method);
var typesWhichUseTheMethod = m.PotentialReferences
.Where(x =>
!IsRecursiveMethodCall(x, m.Method)
&& model.GetSymbolOrCandidateSymbol(x) is IMethodSymbol { } methodReference
&& (methodReference.Equals(methodSymbol) || methodReference.ConstructedFrom.Equals(methodSymbol)))
.Select(ContainingTypeDeclaration)
.Distinct()
.ToArray();
return new MethodUsedByTypes(m.Method, typesWhichUseTheMethod);
}
bool IsRecursiveMethodCall(IdentifierNameSyntax methodCall, MethodDeclarationSyntax methodDeclaration) =>
methodCall.Ancestors().OfType<MethodDeclarationSyntax>().FirstOrDefault() == methodDeclaration;
bool OnlyUsedByOuterType(MethodWithPotentialReferences m) =>
m.PotentialReferences.All(x => ContainingTypeDeclaration(x) == outerType);
static TypeDeclarationSyntax ContainingTypeDeclaration(IdentifierNameSyntax identifier) =>
identifier
.Ancestors()
.OfType<TypeDeclarationSyntax>()
.First();
}
private sealed record MethodWithPotentialReferences(MethodDeclarationSyntax Method, IdentifierNameSyntax[] PotentialReferences);
private sealed record MethodUsedByTypes(MethodDeclarationSyntax Method, TypeDeclarationSyntax[] Types);
/// <summary>
/// Collects all the potential references to a set of methods inside the given syntax node.
/// The collector looks for identifiers which match any of the methods' names, but does not try to resolve them to symbols with the semantic model.
/// Performance gains: by only using the syntax tree to find matches we can eliminate certain methods (which are only used by the type which has declared it) without using the more costly symbolic lookup.
/// </summary>
private sealed class PotentialMethodReferenceCollector : CSharpSyntaxWalker
{
private readonly ISet<MethodDeclarationSyntax> methodsToFind;
private readonly Dictionary<MethodDeclarationSyntax, List<IdentifierNameSyntax>> potentialMethodReferences;
public IEnumerable<MethodWithPotentialReferences> PotentialMethodReferences =>
potentialMethodReferences.Select(x => new MethodWithPotentialReferences(x.Key, x.Value.ToArray()));
public PotentialMethodReferenceCollector(IEnumerable<MethodDeclarationSyntax> methodsToFind)
{
this.methodsToFind = new HashSet<MethodDeclarationSyntax>(methodsToFind);
potentialMethodReferences = new();
}
public override void VisitIdentifierName(IdentifierNameSyntax node)
{
if (methodsToFind.FirstOrDefault(x => x.Identifier.ValueText == node.Identifier.ValueText) is { } method)
{
var referenceList = potentialMethodReferences.GetOrAdd(method, _ => new());
referenceList.Add(node);
}
}
}
}