Skip to content

Commit 095d517

Browse files
Address comments round 1
1 parent 820c400 commit 095d517

File tree

3 files changed

+14
-20
lines changed

3 files changed

+14
-20
lines changed

analyzers/src/SonarAnalyzer.CSharp/Helpers/CSharpSymbolUsageCollector.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,7 @@ static bool IsValueNameOrType(AttributeArgumentSyntax a) =>
159159

160160
public override void VisitIdentifierName(IdentifierNameSyntax node)
161161
{
162-
if (IsKnownIdentifier(node.Identifier)
163-
|| knownSymbolNames.Contains($"get_{node.Identifier.ValueText}")
164-
|| knownSymbolNames.Contains($"set_{node.Identifier.ValueText}"))
162+
if (IsKnownIdentifier(node.Identifier))
165163
{
166164
var symbols = GetSymbols(node);
167165
TryStoreFieldAccess(node, symbols);
@@ -250,17 +248,6 @@ public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node)
250248
base.VisitPropertyDeclaration(node);
251249
}
252250

253-
public override void VisitAccessorDeclaration(AccessorDeclarationSyntax node)
254-
{
255-
if (semanticModel.GetDeclaredSymbol(node) is { } symbol
256-
&& knownSymbolNames.Contains(symbol.Name))
257-
{
258-
var usage = GetFieldSymbolUsage(symbol);
259-
usage.Declaration = node;
260-
}
261-
base.VisitAccessorDeclaration(node);
262-
}
263-
264251
private SymbolAccess ParentAccessType(SyntaxNode node) =>
265252
node.Parent switch
266253
{

analyzers/src/SonarAnalyzer.CSharp/Rules/UnusedPrivateMember.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private static void NamedSymbolAction(SonarSymbolReportingContext context, HashS
8484
var fieldLikeSymbols = new BidirectionalDictionary<ISymbol, SyntaxNode>();
8585
if (GatherSymbols(namedType, context.Compilation, privateSymbols, removableInternalTypes, fieldLikeSymbols, context)
8686
&& privateSymbols.Any()
87-
&& new CSharpSymbolUsageCollector(context.Compilation, privateSymbols) is var usageCollector
87+
&& new CSharpSymbolUsageCollector(context.Compilation, AssociatedSymbols(privateSymbols)) is var usageCollector
8888
&& VisitDeclaringReferences(namedType, usageCollector, context, includeGeneratedFile: true))
8989
{
9090
foreach (var diagnostic in DiagnosticsForUnusedPrivateMembers(usageCollector, privateSymbols, SyntaxConstants.Private, fieldLikeSymbols))
@@ -98,6 +98,9 @@ private static void NamedSymbolAction(SonarSymbolReportingContext context, HashS
9898
}
9999
}
100100

101+
private static IEnumerable<ISymbol> AssociatedSymbols(IEnumerable<ISymbol> privateSymbols) =>
102+
privateSymbols.Select(x => x is IMethodSymbol { AssociatedSymbol: IPropertySymbol property } ? property : x);
103+
101104
private static bool GatherSymbols(INamedTypeSymbol namedType,
102105
Compilation compilation,
103106
HashSet<ISymbol> privateSymbols,
@@ -184,8 +187,8 @@ private static bool IsAccessorUsed(ISymbol symbol, CSharpSymbolUsageCollector us
184187
symbol is IMethodSymbol { } accessor
185188
&& accessor.AssociatedSymbol is IPropertySymbol { } property
186189
&& usageCollector.PropertyAccess.TryGetValue(property, out var access)
187-
&& ((access is AccessorAccess.Get or AccessorAccess.Both && accessor.MethodKind == MethodKind.PropertyGet)
188-
|| (access is AccessorAccess.Set or AccessorAccess.Both && accessor.MethodKind == MethodKind.PropertySet));
190+
&& ((access.HasFlag(AccessorAccess.Get) && accessor.MethodKind == MethodKind.PropertyGet)
191+
|| (access.HasFlag(AccessorAccess.Set) && accessor.MethodKind == MethodKind.PropertySet));
189192

190193
private static string GetFieldAccessibilityForMessage(ISymbol symbol) =>
191194
symbol.DeclaredAccessibility == Accessibility.Private ? SyntaxConstants.Private : "private class";
@@ -436,11 +439,12 @@ public override void VisitMethodDeclaration(MethodDeclarationSyntax node)
436439

437440
base.VisitMethodDeclaration(node);
438441
}
442+
439443
public override void VisitAccessorDeclaration(AccessorDeclarationSyntax node)
440444
{
441445
if (node.Modifiers.Any(SyntaxKind.PrivateKeyword))
442446
{
443-
ConditionalStore(GetDeclaredSymbol(node), IsRemovableMember);
447+
ConditionalStore(GetDeclaredSymbol(node), IsRemovable);
444448
}
445449

446450
base.VisitAccessorDeclaration(node);

analyzers/tests/SonarAnalyzer.Test/Rules/UnusedPrivateMemberTest.Properties.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,16 @@ public class PropertyUsages
148148
public int BProperty { get; private set; } // Noncompliant {{Remove the unused private setter 'set_BProperty'.}}
149149
public int CProperty { internal get; set; } // Compliant
150150
public int DProperty { get; internal set; } // Compliant
151-
protected int EProperty { private get; set; } // Noncompliant {{Remove the unused private getter 'get_EProperty'.}}
151+
public int EProperty { protected get; set; } // Compliant
152+
public int E2Property { get; protected set; } // Compliant
152153
public int FProperty { get; private set; } // Compliant
153154
public int GProperty { private get; set; } // Noncompliant {{Remove the unused private getter 'get_GProperty'.}}
154155
public int HProperty { get; private set; } // Noncompliant {{Remove the unused private setter 'set_HProperty'.}}
155156
public int IProperty { private get; set; } // Compliant
156157
public int JProperty { get; private set; } // Compliant: both read and write
157158
public int KProperty { private get; set; } // Compliant: both read and write
158159
public int LProperty { get; private set; } // FN: private set is used in the constructor, not necessary
160+
protected int MProperty { private get; set; } // Noncompliant {{Remove the unused private getter 'get_MProperty'.}}
159161
160162
public PropertyUsages()
161163
{
@@ -174,11 +176,12 @@ public void Method()
174176
public interface ISomeInterface
175177
{
176178
string Something { get; }
179+
string SomethingElse { get; }
177180
}
178181
179182
public class SomeClass : ISomeInterface
180183
{
181-
public string Something { get; private set; }
184+
public string Something { get; private set; } // Compliant
182185
public string SomethingElse { get; private set; } // Noncompliant
183186
184187
public void Method(string str)

0 commit comments

Comments
 (0)