Skip to content

Commit

Permalink
The explanation should not be null or whitespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel Nobel committed Mar 17, 2023
1 parent 4db4ba1 commit 88d64e7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ namespace SonarAnalyzer.Rules.CSharp;
public sealed class ObsoleteAttributes : ObsoleteAttributesBase<SyntaxKind>
{
protected override ILanguageFacade<SyntaxKind> Language => CSharpFacade.Instance;

protected override SyntaxNode GetExplanationExpression(SyntaxNode node) =>
node is AttributeSyntax { ArgumentList.Arguments: { Count: >= 1 } arguments }
&& arguments[0] is { Expression: { } expression }
? expression
: null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public abstract class ObsoleteAttributesBase<TSyntaxKind> : SonarDiagnosticAnaly

protected abstract ILanguageFacade<TSyntaxKind> Language { get; }

protected abstract SyntaxNode GetExplanationExpression(SyntaxNode node);

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; }

internal DiagnosticDescriptor ExplanationNeededRule { get; }
Expand All @@ -54,11 +56,15 @@ protected sealed override void Initialize(SonarAnalysisContext context) =>
var location = c.Node.GetLocation();
c.ReportIssue(Diagnostic.Create(RemoveRule, location));

if (!attribute.GetParameters().Any())
if (NoExplanation(c.Node, c.SemanticModel))
{
c.ReportIssue(Diagnostic.Create(ExplanationNeededRule, location));
}
}
},
Language.SyntaxKind.Attribute);

private bool NoExplanation(SyntaxNode node, SemanticModel model) =>
GetExplanationExpression(node) is not { } justification
|| string.IsNullOrWhiteSpace(Language.FindConstantValue(model, justification) as string);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ namespace SonarAnalyzer.Rules.VisualBasic;
public sealed class ObsoleteAttributes : ObsoleteAttributesBase<SyntaxKind>
{
protected override ILanguageFacade<SyntaxKind> Language => VisualBasicFacade.Instance;

protected override SyntaxNode GetExplanationExpression(SyntaxNode node) =>
node is AttributeSyntax { ArgumentList.Arguments: { Count: >= 1 } arguments }
&& arguments[0] is SimpleArgumentSyntax { Expression: { } } argument
? argument.Expression
: null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ void FullyDeclaredNamespace() { }
[global::System.Obsolete] // Noncompliant
void GloballyDeclaredNamespace() { }

[Obsolete(null)] // Noncompliant
void WithNull() { }

[Obsolete("")] // Noncompliant
void WithEmptyString() { }

[Obsolete(" ")] // Noncompliant
void WithWhiteSpace() { }

[Obsolete] // Noncompliant
[CLSCompliant(false)]
uint Multiple() { return 0; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ Class Noncompliant
Private Sub GloballyDeclaredNamespace()
End Sub

<Obsolete(Nothing)> ' Noncompliant
Sub WithNothing() { }
End Sub

<Obsolete("")> ' Noncompliant
Sub WithEmptyString() { }
End Sub

<Obsolete(" ")> ' Noncompliant
Sub WithWhiteSpace() { }
End Sub

<Obsolete> ' Noncompliant
<CLSCompliant(False)>
Private Function Multiple() As UInteger
Expand Down

0 comments on commit 88d64e7

Please sign in to comment.