Elements added to an array should be used
- USELESS_ARRAY
- Code Quality
- Low
- No tags
This rule applies when an array becomes useless because elements added to the array are never used.
For maintainability, it is recommended to remove useless arrays. Also, it might be a mistake that a programmer forgets to use the array.
Noncompliant Code Example
View with compliant examples side by side// Example 1
function makeMap(data) {
let map = new Map();
let keys = []; // USELESS_ARRAY alarm
for (let elem of data) {
let key = elem.key;
let value = elem.value;
keys.push(key);
map.set(key, value);
}
return map;
}
// Example 2
function countIf(arr, predicate) {
let satisfying = []; // USELESS_ARRAY alarm because only 'length' property is used.
for (let elem of arr) {
if (predicate(elem)) {
satisfying.push(elem);
}
}
return satisfying.length;
}
Compliant Code Example
View with noncompliant examples side by side// Example 1
function makeMap(data) {
let map = new Map();
for (let elem of data) {
let key = elem.key;
let value = elem.value;
map.set(key, value);
}
return map;
}
// Example 2
function countIf(arr, predicate) {
let satisfying = 0;
for (let elem of arr) {
if (predicate(elem)) {
satisfying++;
}
}
return satisfying;
}
Version
This rule was introduced in DeepScan 1.32.0.