Skip to content

MSTEST0026 (Avoid conditional access in assertions) false positive for conditional access in message/messageArgs #4501

@Youssef1313

Description

@Youssef1313

The following analyzer unit test should pass

    [TestMethod]
    public async Task WhenUsingConditionalsAccess_In_Message_NoDiagnostic()
    {
        string code = """
            #nullable enable
            using System.Text.RegularExpressions;
            using Microsoft.VisualStudio.TestTools.UnitTesting;
            using System.Collections.Generic;

            public class A
            {
                public string? S { get; set; }
                public Regex? R { get; set; }
            }

            [TestClass]
            public class MyTestClass
            {
                [TestMethod]
                public void Compliant()
                {
                    Assert.AreEqual(new object(), new object(), new A().S?.Length.ToString());
                }
            }
            """;

        await VerifyCS.VerifyAnalyzerAsync(code);
    }

But it fails:

    Test method MSTest.Analyzers.UnitTests.AssertionArgsShouldAvoidConditionalAccessAnalyzerTests.WhenUsingConditionalsAccess_In_Message_NoDiagnostic threw exception: 
System.InvalidOperationException: Context: Diagnostics of test state
Mismatch between number of diagnostics returned, expected "0" actual "1"

Diagnostics:
// /0/Test0.cs(18,9): info MSTEST0026: Prefer adding an additional assertion that checks for null
VerifyCS.Diagnostic().WithSpan(18, 9, 18, 82),

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions