diff --git a/src/Meziantou.Analyzer/Internals/OperationExtensions.cs b/src/Meziantou.Analyzer/Internals/OperationExtensions.cs index e0718764..6dec93da 100644 --- a/src/Meziantou.Analyzer/Internals/OperationExtensions.cs +++ b/src/Meziantou.Analyzer/Internals/OperationExtensions.cs @@ -271,8 +271,9 @@ static bool IsValid(Location location, int operationLocation, int? staticContext continue; } - yield return symbol; } } + + public static bool IsConstantZero(this IOperation operation) => operation is { ConstantValue: { HasValue: true, Value: 0 or 0L or 0u or 0uL or 0f or 0d or 0m } }; } diff --git a/src/Meziantou.Analyzer/Suppressors/IDE0058Suppressor.cs b/src/Meziantou.Analyzer/Suppressors/IDE0058Suppressor.cs index 7a84efdf..2fec37d7 100644 --- a/src/Meziantou.Analyzer/Suppressors/IDE0058Suppressor.cs +++ b/src/Meziantou.Analyzer/Suppressors/IDE0058Suppressor.cs @@ -21,6 +21,8 @@ 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"); + var System_IO_Stream = context.Compilation.GetBestTypeByMetadataName("System.IO.Stream"); + var System_Collections_Generic_HashSet = context.Compilation.GetBestTypeByMetadataName("System.Collections.Generic.HashSet`1"); #pragma warning restore IDE1006 foreach (var diagnostic in context.ReportedDiagnostics) @@ -48,6 +50,18 @@ public override void ReportSuppressions(SuppressionAnalysisContext context) { context.ReportSuppression(Suppression.Create(Descriptor, diagnostic)); } + + // Stream.Seek + if (invocation.TargetMethod.Name is "Seek" && invocation.TargetMethod.ContainingType.IsEqualTo(System_IO_Stream) && invocation.Arguments.Length is 2 && invocation.Arguments[0].Value.IsConstantZero() && invocation.Arguments[1].Value.IsConstantZero()) + { + context.ReportSuppression(Suppression.Create(Descriptor, diagnostic)); + } + + // HashSet.Add + if (invocation.TargetMethod.Name is "Add" && invocation.TargetMethod.ContainingType.ConstructedFrom.IsEqualTo(System_Collections_Generic_HashSet)) + { + context.ReportSuppression(Suppression.Create(Descriptor, diagnostic)); + } } } } diff --git a/tests/Meziantou.Analyzer.Test/Suppressors/IDE0058SuppressorTests.cs b/tests/Meziantou.Analyzer.Test/Suppressors/IDE0058SuppressorTests.cs index aebe8ffe..c6391c86 100644 --- a/tests/Meziantou.Analyzer.Test/Suppressors/IDE0058SuppressorTests.cs +++ b/tests/Meziantou.Analyzer.Test/Suppressors/IDE0058SuppressorTests.cs @@ -52,5 +52,44 @@ static void A() } """) .ValidateAsync(); + + [Fact] + public async Task System_IO_Stream_Seek_0_End() + => await CreateProjectBuilder() + .WithSourceCode(""" + using System.IO; + static void A() + { + Stream stream = null; + [||]stream.Seek(0, SeekOrigin.End); + } + """) + .ValidateAsync(); + + [Fact] + public async Task System_IO_Stream_Seek_0_Begin() + => await CreateProjectBuilder() + .WithSourceCode(""" + using System.IO; + static void A() + { + Stream stream = null; + stream.Seek(0, SeekOrigin.Begin); + } + """) + .ValidateAsync(); + + [Fact] + public async Task System_Collections_Generic_HashSet_Add() + => await CreateProjectBuilder() + .WithSourceCode(""" + using System.IO; + static void A() + { + System.Collections.Generic.HashSet a = null; + a.Add(0); + } + """) + .ValidateAsync(); } #endif \ No newline at end of file