Rule javascript:S1226 (BUG) states: Function parameters, caught exceptions and foreach variables’ initial values should not be ignored.
However, when a parameter value is only conditionally overridden, the initial value is not completely ignored; it is only ignored in certain possibly-rare cases. When this conditional override happens inside a block bound by curly braces {…}, Sonarqube flags the issue in what is actually a false positive:
function demonstrateNonCompliant(param1, param2 = false) {
{
if(typeof param1 === 'undefined') {
param2 = true; //False positive BUG flag here
}
//...
if(param2) {
//do something else
}
}
}
function demonstrateCompliant(param1, param2 = false) {
//Not starting try { here to avoid NEW BUG failure in Sonarqube
if(typeof param1 === 'undefined') {
param2 = true;
}
//...
if(param2) {
//do something else
}
//Not using } catch (err) {... here to avoid NEW BUG failure in Sonarqube
}
Sonarqube version: Version 8.9.5 (build 50698)