Skip to content

Get rid of MSTEST0026 code suggestion (Avoid conditional access in assertions) #4706

Open
@Evangelink

Description

Part of the point of adding conditional access operators is to help make the code more readable through brevity. But suggestion MSTEST0026 encourages code bloat by adding unnecessary Assert.IsNotNull() statements. Assert.AreEqual already perfectly handles null values and using a conditional access operator here can combine a !null assertion and an equality assertion together in a readable, compact way.

Here is the example from the MSTEST0026 docs:

Assert.AreEqual("Contoso", company?.Name); // MSTEST0026

// Fixed code
Assert.IsNotNull(company);
Assert.AreEqual("Contoso", company.Name);

But the original statement is both technically correct and readable and the ‘fixed’ code is overly verbose.

Originally reported in https://developercommunity.visualstudio.com/t/Get-rid-of-MSTEST0026-code-suggestion-A/10819575

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions