-
Notifications
You must be signed in to change notification settings - Fork 222
/
UnusedStringBuilder.cs
53 lines (43 loc) · 2.72 KB
/
UnusedStringBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* SonarAnalyzer for .NET
* Copyright (C) 2015-2023 SonarSource SA
* mailto: contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
using System.Text;
namespace SonarAnalyzer.Rules.VisualBasic;
[DiagnosticAnalyzer(LanguageNames.VisualBasic)]
public sealed class UnusedStringBuilder : UnusedStringBuilderBase<SyntaxKind, VariableDeclaratorSyntax, InvocationExpressionSyntax, ReturnStatementSyntax>
{
protected override ILanguageFacade<SyntaxKind> Language => VisualBasicFacade.Instance;
protected override string GetVariableName(VariableDeclaratorSyntax declaration) => declaration.Names.FirstOrDefault().ToString();
protected override bool NeedsToTrack(VariableDeclaratorSyntax expression, SemanticModel semanticModel) =>
expression.Initializer is not null
&& expression.Initializer.Value is ObjectCreationExpressionSyntax objectCreation
&& objectCreation.Type.ToString().Equals(nameof(StringBuilder));
protected override SyntaxNode GetAncestorBlock(VariableDeclaratorSyntax declaration) =>
declaration.Ancestors().OfType<MethodBlockBaseSyntax>().First();
protected override bool IsIsStringInvoked(string variableName, IList<InvocationExpressionSyntax> invocations) =>
invocations.Any(x => x.Expression is MemberAccessExpressionSyntax { } member
&& IsSameVariable(member.Expression, variableName)
&& member.NameIs(nameof(ToString)));
protected override bool IsPassedToMethod(string variableName, IList<InvocationExpressionSyntax> invocations) =>
invocations.Any(x => x.ArgumentList.Arguments.Any(y => IsSameVariable(y.GetExpression(), variableName)));
protected override bool IsReturned(string variableName, IList<ReturnStatementSyntax> returnStatements) =>
returnStatements.Any(x => IsSameVariable(x.Expression, variableName));
private static bool IsSameVariable(ExpressionSyntax expression, string variableName) =>
expression.DescendantNodesAndSelf().OfType<IdentifierNameSyntax>().Any(p => p.NameIs(variableName));
}