-
-
Notifications
You must be signed in to change notification settings - Fork 51
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 and Directory (#762)
- Loading branch information
Showing
10 changed files
with
130 additions
and
19 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,54 @@ | ||
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) | ||
{ | ||
#pragma warning disable IDE1006 // Naming Styles | ||
var System_Text_StringBuilder = context.Compilation.GetBestTypeByMetadataName("System.Text.StringBuilder"); | ||
var System_IO_Directory = context.Compilation.GetBestTypeByMetadataName("System.IO.Directory"); | ||
#pragma warning restore IDE1006 | ||
|
||
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(System_Text_StringBuilder)) | ||
{ | ||
context.ReportSuppression(Suppression.Create(Descriptor, diagnostic)); | ||
} | ||
|
||
// Directory.CreateDirectory | ||
if (invocation.TargetMethod.Name is "CreateDirectory" && invocation.TargetMethod.ContainingType.IsEqualTo(System_IO_Directory)) | ||
{ | ||
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
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
56 changes: 56 additions & 0 deletions
56
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,56 @@ | ||
#if ROSLYN_4_10_OR_GREATER | ||
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() | ||
{ | ||
[|new System.Text.StringBuilder().Append("Hello")|]; | ||
[|System.IO.Directory.CreateDirectory("dir")|]; | ||
} | ||
""") | ||
.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(); | ||
|
||
[Fact] | ||
public async Task Directory_CreateDirectory() | ||
=> await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
static void A() | ||
{ | ||
System.IO.Directory.CreateDirectory("dir"); | ||
} | ||
""") | ||
.ValidateAsync(); | ||
} | ||
#endif |