-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add suppressor for IDE0058 on StringBuilder
- Loading branch information
Showing
9 changed files
with
106 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Operations; | ||
|
||
namespace Meziantou.Analyzer.Suppressors; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class IDE0058Suppressor : DiagnosticSuppressor | ||
{ | ||
private static readonly SuppressionDescriptor Descriptor = new( | ||
id: "MAS0003", | ||
suppressedDiagnosticId: "IDE0058", | ||
justification: "Suppress IDE0058 on well-known types" | ||
); | ||
|
||
public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions => ImmutableArray.Create(Descriptor); | ||
|
||
public override void ReportSuppressions(SuppressionAnalysisContext context) | ||
{ | ||
var stringBuilderSymbol = context.Compilation.GetBestTypeByMetadataName("System.Text.StringBuilder"); | ||
|
||
foreach (var diagnostic in context.ReportedDiagnostics) | ||
{ | ||
var tree = diagnostic.Location.SourceTree; | ||
if (tree is null) | ||
continue; | ||
|
||
var semanticModel = context.GetSemanticModel(tree); | ||
if (semanticModel is null) | ||
continue; | ||
|
||
var node = tree.GetRoot(context.CancellationToken).FindNode(diagnostic.Location.SourceSpan); | ||
var operation = semanticModel.GetOperation(node, context.CancellationToken); | ||
if (operation is IInvocationOperation invocation) | ||
{ | ||
// StringBuilder | ||
if (invocation.TargetMethod.Name is "Append" or "AppendLine" or "AppendJoin" or "AppendFormat" or "Clear" or "Remove" or "Insert" or "Replace" && invocation.TargetMethod.ContainingType.IsEqualTo(stringBuilderSymbol)) | ||
{ | ||
context.ReportSuppression(Suppression.Create(Descriptor, diagnostic)); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
tests/Meziantou.Analyzer.Test/Suppressors/IDE0058SuppressorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System.Threading.Tasks; | ||
using Meziantou.Analyzer.Suppressors; | ||
using Microsoft.CodeAnalysis; | ||
using TestHelper; | ||
using Xunit; | ||
|
||
namespace Meziantou.Analyzer.Test.Suppressors; | ||
public sealed class IDE0058SuppressorTests | ||
{ | ||
private static ProjectBuilder CreateProjectBuilder() | ||
=> new ProjectBuilder() | ||
.WithMicrosoftCodeAnalysisCSharpCodeStyleAnalyzers("IDE0058") | ||
.WithAnalyzer<IDE0058Suppressor>() | ||
.WithOutputKind(OutputKind.ConsoleApplication); | ||
|
||
// Ensure the diagnostic is reported without the suppressor | ||
[Fact] | ||
public async Task IDE0058IsReported() | ||
=> await new ProjectBuilder() | ||
.WithMicrosoftCodeAnalysisCSharpCodeStyleAnalyzers("IDE0058") | ||
.WithOutputKind(OutputKind.ConsoleApplication) | ||
.WithSourceCode(""" | ||
static void A() | ||
{ | ||
var sb = new System.Text.StringBuilder(); | ||
[|sb.Append("Hello")|]; | ||
System.Console.WriteLine(sb.ToString()); | ||
} | ||
""") | ||
.ValidateAsync(); | ||
|
||
[Fact] | ||
public async Task StringBuilder_Append() | ||
=> await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
static void A() | ||
{ | ||
var sb = new System.Text.StringBuilder(); | ||
sb.Append("Hello"); | ||
System.Console.WriteLine(sb.ToString()); | ||
} | ||
""") | ||
.ValidateAsync(); | ||
} |