Skip to content

Commit

Permalink
Cleanup around ShouldImplementExportedInterfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
costin-zaharia-sonarsource committed May 24, 2022
1 parent 159ddd4 commit abc493c
Showing 1 changed file with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,35 @@ protected override void Initialize(SonarAnalysisContext context) =>
c =>
{
var attributeSyntax = (TAttributeSyntax)c.Node;

if (!(c.SemanticModel.GetSymbolInfo(GetAttributeName(attributeSyntax)).Symbol is IMethodSymbol attributeCtorSymbol)
if (c.SemanticModel.GetSymbolInfo(GetAttributeName(attributeSyntax)).Symbol is not IMethodSymbol attributeCtorSymbol
|| !attributeCtorSymbol.ContainingType.IsAny(exportAttributes))
{
return;
}

var exportedType = GetExportedTypeSymbol(GetAttributeArguments(attributeSyntax), c.SemanticModel);
var attributeTargetType = GetAttributeTargetSymbol(attributeSyntax, c.SemanticModel);

if (exportedType != null && attributeTargetType != null && !IsOfExportType(attributeTargetType, exportedType))
if (exportedType is null
|| attributeTargetType is null
|| IsOfExportType(attributeTargetType, exportedType))
{
var action = exportedType.IsInterface()
? ActionForInterface
: ActionForClass;
return;
}

c.ReportIssue(Diagnostic.Create(Rule, attributeSyntax.GetLocation(), action,
var action = exportedType.IsInterface()
? ActionForInterface
: ActionForClass;

c.ReportIssue(
Diagnostic.Create(Rule,
attributeSyntax.GetLocation(),
action,
exportedType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat),
attributeTargetType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)));
}
},
Language.SyntaxKind.Attribute);

private static bool IsOfExportType(ITypeSymbol type, INamedTypeSymbol exportedType) =>
private static bool IsOfExportType(ITypeSymbol type, ISymbol exportedType) =>
type.GetSelfAndBaseTypes()
.Union(type.AllInterfaces)
.Any(currentType => currentType.Equals(exportedType));
Expand All @@ -102,9 +107,10 @@ private INamedTypeSymbol GetExportedTypeSymbol(SeparatedSyntaxList<TArgumentSynt
?? GetArgumentFromDoubleArgumentAttribute(arguments, semanticModel);

var typeOfOrGetTypeExpression = Language.Syntax.NodeExpression(argumentSyntax);

var exportedTypeSyntax = GetTypeOfOrGetTypeExpression(typeOfOrGetTypeExpression);
return exportedTypeSyntax == null ? null : semanticModel.GetSymbolInfo(exportedTypeSyntax).Symbol as INamedTypeSymbol;
return exportedTypeSyntax == null
? null
: semanticModel.GetSymbolInfo(exportedTypeSyntax).Symbol as INamedTypeSymbol;
}

private ITypeSymbol GetAttributeTargetSymbol(SyntaxNode syntaxNode, SemanticModel semanticModel) =>
Expand All @@ -131,6 +137,6 @@ private TArgumentSyntax GetArgumentFromDoubleArgumentAttribute(SeparatedSyntaxLi
}

private static TArgumentSyntax GetArgumentFromSingleArgumentAttribute(SeparatedSyntaxList<TArgumentSyntax> arguments) =>
arguments.Count != 1 ? null : arguments[0]; // Only one argument, should be typeof expression
arguments.Count == 1 ? arguments[0] : null; // Only one argument, should be typeof expression
}
}

0 comments on commit abc493c

Please sign in to comment.