sonarjs/no-in-misuse
eslint-plugin-sonarjs
SuggestionRecommended
eslint.config.js
export default [
{
rules: {
"sonarjs/no-in-misuse": "error",
},
},
];function func1() {
let arr = ["a", "b", "c"];
let expectedValue = "b";
if (expectedValue in arr) { // Noncompliant, will be always false
return expectedValue + " found in the array";
} else {
return expectedValue + " not found";
}
}
function func2() {
let arr = ["a", "b", "c"];
let expectedValue = "1"; // index #1 is corresponding to the value "b"
if (expectedValue in arr) { // Noncompliant, will be always true because the array is made of 3 elements and the #1 is always there whatever its value
return expectedValue + " found in the array";
} else {
return expectedValue + " not found";
}
}function func() {
let arr = ["a", "b", "c"];
let expectedValue = "b";
if (arr.includes(expectedValue)) {
return expectedValue + " was found in the array";
} else {
return expectedValue + " not found";
}
}Examples from the ESLint documentation.
sonarjs/no-in-misuse
eslint-plugin-sonarjs
export default [
{
rules: {
"sonarjs/no-in-misuse": "error",
},
},
];Oxlint does not have this rule.
Biome does not have this rule.
Assertions comparing incompatible types should not be made
Return values from functions without side effects should not be ignored