Skip to content

Commit

Permalink
Fixed VB test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
CristianAmbrosini committed Feb 2, 2023
1 parent 98dc16c commit b9f2741
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,14 @@ public abstract class UnusedStringBuilderBase<TSyntaxKind, TVariableDeclarator,
private const string DiagnosticId = "S3063";
protected override string MessageFormat => """Remove this "StringBuilder"; ".ToString()" is never called.""";

private readonly DiagnosticDescriptor rule;

protected abstract string GetVariableName(TVariableDeclarator declaration);
protected abstract bool NeedsToTrack(TVariableDeclarator declaration, SemanticModel semanticModel);
protected abstract SyntaxNode GetAncestorBlock(TVariableDeclarator declaration);
protected abstract bool IsIsStringInvoked(string variableName, IList<TInvocationExpression> invocations, SemanticModel semanticModel);
protected abstract bool IsPassedToMethod(string variableName, IList<TInvocationExpression> invocations);
protected abstract bool IsReturned(string variableName, IList<TReturnStatement> returnStatements);

protected UnusedStringBuilderBase() : base(DiagnosticId) =>
rule = Language.CreateDescriptor(DiagnosticId, MessageFormat);
protected UnusedStringBuilderBase() : base(DiagnosticId) { }

protected sealed override void Initialize(SonarAnalysisContext context) =>
context.RegisterNodeAction(Language.GeneratedCodeRecognizer, c =>
Expand All @@ -56,13 +53,12 @@ public abstract class UnusedStringBuilderBase<TSyntaxKind, TVariableDeclarator,
return;
}
var invocations = block.DescendantNodes().OfType<TInvocationExpression>().ToList();
if (IsIsStringInvoked(variableName, invocations, c.SemanticModel)
|| IsPassedToMethod(variableName, invocations)
|| IsReturned(variableName, block.DescendantNodes().OfType<TReturnStatement>().ToList()))
{
return;
}
c.ReportIssue(Diagnostic.Create(rule, variableDeclaration.GetLocation()));
c.ReportIssue(Diagnostic.Create(Rule, variableDeclaration.GetLocation()));
}, Language.SyntaxKind.VariableDeclarator);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed class UnusedStringBuilder : UnusedStringBuilderBase<SyntaxKind, Va
protected override ILanguageFacade<SyntaxKind> Language => VisualBasicFacade.Instance;

protected override string GetVariableName(VariableDeclaratorSyntax declaration) =>
declaration.GetName();
declaration.Names.FirstOrDefault().ToString();

protected override bool NeedsToTrack(VariableDeclaratorSyntax expression, SemanticModel semanticModel) =>
expression.Initializer is not null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ Public Class Program
builderCalls.Append("Append")
builderCalls.AppendLine("AppendLine")
builderCalls.Replace("a", "b")
builderCalls.Remove(builderCalls.Length - 1, 1)
builderCalls.Insert(builderCalls.Length, "a")
builderCalls.Clear()

Dim builderCalls2 As StringBuilder = New StringBuilder() ' Compliant
builderCalls2.Remove(builderCalls2.Length - 1, 1)

Dim builderReturn = New StringBuilder() ' Compliant
Return builderReturn
End Function
Expand Down

0 comments on commit b9f2741

Please sign in to comment.