Skip to content

Commit 9bea835

Browse files
committed
Small refactoring
1 parent abb5bf7 commit 9bea835

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/QualityGuidelines/MarkMembersAsStatic.cs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,7 @@ void OnOperationBlockStart(OperationBlockStartAnalysisContext blockStartContext)
9898
}
9999

100100
// Don't run any other check for this method if it isn't a valid analysis context
101-
if (!ShouldAnalyze(methodSymbol, wellKnownTypeProvider, skippedAttributes,
102-
blockStartContext.Options, isWebProject, blockStartContext.CancellationToken))
103-
{
104-
return;
105-
}
106-
107-
// Don't report methods which have a single throw statement
108-
// with NotImplementedException or NotSupportedException
109-
if (blockStartContext.IsMethodNotImplementedOrSupported())
101+
if (!ShouldAnalyze(methodSymbol, wellKnownTypeProvider, skippedAttributes, isWebProject, blockStartContext))
110102
{
111103
return;
112104
}
@@ -191,9 +183,10 @@ private static bool ShouldAnalyze(
191183
IMethodSymbol methodSymbol,
192184
WellKnownTypeProvider wellKnownTypeProvider,
193185
ImmutableArray<INamedTypeSymbol> skippedAttributes,
194-
AnalyzerOptions options,
195186
bool isWebProject,
196-
CancellationToken cancellationToken)
187+
#pragma warning disable RS1012 // Start action has no registered actions
188+
OperationBlockStartAnalysisContext blockStartContext)
189+
#pragma warning restore RS1012 // Start action has no registered actions
197190
{
198191
// Modifiers that we don't care about
199192
if (methodSymbol.IsStatic || methodSymbol.IsOverride || methodSymbol.IsVirtual ||
@@ -208,15 +201,31 @@ private static bool ShouldAnalyze(
208201
return false;
209202
}
210203

211-
// Do not analyze public APIs for web projects
212-
// See https://github.com/dotnet/roslyn-analyzers/issues/3835 for details.
213-
if (isWebProject && methodSymbol.IsExternallyVisible())
204+
// Don't report methods which have a single throw statement
205+
// with NotImplementedException or NotSupportedException
206+
if (blockStartContext.IsMethodNotImplementedOrSupported())
214207
{
215208
return false;
216209
}
217210

218-
// CA1000 says one shouldn't declare static members on generic types. So don't flag such cases.
219-
if (methodSymbol.ContainingType.IsGenericType && methodSymbol.IsExternallyVisible())
211+
if (methodSymbol.IsExternallyVisible())
212+
{
213+
// Do not analyze public APIs for web projects
214+
// See https://github.com/dotnet/roslyn-analyzers/issues/3835 for details.
215+
if (isWebProject)
216+
{
217+
return false;
218+
}
219+
220+
// CA1000 says one shouldn't declare static members on generic types. So don't flag such cases.
221+
if (methodSymbol.ContainingType.IsGenericType)
222+
{
223+
return false;
224+
}
225+
}
226+
227+
// We consider that auto-property have the intent to always be instance members so we want to workaround this issue.
228+
if (methodSymbol.IsAutoPropertyAccessor())
220229
{
221230
return false;
222231
}
@@ -248,14 +257,14 @@ private static bool ShouldAnalyze(
248257
return false;
249258
}
250259

251-
if (!options.MatchesConfiguredVisibility(Rule, methodSymbol, wellKnownTypeProvider.Compilation, cancellationToken,
252-
defaultRequiredVisibility: SymbolVisibilityGroup.All))
260+
var hasCorrectVisibility = blockStartContext.Options.MatchesConfiguredVisibility(Rule, methodSymbol, wellKnownTypeProvider.Compilation,
261+
blockStartContext.CancellationToken, defaultRequiredVisibility: SymbolVisibilityGroup.All);
262+
if (!hasCorrectVisibility)
253263
{
254264
return false;
255265
}
256266

257-
// We consider that auto-property have the intent to always be instance members so we want to workaround this issue.
258-
return !methodSymbol.IsAutoPropertyAccessor();
267+
return true;
259268
}
260269

261270
private static bool IsExplicitlyVisibleFromCom(IMethodSymbol methodSymbol, WellKnownTypeProvider wellKnownTypeProvider)

0 commit comments

Comments
 (0)