Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Oct 18, 2024
1 parent 623fc92 commit 8a5cfc2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
13 changes: 11 additions & 2 deletions src/Meziantou.Analyzer/Suppressors/IDE0058Suppressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ public sealed class IDE0058Suppressor : DiagnosticSuppressor

public override void ReportSuppressions(SuppressionAnalysisContext context)
{
var stringBuilderSymbol = context.Compilation.GetBestTypeByMetadataName("System.Text.StringBuilder");
#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)
{
Expand All @@ -35,7 +38,13 @@ public override void ReportSuppressions(SuppressionAnalysisContext context)
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))
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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
#if ROSLYN_4_10_OR_GREATER
using System.Threading.Tasks;
using Meziantou.Analyzer.Suppressors;
using TestHelper;
using Xunit;
Expand Down Expand Up @@ -65,3 +66,4 @@ internal sealed class Sample
.ValidateAsync();
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
#if ROSLYN_4_10_OR_GREATER
using System.Threading.Tasks;
using Meziantou.Analyzer.Suppressors;
using Microsoft.CodeAnalysis;
using TestHelper;
Expand All @@ -22,9 +23,8 @@ public async Task IDE0058IsReported()
.WithSourceCode("""
static void A()
{
var sb = new System.Text.StringBuilder();
[|sb.Append("Hello")|];
System.Console.WriteLine(sb.ToString());
[|new System.Text.StringBuilder().Append("Hello")|];
[|System.IO.Directory.CreateDirectory("dir")|];
}
""")
.ValidateAsync();
Expand All @@ -41,4 +41,16 @@ static void A()
}
""")
.ValidateAsync();

[Fact]
public async Task Directory_CreateDirectory()
=> await CreateProjectBuilder()
.WithSourceCode("""
static void A()
{
System.IO.Directory.CreateDirectory("dir");
}
""")
.ValidateAsync();
}
#endif

0 comments on commit 8a5cfc2

Please sign in to comment.