diff --git a/src/nunit.analyzers.tests/DiagnosticSuppressors/DereferencePossiblyNullReferenceSuppressorTests.cs b/src/nunit.analyzers.tests/DiagnosticSuppressors/DereferencePossiblyNullReferenceSuppressorTests.cs index 92951a2c..a894a85b 100644 --- a/src/nunit.analyzers.tests/DiagnosticSuppressors/DereferencePossiblyNullReferenceSuppressorTests.cs +++ b/src/nunit.analyzers.tests/DiagnosticSuppressors/DereferencePossiblyNullReferenceSuppressorTests.cs @@ -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); + } } } diff --git a/src/nunit.analyzers/DiagnosticSuppressors/DereferencePossiblyNullReferenceSuppressor.cs b/src/nunit.analyzers/DiagnosticSuppressors/DereferencePossiblyNullReferenceSuppressor.cs index b0b94fa6..91017998 100644 --- a/src/nunit.analyzers/DiagnosticSuppressors/DereferencePossiblyNullReferenceSuppressor.cs +++ b/src/nunit.analyzers/DiagnosticSuppressors/DereferencePossiblyNullReferenceSuppressor.cs @@ -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;