Skip to content

Commit

Permalink
Disable more IDE0058
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Oct 20, 2024
1 parent 61fa4a3 commit 3b050d1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Meziantou.Analyzer/Internals/OperationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 } };
}
14 changes: 14 additions & 0 deletions src/Meziantou.Analyzer/Suppressors/IDE0058Suppressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<T>.Add
if (invocation.TargetMethod.Name is "Add" && invocation.TargetMethod.ContainingType.ConstructedFrom.IsEqualTo(System_Collections_Generic_HashSet))
{
context.ReportSuppression(Suppression.Create(Descriptor, diagnostic));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> a = null;
a.Add(0);
}
""")
.ValidateAsync();
}
#endif

0 comments on commit 3b050d1

Please sign in to comment.