Skip to content

Commit

Permalink
Unify HasAttribute and HasAnyAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Jul 31, 2023
1 parent f57fa2c commit d2a70fe
Show file tree
Hide file tree
Showing 42 changed files with 89 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override void Initialize(AnalysisContext context)
var namedType = (INamedTypeSymbol)sac.Symbol;

if (namedType.TypeKind == TypeKind.Class &&
namedType.HasAttribute(diagnosticAnalyzerAttribute) &&
namedType.HasAnyAttribute(diagnosticAnalyzerAttribute) &&
!namedType.DerivesFrom(diagnosticAnalyzer, baseTypesOnly: true))
{
sac.ReportDiagnostic(namedType.Locations[0].CreateDiagnostic(Rule, namedType.Name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void Analyze(SymbolAnalysisContext analysisContext)
private bool IsOutParameter(IParameterSymbol parameterSymbol) =>
parameterSymbol.RefKind == RefKind.Out ||
// Handle VB.NET special case for out parameters
(parameterSymbol.RefKind == RefKind.Ref && parameterSymbol.HasAttribute(outAttributeSymbol));
(parameterSymbol.RefKind == RefKind.Ref && parameterSymbol.HasAnyAttribute(outAttributeSymbol));

private bool IsTryPatternMethod(IMethodSymbol methodSymbol, int numberOfOutParams) =>
methodSymbol.Name.StartsWith("Try", StringComparison.Ordinal) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, KnownTypes know
}

// Special case: the DataContractSerializer requires that a public setter exists.
if (property.HasAttribute(knownTypes.DataMemberAttribute))
if (property.HasAnyAttribute(knownTypes.DataMemberAttribute))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override void Initialize(AnalysisContext context)

// Do not report on types marked with StructLayoutAttribute
// See https://github.com/dotnet/roslyn-analyzers/issues/4149
if (field.ContainingType.HasAttribute(structLayoutAttributeType))
if (field.ContainingType.HasAnyAttribute(structLayoutAttributeType))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public override void Initialize(AnalysisContext context)
{
// VB.NET out is a ref parameter with the OutAttribute
if (parameterSymbol.RefKind == RefKind.Ref &&
!parameterSymbol.HasAttribute(outAttributeType))
!parameterSymbol.HasAnyAttribute(outAttributeType))
{
context.ReportDiagnostic(parameterSymbol.CreateDiagnostic(Rule, parameterSymbol.Name));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo
}

// If enum is Int64 and has Flags attributes then exit
if (underlyingType == SpecialType.System_Int64 && symbol.HasAttribute(flagsAttribute))
if (underlyingType == SpecialType.System_Int64 && symbol.HasAnyAttribute(flagsAttribute))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext symbolContext, INamedTyp

if (EnumHelpers.TryGetEnumMemberValues(symbol, out IList<ulong> memberValues))
{
if (symbol.HasAttribute(flagsAttributeType))
if (symbol.HasAnyAttribute(flagsAttributeType))
{
// Check "CA2217: Do not mark enums with FlagsAttribute"
if (reportCA2217 && !ShouldBeFlags(memberValues, out IEnumerable<ulong> missingValues))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo

ImmutableArray<IFieldSymbol> zeroValuedFields = GetZeroValuedFields(symbol).ToImmutableArray();

if (symbol.HasAttribute(flagsAttribute))
if (symbol.HasAnyAttribute(flagsAttribute))
{
CheckFlags(symbol, zeroValuedFields, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private static void AnalyzeSymbol<TContext>(INamedTypeSymbol symbol, INamedTypeS
return;
}

if (!symbol.HasAttribute(attributeUsageAttributeType))
if (!symbol.HasAnyAttribute(attributeUsageAttributeType))
{
addDiagnostic(context, symbol.CreateDiagnostic(Rule, symbol.Name));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo

// If either the property or method is marked as obsolete, bail out
// see https://github.com/dotnet/roslyn-analyzers/issues/2956
if (symbol.HasAttribute(obsoleteAttributeType))
if (symbol.HasAnyAttribute(obsoleteAttributeType))
{
return;
}
Expand Down Expand Up @@ -116,7 +116,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo

// If either the property or method is marked as obsolete, bail out
// see https://github.com/dotnet/roslyn-analyzers/issues/2956
if (member.HasAttribute(obsoleteAttributeType))
if (member.HasAnyAttribute(obsoleteAttributeType))
{
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static bool IsDelegateTypeWithInvokeMethod(INamedTypeSymbol namedType) =>
INamedTypeSymbol? comSourceInterfacesAttribute = context.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemRuntimeInteropServicesComSourceInterfacesAttribute);
bool ContainingTypeHasComSourceInterfacesAttribute(IEventSymbol eventSymbol) =>
comSourceInterfacesAttribute != null &&
eventSymbol.ContainingType.HasAttribute(comSourceInterfacesAttribute);
eventSymbol.ContainingType.HasAnyAttribute(comSourceInterfacesAttribute);

context.RegisterSymbolAction(symbolContext =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public sealed override void Initialize(AnalysisContext context)
var compilation = startContext.Compilation;
var entryPointContainingType = compilation.GetEntryPoint(startContext.CancellationToken)?.ContainingType;
var wellKnownTypeProvider = WellKnownTypeProvider.GetOrCreate(compilation);
var hasInternalsVisibleTo = startContext.Compilation.Assembly.HasAttribute(
var hasInternalsVisibleTo = startContext.Compilation.Assembly.HasAnyAttribute(
startContext.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemRuntimeCompilerServicesInternalsVisibleToAttribute));

var systemAttributeSymbol = wellKnownTypeProvider.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemAttribute);
Expand Down Expand Up @@ -349,8 +349,8 @@ public static bool IsMefExported(
INamedTypeSymbol? mef1ExportAttributeSymbol,
INamedTypeSymbol? mef2ExportAttributeSymbol)
{
return (mef1ExportAttributeSymbol != null && type.HasAttribute(mef1ExportAttributeSymbol))
|| (mef2ExportAttributeSymbol != null && type.HasAttribute(mef2ExportAttributeSymbol));
return (mef1ExportAttributeSymbol != null && type.HasAnyAttribute(mef1ExportAttributeSymbol))
|| (mef2ExportAttributeSymbol != null && type.HasAnyAttribute(mef2ExportAttributeSymbol));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ bool IsXunitThrowsArgument(IParameterSymbol parameterSymbol)

IMethodSymbol methodSymbol = (IMethodSymbol)operationContext.ContainingSymbol;

return methodSymbol.HasAttribute(expectedExceptionType);
return methodSymbol.HasAnyAttribute(expectedExceptionType);
}
else
{
Expand Down Expand Up @@ -346,7 +346,7 @@ private static bool IsHResultOrErrorCodeReturningMethod(IMethodSymbol method)

private static bool IsPureMethod(IMethodSymbol method, Compilation compilation)
{
return method.HasAttribute(compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemDiagnosticsContractsPureAttribute));
return method.HasAnyAttribute(compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemDiagnosticsContractsPureAttribute));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ private static bool IsExplicitlyVisibleFromCom(IMethodSymbol methodSymbol, WellK
return false;
}

if (methodSymbol.HasAttribute(comVisibleAttribute) ||
methodSymbol.ContainingType.HasAttribute(comVisibleAttribute))
if (methodSymbol.HasAnyAttribute(comVisibleAttribute) ||
methodSymbol.ContainingType.HasAnyAttribute(comVisibleAttribute))
{
return true;
}
Expand Down Expand Up @@ -371,7 +371,7 @@ private static bool IsOnObsoleteMemberChain(ISymbol symbol, WellKnownTypeProvide

while (symbol != null)
{
if (symbol.HasAttribute(obsoleteAttributeType))
if (symbol.HasAnyAttribute(obsoleteAttributeType))
return true;

symbol = symbol is IMethodSymbol method && method.AssociatedSymbol != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private static bool CanDescendIntoOperation(IOperation operation, INamedTypeSymb
if (operation.Kind == OperationKind.Invocation)
{
return isMethodSurroundedWithDirective
|| !((IInvocationOperation)operation).TargetMethod.HasAttribute(conditionalAttributeType);
|| !((IInvocationOperation)operation).TargetMethod.HasAnyAttribute(conditionalAttributeType);
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public override void Initialize(AnalysisContext context)
out INamedTypeSymbol? disableRuntimeMarshallingAttribute))
{
AutoLayoutTypeCache autoLayoutCache = new(context.Compilation);
var hasDisableRuntimeMarshallingAttribute = context.Compilation.Assembly.HasAttribute(disableRuntimeMarshallingAttribute);
var hasDisableRuntimeMarshallingAttribute = context.Compilation.Assembly.HasAnyAttribute(disableRuntimeMarshallingAttribute);
if (hasDisableRuntimeMarshallingAttribute)
{
var disabledRuntimeMarshallingAssemblyAnalyzer = new DisabledRuntimeMarshallingAssemblyAnalyzer(context.Compilation, autoLayoutCache);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private void AnalyzeInvocation(OperationAnalysisContext context)

void AnalyzeDelegateType(OperationAnalysisContext context, IOperation operation, INamedTypeSymbol delegateType)
{
if (delegateType.ContainingAssembly.HasAttribute(_disableRuntimeMarshallingAttribute))
if (delegateType.ContainingAssembly.HasAnyAttribute(_disableRuntimeMarshallingAttribute))
{
AnalyzeMethodSignature(_autoLayoutCache, context.ReportDiagnostic, delegateType.DelegateInvokeMethod!, ImmutableArray.Create(operation.Syntax.GetLocation()), FeatureUnsupportedWhenRuntimeMarshallingDisabledDelegateUsage);
}
Expand Down Expand Up @@ -137,7 +137,7 @@ private void AnalyzeMethod(Action<Diagnostic> reportDiagnostic, IMethodSymbol sy

void AnalyzeDelegateMethodSignature(INamedTypeSymbol delegateType, ISymbol signatureSymbol)
{
if (delegateType.ContainingAssembly.HasAttribute(_disableRuntimeMarshallingAttribute))
if (delegateType.ContainingAssembly.HasAnyAttribute(_disableRuntimeMarshallingAttribute))
{
AnalyzeMethodSignature(_autoLayoutCache, reportDiagnostic, delegateType.DelegateInvokeMethod!, signatureSymbol.Locations, FeatureUnsupportedWhenRuntimeMarshallingDisabledDelegateUsage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void AnalyzeType(SymbolAnalysisContext context)
{
Debug.Assert(_unmanagedFunctionPointerAttribute is not null);
INamedTypeSymbol type = (INamedTypeSymbol)context.Symbol;
if (type.TypeKind != TypeKind.Delegate || !type.HasAttribute(_unmanagedFunctionPointerAttribute))
if (type.TypeKind != TypeKind.Delegate || !type.HasAnyAttribute(_unmanagedFunctionPointerAttribute))
{
return;
}
Expand Down Expand Up @@ -152,7 +152,7 @@ private void AnalyzeMethod(Action<Diagnostic> reportDiagnostic, IMethodSymbol me
reportDiagnostic(method.CreateDiagnostic(FeatureUnsupportedWhenRuntimeMarshallingDisabledHResultSwapping));
}

if (method.HasAttribute(_lcidConversionAttribute))
if (method.HasAnyAttribute(_lcidConversionAttribute))
{
reportDiagnostic(method.CreateDiagnostic(FeatureUnsupportedWhenRuntimeMarshallingDisabledUsingLCIDConversionAttribute));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo
{
if (parameter.Type.SpecialType == SpecialType.System_String
&& parameter.RefKind == RefKind.None
&& parameter.HasAttribute(outAttributeType))
&& parameter.HasAnyAttribute(outAttributeType))
{
context.ReportDiagnostic(parameter.CreateDiagnostic(Rule, parameter.Name));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private static void AnalyzeType(SymbolAnalysisContext context, INamedTypeSymbol
return;
}

if (!targetType.HasAttribute(dynamicInterfaceCastableImplementationAttribute))
if (!targetType.HasAnyAttribute(dynamicInterfaceCastableImplementationAttribute))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ public bool TryGetConstantExpectedAttributeData([NotNullWhen(true)] IParameterSy

private bool HasConstantExpectedAttributeData(IParameterSymbol parameter)
{
return parameter.HasAttribute(AttributeSymbol);
return parameter.HasAnyAttribute(AttributeSymbol);
}

public static bool TryCreate(Compilation compilation, [NotNullWhen(true)] out ConstantExpectedContext? constantExpectedContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public override void Initialize(AnalysisContext context)
return;
}

if (context.Compilation.Assembly.HasAttribute(previewFeaturesAttribute))
if (context.Compilation.Assembly.HasAnyAttribute(previewFeaturesAttribute))
{
// This assembly has enabled preview attributes.
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ private static bool ShouldBeLocalized(

// FxCop compat checks.
if (typesToIgnore.Contains(parameterSymbol.ContainingType) ||
parameterSymbol.ContainingSymbol.HasAttribute(conditionalAttributeSymbol))
parameterSymbol.ContainingSymbol.HasAnyAttribute(conditionalAttributeSymbol))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private static void OnCompilationStart(CompilationStartAnalysisContext context)

var candidateTypes = PooledConcurrentSet<INamedTypeSymbol>.GetInstance(SymbolEqualityComparer.Default);
var baseTypes = PooledConcurrentSet<INamedTypeSymbol>.GetInstance(SymbolEqualityComparer.Default);
var hasInternalsVisibleTo = context.Compilation.Assembly.HasAttribute(
var hasInternalsVisibleTo = context.Compilation.Assembly.HasAnyAttribute(
context.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemRuntimeCompilerServicesInternalsVisibleToAttribute));

context.RegisterSymbolAction(context =>
Expand All @@ -55,7 +55,7 @@ private static void OnCompilationStart(CompilationStartAnalysisContext context)
!type.IsStatic &&
!type.IsSealed &&
!type.IsExternallyVisible() &&
!type.HasAttribute(comImportAttributeType) &&
!type.HasAnyAttribute(comImportAttributeType) &&
!type.IsTopLevelStatementsEntryPointType())
{
candidateTypes.Add(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public void AnalyzeSymbol(SymbolAnalysisContext context)
}

// Check for [NonSerialized]
if (field.HasAttribute(_nonSerializedAttributeTypeSymbol))
if (field.HasAnyAttribute(_nonSerializedAttributeTypeSymbol))
{
continue;
}
Expand Down Expand Up @@ -276,7 +276,7 @@ private bool IsSerializable(ITypeSymbol type)
TypeKind.Class or TypeKind.Struct => ((INamedTypeSymbol)type).IsSerializable,// Check SerializableAttribute or Serializable flag from metadata.
TypeKind.Delegate => true,// delegates are always serializable, even if
// they aren't actually marked [Serializable]
_ => type.HasAttribute(_serializableAttributeTypeSymbol),
_ => type.HasAnyAttribute(_serializableAttributeTypeSymbol),
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public override void Initialize(AnalysisContext context)

foreach (IMethodSymbol method in methodSymbols)
{
if (!method.HasAttribute(systemObsoleteAttribute)
if (!method.HasAnyAttribute(systemObsoleteAttribute)
&& HasSupersetOfParameterTypes(method, methodSymbol)
&& method.Name != containingMethodName
&& HasAsyncCompatibleReturnType(method, syncBlockingTypes))
Expand Down Expand Up @@ -234,7 +234,7 @@ static bool CheckReturnTypeMatch(string targetType, ISymbol returnType, Concurre
|| CheckReturnTypeMatch("ValueTask", returnType, syncBlockingTypes)
|| CheckReturnTypeMatch("IAsyncEnumerableGeneric", returnType, syncBlockingTypes)
|| (syncBlockingTypes.TryGetValue("AsyncMethodBuilderAttribute", out INamedTypeSymbol? asyncMethodBuilderAttributeTypeValue)
&& returnType.HasAttribute(asyncMethodBuilderAttributeTypeValue));
&& returnType.HasAnyAttribute(asyncMethodBuilderAttributeTypeValue));
}

private static IMethodSymbol? GetParentMethodOrDelegate(OperationAnalysisContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public override void Initialize(AnalysisContext context)
}

// Once we have the field, see if it's attributed with [ThreadStatic].
if (fieldSymbol?.HasAttribute(threadStaticAttributeType) == true)
if (fieldSymbol?.HasAnyAttribute(threadStaticAttributeType) == true)
{
context.ReportDiagnostic(symbol.CreateDiagnostic(ThreadStaticOnNonStaticFieldRule));
}
Expand All @@ -88,7 +88,7 @@ public override void Initialize(AnalysisContext context)
var fieldInit = (IFieldInitializerOperation)context.Operation;
foreach (IFieldSymbol field in fieldInit.InitializedFields)
{
if (field.IsStatic && field.HasAttribute(threadStaticAttributeType))
if (field.IsStatic && field.HasAnyAttribute(threadStaticAttributeType))
{
context.ReportDiagnostic(fieldInit.CreateDiagnostic(ThreadStaticInitializedInlineRule));
break;
Expand Down
Loading

0 comments on commit d2a70fe

Please sign in to comment.