diff --git a/analyzers/src/SonarAnalyzer.Common/Rules/ShouldImplementExportedInterfacesBase.cs b/analyzers/src/SonarAnalyzer.Common/Rules/ShouldImplementExportedInterfacesBase.cs index 8d0393ddba3..7a4c4b02153 100644 --- a/analyzers/src/SonarAnalyzer.Common/Rules/ShouldImplementExportedInterfacesBase.cs +++ b/analyzers/src/SonarAnalyzer.Common/Rules/ShouldImplementExportedInterfacesBase.cs @@ -56,8 +56,7 @@ 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; @@ -65,21 +64,27 @@ protected override void Initialize(SonarAnalysisContext context) => 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)); @@ -102,9 +107,10 @@ private INamedTypeSymbol GetExportedTypeSymbol(SeparatedSyntaxList @@ -131,6 +137,6 @@ private TArgumentSyntax GetArgumentFromDoubleArgumentAttribute(SeparatedSyntaxLi } private static TArgumentSyntax GetArgumentFromSingleArgumentAttribute(SeparatedSyntaxList 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 } }