Skip to content

Commit

Permalink
Ignore null suppression operator when matching Is.Not.Null
Browse files Browse the repository at this point in the history
  • Loading branch information
manfred-brands committed Mar 12, 2024
1 parent 4798485 commit 0aacb17
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -997,5 +997,32 @@ public Extra(string info, int value)
ExpectedDiagnostic.Create(DereferencePossiblyNullReferenceSuppressor.SuppressionDescriptors["CS8602"]),
testCode);
}

[Test]
public void TestNullSuppressionOperator()
{
var testCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
[TestCase(default(string))]
public void Test(string ?possibleNullString)
{
HasString str = new(possibleNullString);
string nonNullString = GetStringSuppress(str);
Assert.That(nonNullString, Is.Not.Null);
}
private static string GetStringSuppress(HasString? str) // argument is nullable
{
Assert.That(str!.Inner, Is.Not.Null);
return str.Inner; // warning: possible null reference return
}
private record HasString(string? Inner);
");

RoslynAssert.Suppressed(suppressor,
ExpectedDiagnostic.Create(DereferencePossiblyNullReferenceSuppressor.SuppressionDescriptors["CS8603"]),
testCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,12 @@ private static bool InvalidatedBy(string assignment, string possibleNullReferenc

private static bool CoveredBy(string assertedNotNull, string possibleNullReference)
{
int exclamation = assertedNotNull.IndexOf('!');
if (exclamation >= 0)
{
assertedNotNull = assertedNotNull.Replace("!", string.Empty);
}

if (possibleNullReference == assertedNotNull)
{
return true;
Expand Down

0 comments on commit 0aacb17

Please sign in to comment.