S1125 false positive on C# 9 target typed conditional expression

bool B { get; set; }

void M()
{
    var v = B ? true : null; // S1125 on this line
}

In previous versions of C#, B ? true : null would not have compiled and required an explicit cast like: B ? (bool?)true : null. This rule does not trigger if you include the explicit cast, but now in C# 9 this cast is no longer necessary.

This issue is present in the SonarAnalyzer.CSharp nuget package version 8.30.0.37606

Hello @dmilligan

Welcome to our community!

With your snippet I cannot reproduce the issue as the snippet does not compile. However I found a false positive and opened an issue for the following case:

bool B { get; set; }

void M()
{
    bool? v = B ? true : null; // S1125 on this line
}

Did you mean to report that case?

All the best,
Čaba

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.