Skip to content

Commit

Permalink
Fix for generic EqualTo<T> which no longer has an implicit cast.
Browse files Browse the repository at this point in the history
  • Loading branch information
manfred-brands committed Dec 22, 2024
1 parent 6e77c26 commit 979589e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -980,5 +980,36 @@ public void AnalyzeWhenComparingStringWithUri()

RoslynAssert.Diagnostics(analyzer, testCode);
}

[Test]
public void NoDiagnosticWhenComparingInstanceWithValueCastToInstanceType()
{
var testCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings("""
[Test]
public void TestMethod()
{
MyLong l0 = new(0);
Assert.That(l0, Is.EqualTo((MyLong)0));
}
private readonly struct MyLong : IEquatable<MyLong>
{
private readonly long _value;
public MyLong(long value) => _value = value;
public bool Equals(MyLong other) => _value == other._value;
public override bool Equals(object? obj) => obj is MyLong other && Equals(other);
public override int GetHashCode() => _value.GetHashCode();
public static implicit operator MyLong(long value) => new(value);
public static implicit operator long(MyLong value) => value._value;
}
""");

RoslynAssert.Valid(analyzer, testCode);
}
}
}
2 changes: 1 addition & 1 deletion src/nunit.analyzers/Operations/ConstraintExpressionPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public string[] GetSuffixesNames()
{
var argument = (this.Root as IInvocationOperation)?.Arguments.FirstOrDefault()?.Value;

if (argument is IConversionOperation conversion)
if (argument is IConversionOperation conversion && conversion.IsImplicit)
argument = conversion.Operand;

return argument;
Expand Down

0 comments on commit 979589e

Please sign in to comment.