S2583 falsely triggered by preprocessor directives

Please provide

  • Operating system: Windows 10
  • Visual Studio version: 17.9.6
  • SonarLint plugin version: Nuget SonarAnalyzers.CSharp 9.32.0.97167
  • Programming language you’re coding in: C#
  • Is connected mode used: No
    • Connected to SonarCloud or SonarQube (and which version):

And a thorough description of the problem / question:
Snippet

S2583

 bool isDebug = false;
#if DEBUG
        isDebug = true;
#endif
if(isDebug)
{
}

This will trigger:
Warning S2583 Change this condition so that it does not always evaluate to ‘True’. Some code paths are unreachable.

And it shouldn’t be triggered, because debug is changing depending on a preprocessor directive.

Hello @twojnarowski,

Thank you for raising this issue!

Currently our engine does not support preprocessor directives.
We rely on the compilation context so, if you compile without the DEBUG property, we won’t see the code within DEBUG preprocessor.

Based on your snippet, the only suggestion I could give you is to replace the if (isDebug) and put the content inside the #if DEBUG directive:

#if DEBUG
    // Whatever was inside the if (isDebug) block
#endif

That way the issue should not appear anymore.
However, your actual code might be more complex than that.

I have created a ticket in our backlog to fix this issue in the future.

Have a nice day!