Unsafe recommendation with "SonarLint: Correct this '|' to '||' "

  • Operating system: MacOS 14.2.1
  • SonarLint plugin version:
  • Programming language you’re coding in: C# with Rider
  • Is connected mode used: No

And a thorough description of the problem / question:

I am working with a codebase that was converted and uses non-short circuiting operators.

Sonarlint recommends “Use short-circuiting operators”. When I click this it inserts the extra character as expected however this is not safe as there is different precedence between this and other operators for example

The following code is contains the warning

        if (startDate < systemDate && 
            action == ActionType.DFS |
            action == ActionType.ROOS)

The action performs this:

        if (startDate < systemDate && 
            action == ActionType.DFS ||
            action == ActionType.ROOS)

But the expected/correct transformation that preserves the semantics is:

        if (startDate < systemDate && 
            (action == ActionType.DFS ||
            action == ActionType.ROOS))

Note the extra parenthesis.

Hi @daveh and thanks for the report!
Even though the code fix does exactly what it says (using a short-circuiting operator), I agree that it is problematic, that it does not preserve operator precedence. I created an issue in our backlog: