github / github/codeql

LGTM.com - false positive: "Unused index variable" when using array to set order of execution

Open
#9,242 2 comments 0 reactions 0 assignees View on GitHub
acknowledged false-positive JS
Dominant language
CodeQL
Stars
10.1k
Forks
2.1k
Avg merge
2d 15h
Merged PRs (30d)
141

Description

**Description of the false positive**

I am creating a button toolbar, and I have an array called `buttonsOrder` that holds the position of each button to be inserted into the toolbar (say `[5,4,2,1,6,3,7]`), where the 1st button will be inserted 5th, the 2nd will be inserted 4th, the 3rd inserted 2nd, etc. The maximum value of each array element is guaranteed to be the same as the length of the array (due to the nature of the data contained), and so to do this I am using the for loop below:

```
for (let i = 1; i <= buttonsOrder.length; i++) {
if (i == buttonsOrder[0]) {
// Insert 1st button
}
if (i == buttonsOrder[1]) {
// Insert 2nd button
}
...
}
```

This ensures the order of the buttons by only executing each index's corresponding code on its respective iteration, however, it is detected by LGTM as not using the index variable (due to me comparing `i` to the _value_ of each index, rather than using `i` _as_ an index). I am not aware of a better way to do what I am trying to do, although if it exists, please excuse my ignorance.

**URL to the alert on the project page on LGTM.com**

https://lgtm.com/projects/g/programmer2514/BetterDiscord-CollapsibleUI/snapshot/768904a74c3558d87e320c9b6618089032d8e627/files/CollapsibleUI.plugin.js?sort=name&dir=ASC&mode=heatmap#xc9cf67112902b80f:1

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.