Skip to content

Commit b9bc28c

Browse files
committed
Refactor PropertiesShouldNotBeWriteOnlyAnalyzer (CA1044)
1 parent 3587540 commit b9bc28c

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/PropertiesShouldNotBeWriteOnly.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,40 +57,41 @@ public override void Initialize(AnalysisContext context)
5757
/// </summary>
5858
private static void AnalyzeSymbol(SymbolAnalysisContext context)
5959
{
60-
if (context.Symbol is not IPropertySymbol property)
61-
{
62-
return;
63-
}
60+
var property = (IPropertySymbol)context.Symbol;
6461

6562
// not raising a violation for when:
6663
// property is overridden because the issue can only be fixed in the base type
6764
// property is the implementation of any interface member
68-
if (property.IsOverride || property.IsImplementationOfAnyInterfaceMember())
65+
if (property.IsOverride || GetRule(property) is not DiagnosticDescriptor descriptor || property.IsImplementationOfAnyInterfaceMember())
6966
{
7067
return;
7168
}
7269

7370
Debug.Assert(context.Options.MatchesConfiguredVisibility(MakeMoreAccessibleRule, property, context.Compilation) == context.Options.MatchesConfiguredVisibility(AddGetterRule, property, context.Compilation));
71+
Debug.Assert(descriptor == MakeMoreAccessibleRule || descriptor == AddGetterRule);
7472

73+
// Only analyze externally visible properties by default
74+
if (context.Options.MatchesConfiguredVisibility(descriptor, property, context.Compilation))
75+
{
76+
context.ReportDiagnostic(property.CreateDiagnostic(descriptor, property.Name));
77+
}
78+
}
79+
80+
private static DiagnosticDescriptor? GetRule(IPropertySymbol property)
81+
{
7582
// We handled the non-CA1044 cases earlier. Now, we handle CA1044 cases
7683
// If there is no getter then it is not accessible
7784
if (property.IsWriteOnly)
7885
{
79-
// Only analyze externally visible properties by default
80-
if (context.Options.MatchesConfiguredVisibility(AddGetterRule, property, context.Compilation))
81-
{
82-
context.ReportDiagnostic(property.CreateDiagnostic(AddGetterRule, property.Name));
83-
}
86+
return AddGetterRule;
8487
}
8588
// Otherwise if there is a setter, check for its relative accessibility
8689
else if (!property.IsReadOnly && (property.GetMethod!.DeclaredAccessibility < property.SetMethod!.DeclaredAccessibility))
8790
{
88-
// Only analyze externally visible properties by default
89-
if (context.Options.MatchesConfiguredVisibility(MakeMoreAccessibleRule, property, context.Compilation))
90-
{
91-
context.ReportDiagnostic(property.CreateDiagnostic(MakeMoreAccessibleRule, property.Name));
92-
}
91+
return MakeMoreAccessibleRule;
9392
}
93+
94+
return null;
9495
}
9596
}
9697
}

src/Utilities/Compiler/Extensions/ISymbolExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,17 +563,17 @@ public static ISymbol GetOverriddenMember(this ISymbol symbol)
563563
/// </summary>
564564
public static bool IsImplementationOfAnyExplicitInterfaceMember([NotNullWhen(returnValue: true)] this ISymbol? symbol)
565565
{
566-
if (symbol is IMethodSymbol methodSymbol && methodSymbol.ExplicitInterfaceImplementations.Any())
566+
if (symbol is IMethodSymbol methodSymbol && !methodSymbol.ExplicitInterfaceImplementations.IsEmpty)
567567
{
568568
return true;
569569
}
570570

571-
if (symbol is IPropertySymbol propertySymbol && propertySymbol.ExplicitInterfaceImplementations.Any())
571+
if (symbol is IPropertySymbol propertySymbol && !propertySymbol.ExplicitInterfaceImplementations.IsEmpty)
572572
{
573573
return true;
574574
}
575575

576-
if (symbol is IEventSymbol eventSymbol && eventSymbol.ExplicitInterfaceImplementations.Any())
576+
if (symbol is IEventSymbol eventSymbol && !eventSymbol.ExplicitInterfaceImplementations.IsEmpty)
577577
{
578578
return true;
579579
}

0 commit comments

Comments
 (0)