Skip to content

Commit

Permalink
Merge pull request #704 from manfred-brands/issues/679_NullSuppressio…
Browse files Browse the repository at this point in the history
…nOperator

Ignore null suppression operator when matching Is.Not.Null
  • Loading branch information
mikkelbu authored Mar 16, 2024
2 parents 4798485 + 1703848 commit f771316
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -997,5 +997,40 @@ 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 sealed class HasString
{
public HasString(string? inner)
{
Inner = inner;
}
public string? Inner { get; }
}
");

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 f771316

Please sign in to comment.