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);
}
}