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

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

Open
Youssef1313 opened this issue Jan 3, 2025 · 0 comments · May be fixed by #4502

Comments

@Youssef1313
Copy link
Member

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),
@Youssef1313 Youssef1313 self-assigned this Jan 3, 2025
@Youssef1313 Youssef1313 linked a pull request Jan 3, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant