Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore null suppression operator when matching Is.Not.Null #704

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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