Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/BenchmarkDotNet.Analyzers/AnalyzerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static bool AttributeListsContainAttribute(INamedTypeSymbol? attributeTyp
continue;
}

if (attributeSyntaxTypeSymbol.Equals(attributeTypeSymbol))
if (SymbolEqualityComparer.Default.Equals(attributeSyntaxTypeSymbol, attributeTypeSymbol))
{
return true;
}
Expand All @@ -56,7 +56,7 @@ public static bool AttributeListContainsAttribute(INamedTypeSymbol? attributeTyp
return false;
}

return attributeList.Any(ad => ad.AttributeClass != null && ad.AttributeClass.Equals(attributeTypeSymbol));
return attributeList.Any(ad => SymbolEqualityComparer.Default.Equals(ad.AttributeClass, attributeTypeSymbol));
}

public static ImmutableArray<AttributeSyntax> GetAttributes(string attributeName, Compilation compilation, SyntaxList<AttributeListSyntax> attributeLists, SemanticModel semanticModel)
Expand All @@ -81,7 +81,7 @@ public static ImmutableArray<AttributeSyntax> GetAttributes(INamedTypeSymbol? at
continue;
}

if (attributeSyntaxTypeSymbol.Equals(attributeTypeSymbol))
if (SymbolEqualityComparer.Default.Equals(attributeSyntaxTypeSymbol, attributeTypeSymbol))
{
attributesBuilder.Add(attributeSyntax);
}
Expand Down Expand Up @@ -118,7 +118,8 @@ public static void Deconstruct<T1, T2>(this KeyValuePair<T1, T2> tuple, out T1 k
}

public static Location GetLocation(this AttributeData attributeData)
=> attributeData.ApplicationSyntaxReference.SyntaxTree.GetLocation(attributeData.ApplicationSyntaxReference.Span);
=> attributeData.ApplicationSyntaxReference?.SyntaxTree.GetLocation(attributeData.ApplicationSyntaxReference.Span)
?? Location.None;

public static bool IsAssignable(TypedConstant constant, ExpressionSyntax expression, ITypeSymbol targetType, Compilation compilation)
{
Expand Down Expand Up @@ -165,16 +166,16 @@ public static ExpressionSyntax GetAttributeParamsArgumentExpression(this Attribu
{
Debug.Assert(index >= 0);
// Properties must come after constructor arguments, so we don't need to worry about it here.
var attrSyntax = (AttributeSyntax) attributeData.ApplicationSyntaxReference.GetSyntax();
var args = attrSyntax.ArgumentList.Arguments;
var attrSyntax = (AttributeSyntax)attributeData.ApplicationSyntaxReference!.GetSyntax();
var args = attrSyntax.ArgumentList!.Arguments;
Debug.Assert(args is { Count: > 0 });
var maybeArrayExpression = args[0].Expression;

#if CODE_ANALYSIS_4_8
if (maybeArrayExpression is CollectionExpressionSyntax collectionExpressionSyntax)
{
Debug.Assert(index < collectionExpressionSyntax.Elements.Count);
return ((ExpressionElementSyntax) collectionExpressionSyntax.Elements[index]).Expression;
return ((ExpressionElementSyntax)collectionExpressionSyntax.Elements[index]).Expression;
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class ArgumentsAttributeAnalyzer : DiagnosticAnalyzer
"Usage",
DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: AnalyzerHelper.GetResourceString(nameof(BenchmarkDotNetAnalyzerResources.Attributes_ArgumentsAttribute_MustHaveMatchingValueType_Description)));
description: AnalyzerHelper.GetResourceString(nameof(BenchmarkDotNetAnalyzerResources.Attributes_ArgumentsAttribute_RequiresParameters_Description)));

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => new DiagnosticDescriptor[]
{
Expand Down Expand Up @@ -95,15 +95,15 @@ private static void AnalyzeMethodSymbol(SymbolAnalysisContext context)
var argumentsSourceAttributes = new List<AttributeData>();
foreach (var attr in methodSymbol.GetAttributes())
{
if (attr.AttributeClass.Equals(benchmarkAttributeTypeSymbol))
if (SymbolEqualityComparer.Default.Equals(attr.AttributeClass, benchmarkAttributeTypeSymbol))
{
hasBenchmarkAttribute = true;
}
else if (attr.AttributeClass.Equals(argumentsAttributeTypeSymbol))
else if (SymbolEqualityComparer.Default.Equals(attr.AttributeClass, argumentsAttributeTypeSymbol))
{
argumentsAttributes.Add(attr);
}
else if (attr.AttributeClass.Equals(argumentsSourceAttributeTypeSymbol))
else if (SymbolEqualityComparer.Default.Equals(attr.AttributeClass, argumentsSourceAttributeTypeSymbol))
{
argumentsSourceAttributes.Add(attr);
}
Expand Down Expand Up @@ -150,10 +150,10 @@ private static void AnalyzeMethodSymbol(SymbolAnalysisContext context)
}
else
{
var syntax = (AttributeSyntax) attr.ApplicationSyntaxReference.GetSyntax();
var syntax = (AttributeSyntax)attr.ApplicationSyntaxReference!.GetSyntax();
AnalyzeAssignableValueType(
attr.ConstructorArguments[0],
syntax.ArgumentList.Arguments[0].Expression,
syntax.ArgumentList!.Arguments[0].Expression,
methodSymbol.Parameters[0].Type
);
}
Expand Down Expand Up @@ -200,7 +200,7 @@ void AnalyzeAssignableValueType(TypedConstant value, ExpressionSyntax expression
expression.GetLocation(),
expression.ToString(),
parameterType.ToDisplayString(),
value.IsNull ? "null" : value.Type.ToDisplayString())
value.IsNull ? "null" : value.Type!.ToDisplayString())
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ private static void Analyze(SyntaxNodeAnalysisContext context)
if (attributeSyntaxTypeSymbol == null
|| attributeSyntaxTypeSymbol.TypeKind == TypeKind.Error
||
(!attributeSyntaxTypeSymbol.Equals(paramsAttributeTypeSymbol)
&& !attributeSyntaxTypeSymbol.Equals(paramsSourceAttributeTypeSymbol)
&& !attributeSyntaxTypeSymbol.Equals(paramsAllValuesAttributeTypeSymbol)))
(!SymbolEqualityComparer.Default.Equals(attributeSyntaxTypeSymbol, paramsAttributeTypeSymbol)
&& !SymbolEqualityComparer.Default.Equals(attributeSyntaxTypeSymbol, paramsSourceAttributeTypeSymbol)
&& !SymbolEqualityComparer.Default.Equals(attributeSyntaxTypeSymbol, paramsAllValuesAttributeTypeSymbol)))
{
return;
}
Expand All @@ -153,10 +153,10 @@ private static void Analyze(SyntaxNodeAnalysisContext context)

ImmutableArray<AttributeSyntax> declaredAttributes;
bool fieldOrPropertyIsPublic;
Location fieldConstModifierLocation = null;
Location fieldReadonlyModifierLocation = null;
Location? fieldConstModifierLocation = null;
Location? fieldReadonlyModifierLocation = null;
string fieldOrPropertyIdentifier;
Location propertyInitAccessorKeywordLocation = null;
Location? propertyInitAccessorKeywordLocation = null;
Location fieldOrPropertyIdentifierLocation;
bool propertyIsMissingAssignableSetter = false;
DiagnosticDescriptor fieldOrPropertyCannotHaveMoreThanOneParameterAttributeAppliedDiagnosticRule;
Expand Down Expand Up @@ -186,11 +186,11 @@ private static void Analyze(SyntaxNodeAnalysisContext context)

#if CODE_ANALYSIS_3_8
var propertyInitAccessorIndex = propertyDeclarationSyntax.AccessorList?.Accessors.IndexOf(SyntaxKind.InitAccessorDeclaration);
propertyInitAccessorKeywordLocation = propertyInitAccessorIndex >= 0 ? propertyDeclarationSyntax.AccessorList.Accessors[propertyInitAccessorIndex.Value].Keyword.GetLocation() : null;
propertyInitAccessorKeywordLocation = propertyInitAccessorIndex >= 0 ? propertyDeclarationSyntax.AccessorList!.Accessors[propertyInitAccessorIndex.Value].Keyword.GetLocation() : null;
#endif

var propertySetAccessorIndex = propertyDeclarationSyntax.AccessorList?.Accessors.IndexOf(SyntaxKind.SetAccessorDeclaration);
propertyIsMissingAssignableSetter = !propertySetAccessorIndex.HasValue || propertySetAccessorIndex.Value < 0 || propertyDeclarationSyntax.AccessorList.Accessors[propertySetAccessorIndex.Value].Modifiers.Any();
propertyIsMissingAssignableSetter = !propertySetAccessorIndex.HasValue || propertySetAccessorIndex.Value < 0 || propertyDeclarationSyntax.AccessorList!.Accessors[propertySetAccessorIndex.Value].Modifiers.Any();

fieldOrPropertyIdentifierLocation = propertyDeclarationSyntax.Identifier.GetLocation();
fieldOrPropertyCannotHaveMoreThanOneParameterAttributeAppliedDiagnosticRule = MutuallyExclusiveOnPropertyRule;
Expand All @@ -203,9 +203,9 @@ private static void Analyze(SyntaxNodeAnalysisContext context)

AnalyzeFieldOrPropertySymbol(
context,
paramsAttributeTypeSymbol,
paramsSourceAttributeTypeSymbol,
paramsAllValuesAttributeTypeSymbol,
paramsAttributeTypeSymbol!,
paramsSourceAttributeTypeSymbol!,
paramsAllValuesAttributeTypeSymbol!,
declaredAttributes,
fieldOrPropertyIsPublic,
fieldConstModifierLocation,
Expand All @@ -222,9 +222,9 @@ private static void Analyze(SyntaxNodeAnalysisContext context)

private static void AnalyzeFieldOrPropertySymbol(
SyntaxNodeAnalysisContext context,
INamedTypeSymbol? paramsAttributeTypeSymbol,
INamedTypeSymbol? paramsSourceAttributeTypeSymbol,
INamedTypeSymbol? paramsAllValuesAttributeTypeSymbol,
INamedTypeSymbol paramsAttributeTypeSymbol,
INamedTypeSymbol paramsSourceAttributeTypeSymbol,
INamedTypeSymbol paramsAllValuesAttributeTypeSymbol,
ImmutableArray<AttributeSyntax> declaredAttributes,
bool fieldOrPropertyIsPublic,
Location? fieldConstModifierLocation,
Expand All @@ -238,14 +238,14 @@ private static void AnalyzeFieldOrPropertySymbol(
AttributeSyntax attributeSyntax,
SyntaxNode attributeTarget)
{
ImmutableArray<INamedTypeSymbol> applicableParameterAttributeTypeSymbols = new INamedTypeSymbol[]
{
ImmutableArray<INamedTypeSymbol> applicableParameterAttributeTypeSymbols =
[
paramsAttributeTypeSymbol,
paramsSourceAttributeTypeSymbol,
paramsAllValuesAttributeTypeSymbol
}.ToImmutableArray();
];

var parameterAttributeTypeSymbols = new HashSet<INamedTypeSymbol>();
var parameterAttributeTypeSymbols = new HashSet<INamedTypeSymbol>(SymbolEqualityComparer.Default);

foreach (var declaredAttributeSyntax in declaredAttributes)
{
Expand All @@ -254,7 +254,7 @@ private static void AnalyzeFieldOrPropertySymbol(
{
foreach (var applicableParameterAttributeTypeSymbol in applicableParameterAttributeTypeSymbols)
{
if (declaredAttributeTypeSymbol.Equals(applicableParameterAttributeTypeSymbol))
if (SymbolEqualityComparer.Default.Equals(declaredAttributeTypeSymbol, applicableParameterAttributeTypeSymbol))
{
if (!parameterAttributeTypeSymbols.Add(applicableParameterAttributeTypeSymbol))
{
Expand Down Expand Up @@ -406,7 +406,7 @@ private static void AnalyzeParamsSourceWriteOnlyProperty(
return;
}

var referencedMember = targetType.GetMembers(sourceName).FirstOrDefault();
var referencedMember = targetType.GetMembers(sourceName!).FirstOrDefault();
if (referencedMember is IPropertySymbol propertySymbol
&& propertySymbol.SetMethod != null
&& propertySymbol.GetMethod == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private static void Analyze(SyntaxNodeAnalysisContext context)
var paramsAllValuesAttributeTypeSymbol = GetParamsAllValuesAttributeTypeSymbol(context.Compilation);

var attributeSyntaxTypeSymbol = context.SemanticModel.GetTypeInfo(attributeSyntax).Type;
if (attributeSyntaxTypeSymbol == null || !attributeSyntaxTypeSymbol.Equals(paramsAllValuesAttributeTypeSymbol))
if (!SymbolEqualityComparer.Default.Equals(attributeSyntaxTypeSymbol, paramsAllValuesAttributeTypeSymbol))
{
return;
}
Expand Down Expand Up @@ -111,7 +111,7 @@ private static void AnalyzeFieldOrPropertyTypeSyntax(SyntaxNodeAnalysisContext c
return;
}

if (fieldOrPropertyTypeSymbol.GetAttributes().Any(ad => ad.AttributeClass != null && ad.AttributeClass.Equals(flagsAttributeTypeSymbol)))
if (fieldOrPropertyTypeSymbol.GetAttributes().Any(ad => SymbolEqualityComparer.Default.Equals(ad.AttributeClass, flagsAttributeTypeSymbol)))
{
context.ReportDiagnostic(Diagnostic.Create(NotAllowedOnFlagsEnumPropertyOrFieldTypeRule, fieldOrPropertyTypeSyntax.GetLocation(), fieldOrPropertyTypeSymbol.ToString()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override void Initialize(AnalysisContext analysisContext)

private void Analyze(SymbolAnalysisContext context)
{
ITypeSymbol fieldOrPropertyType = context.Symbol switch
ITypeSymbol? fieldOrPropertyType = context.Symbol switch
{
IFieldSymbol fieldSymbol => fieldSymbol.Type,
IPropertySymbol propertySymbol => propertySymbol.Type,
Expand All @@ -74,7 +74,7 @@ private void Analyze(SymbolAnalysisContext context)

var paramsAttributeTypeSymbol = GetParamsAttributeTypeSymbol(context.Compilation);
var attrs = context.Symbol.GetAttributes();
var paramsAttributes = attrs.Where(attr => attr.AttributeClass.Equals(paramsAttributeTypeSymbol)).ToImmutableArray();
var paramsAttributes = attrs.Where(attr => SymbolEqualityComparer.Default.Equals(attr.AttributeClass, paramsAttributeTypeSymbol)).ToImmutableArray();
if (paramsAttributes.Length != 1)
{
// Don't analyze zero or multiple [Params] (multiple is not legal and already handled by GeneralParameterAttributesAnalyzer).
Expand All @@ -93,10 +93,10 @@ private void Analyze(SymbolAnalysisContext context)
// [Params(null)]
if (attr.ConstructorArguments[0].IsNull)
{
var syntax = (AttributeSyntax) attr.ApplicationSyntaxReference.GetSyntax();
var syntax = (AttributeSyntax)attr.ApplicationSyntaxReference!.GetSyntax();
AnalyzeAssignableValueType(
attr.ConstructorArguments[0],
syntax.ArgumentList.Arguments[0].Expression,
syntax.ArgumentList!.Arguments[0].Expression,
fieldOrPropertyType
);
return;
Expand Down Expand Up @@ -140,7 +140,7 @@ void AnalyzeAssignableValueType(TypedConstant value, ExpressionSyntax expression
expression.GetLocation(),
expression.ToString(),
parameterType.ToDisplayString(),
value.IsNull ? "null" : value.Type.ToDisplayString())
value.IsNull ? "null" : value.Type!.ToDisplayString())
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
<DefineConstants Condition="'$(MccVersion)' >= '3.8'">$(DefineConstants);CODE_ANALYSIS_3_8</DefineConstants>
<DefineConstants Condition="'$(MccVersion)' >= '4.8'">$(DefineConstants);CODE_ANALYSIS_4_8</DefineConstants>
<DefineConstants Condition="'$(MccVersion)' >= '5.0'">$(DefineConstants);CODE_ANALYSIS_5_0</DefineConstants>
<!-- New equality comparison doesn't exist in v2.8, and we only use the default comparison anyway. -->
<!-- Formatting in AnalyzerReleases.Unshipped.md -->
<NoWarn>$(NoWarn);RS1024;RS2007</NoWarn>
<NoWarn>$(NoWarn);RS2007</NoWarn>
<!-- Suppress false positives due to a bug in v3.8.0. -->
<NoWarn Condition="'$(MccVersion)' == '3.8'">$(NoWarn);RS2002</NoWarn>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<!-- We multi-target for different compiler versions. https://github.com/dotnet/roslyn/discussions/81256#discussioncomment-14975130, https://github.com/dotnet/roslyn/blob/main/docs/wiki/NuGet-packages.md#versioning -->
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,7 @@ Either add the [ArgumentsSource] or [Arguments] attribute(s) or remove the param
<data name="Attributes_ArgumentsAttribute_RequiresParameters_MessageFormat" xml:space="preserve">
<value>Method {0} has no parameters</value>
</data>
<data name="Attributes_ArgumentsAttribute_RequiresParameters_Description" xml:space="preserve">
<value>The values passed to an [Arguments] must have parameter(s)</value>
</data>
</root>
4 changes: 2 additions & 2 deletions src/BenchmarkDotNet.Analyzers/BenchmarkRunner/RunAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private static void Analyze(SyntaxNodeAnalysisContext context)
var classMemberAccessTypeSymbol = context.SemanticModel.GetTypeInfo(identifierNameSyntax).Type;
if (classMemberAccessTypeSymbol is null
|| classMemberAccessTypeSymbol.TypeKind == TypeKind.Error
|| !classMemberAccessTypeSymbol.Equals(benchmarkRunnerTypeSymbol))
|| !SymbolEqualityComparer.Default.Equals(classMemberAccessTypeSymbol, benchmarkRunnerTypeSymbol))
{
return;
}
Expand Down Expand Up @@ -202,7 +202,7 @@ bool HasBenchmarkAttribute()
{
if (attributeData.AttributeClass != null)
{
if (attributeData.AttributeClass.Equals(benchmarkAttributeTypeSymbol))
if (SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, benchmarkAttributeTypeSymbol))
{
return true;
}
Expand Down
Loading