diff --git a/src/nunit.analyzers.tests/DiagnosticSuppressors/DereferencePossiblyNullReferenceSuppressorTests.cs b/src/nunit.analyzers.tests/DiagnosticSuppressors/DereferencePossiblyNullReferenceSuppressorTests.cs index a894a85b..7fe64b2a 100644 --- a/src/nunit.analyzers.tests/DiagnosticSuppressors/DereferencePossiblyNullReferenceSuppressorTests.cs +++ b/src/nunit.analyzers.tests/DiagnosticSuppressors/DereferencePossiblyNullReferenceSuppressorTests.cs @@ -1032,5 +1032,27 @@ public HasString(string? inner) ExpectedDiagnostic.Create(DereferencePossiblyNullReferenceSuppressor.SuppressionDescriptors["CS8603"]), testCode); } + + [Test] + public void TestIssue739FullyQualified() + { + var testCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + [Test] + public void T() + { + object? x = null; + NUnit.Framework.Assert.That(x, Is.Not.Null); + M(x); + } + + void M(object o) + { + } + "); + + RoslynAssert.Suppressed(suppressor, + ExpectedDiagnostic.Create(DereferencePossiblyNullReferenceSuppressor.SuppressionDescriptors["CS8604"]), + testCode); + } } } diff --git a/src/nunit.analyzers/Helpers/AssertHelper.cs b/src/nunit.analyzers/Helpers/AssertHelper.cs index 6089cde8..f5387876 100644 --- a/src/nunit.analyzers/Helpers/AssertHelper.cs +++ b/src/nunit.analyzers/Helpers/AssertHelper.cs @@ -144,20 +144,33 @@ private static bool IsAssert(ExpressionSyntax? expression, [NotNullWhen(true)] out ArgumentListSyntax? argumentList) { if (expression is InvocationExpressionSyntax invocationExpression && - invocationExpression.Expression is MemberAccessExpressionSyntax memberAccessExpression && - memberAccessExpression.Expression is IdentifierNameSyntax identifierName && - isNameOfAssert(identifierName.Identifier.Text)) + invocationExpression.Expression is MemberAccessExpressionSyntax memberAccessExpression) { - member = memberAccessExpression.Name.Identifier.Text; - argumentList = invocationExpression.ArgumentList; - return true; - } - else - { - member = string.Empty; - argumentList = null; - return false; + SimpleNameSyntax? className = GetClassName(memberAccessExpression); + + if (className is not null && + isNameOfAssert(className.Identifier.Text)) + { + member = memberAccessExpression.Name.Identifier.Text; + argumentList = invocationExpression.ArgumentList; + return true; + } } + + member = string.Empty; + argumentList = null; + return false; + } + + private static SimpleNameSyntax? GetClassName(MemberAccessExpressionSyntax memberAccessExpression) + { + if (memberAccessExpression.Expression is MemberAccessExpressionSyntax nestedMemberAccessExpression) + return nestedMemberAccessExpression.Name; + + if (memberAccessExpression.Expression is IdentifierNameSyntax identifierName) + return identifierName; + + return null; } } }