FP: (javascript:S1226) Sonar sometimes doesn't recognize that param reassignment is conditional

Please provide

  • Operating system: Win10
  • SonarLint plugin version: 3.8.0
  • Is connected mode used: Yes
    • Connected to SonarCloud or SonarQube (and which version): SonarQube 8.9.7 (build 52159)

And a thorough description of the problem / question:

There is a false positive when a parameter is conditionally reassigned, raising an issue as if the parameter were completely ignored when it is used in most cases and overridden only in one other. The behavior is also inconsistent, being observed only inside a code block (such as a try-catch); moving the code outside the block (e.g. here, removing the try/catch) gets rid of the false positive but may have other unwanted impacts. The false positive is also not observed in TypeScript.

const demoFn = function(param1, booleanValue = false) {
    try { //Issue is only observable in a code block like this (e.g. a try...catch)
        if(param1 === 0) {
            //sonarlint(javascript:S1226) FP on next line:
            //Introduce a new variable or use its initial
            //value before reassigning "booleanValue".
            booleanValue = true;
        }
        if(booleanValue) {
            console.log('Boolean value is true; respond appropriately.');
        } else {
            console.log('Boolean value is false; respond appropriately.');
        }
    } catch (err) {
        console.error('Error: ', err);
    }
}

Hi,

The current version of SonarLint for Visual Studio is 6.7. Could you upgrade and see if this is still a problem?

 
Ann

This looks like the latest available in Visual Studio Code.
I’m not able to update SonarQube because later major version’s aren’t LTS releases, but you may be able to try it yourself usin the test code above.

Hi,

Sorry. I’m going to blame that on a case of the Fridays. The language experts have been pinged on this. They should be along soon.

 
Ann

Thanks @unblocker for reporting this.

We have opened an issue to track this. Agreed that it does not look consistent.

Cheers,
Victor

Thanks!
To be clear, the desired resolution is that Sonar shouldn’t flag this conditional assignment as an issue at all.

Hi Ben,

agreed, given that the rule has exceptions on certain conditional assignments, it should be normalized to add into the exceptions all conditional assignments. We will check internally why it was like this in the first place.

Again, thanks for reporting.
Victor