From 8a5cfc2666276de3f19c68046c9e4a6243fe50e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rald=20Barr=C3=A9?= Date: Thu, 17 Oct 2024 20:25:11 -0400 Subject: [PATCH] fix --- .../Suppressors/IDE0058Suppressor.cs | 13 ++++++++++-- .../CA1822DecoratedMethodSuppressorTests.cs | 4 +++- .../Suppressors/IDE0058SuppressorTests.cs | 20 +++++++++++++++---- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/Meziantou.Analyzer/Suppressors/IDE0058Suppressor.cs b/src/Meziantou.Analyzer/Suppressors/IDE0058Suppressor.cs index 43c7a7202..7a84efdfc 100644 --- a/src/Meziantou.Analyzer/Suppressors/IDE0058Suppressor.cs +++ b/src/Meziantou.Analyzer/Suppressors/IDE0058Suppressor.cs @@ -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) { @@ -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)); } diff --git a/tests/Meziantou.Analyzer.Test/Suppressors/CA1822DecoratedMethodSuppressorTests.cs b/tests/Meziantou.Analyzer.Test/Suppressors/CA1822DecoratedMethodSuppressorTests.cs index 86e8691b1..41a163052 100644 --- a/tests/Meziantou.Analyzer.Test/Suppressors/CA1822DecoratedMethodSuppressorTests.cs +++ b/tests/Meziantou.Analyzer.Test/Suppressors/CA1822DecoratedMethodSuppressorTests.cs @@ -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; @@ -65,3 +66,4 @@ internal sealed class Sample .ValidateAsync(); } } +#endif \ No newline at end of file diff --git a/tests/Meziantou.Analyzer.Test/Suppressors/IDE0058SuppressorTests.cs b/tests/Meziantou.Analyzer.Test/Suppressors/IDE0058SuppressorTests.cs index 4a034be0d..aebe8ffe8 100644 --- a/tests/Meziantou.Analyzer.Test/Suppressors/IDE0058SuppressorTests.cs +++ b/tests/Meziantou.Analyzer.Test/Suppressors/IDE0058SuppressorTests.cs @@ -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; @@ -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(); @@ -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 \ No newline at end of file