False positive with C# switch expression patterns?

I have the following code which represents a KeyDown event handler for a form:

private void OnKeyDown(object sender, KeyEventArgs e)
    _ignoreKeyDown = e switch
        {Shift: true} => true,
        {Control: true} => false,
        _ => false

On the {Shift: true} line, SonarCloud shows this:

AFAIK, there is no way to simplify this expression. Is this truly a false positive?

Hey there.

Thanks for the report! This matches an existing false-positive report that you can track here: S3440 FP: Variable assignment and switch expression · Issue #5246 · SonarSource/sonar-dotnet · GitHub

In the future, please be sure to include additional information from the topic template: like which product you are using (SonarQube / SonarCloud) and, if SonarQube, which version.

Thank you! I don’t remember seeing a template, but I did include “sonarcloud” in the list of tags for the post.

:person_facepalming: I totally missed it reading your post too.

And it was moved from the Get Help > SonarCloud category to the False-Positive category….So of course you wouldn’t have seen the template.

Sorry about that. Every week I have to blame one post on Monday morning — this is that post this week. :sweat_smile:

1 Like

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