LGTM.com - false positive: "Unused index variable" when using array to set order of execution
- Lingua principale
- CodeQL
- Stelle
- 10.1k
- Fork
- 2.1k
- Merge medio
- 2g 15h
- PR unite (30g)
- 141
Descrizione
**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
Guida per i contributori
Apri la guida per i contributori
Valutazione
Questa issue non è ancora stata valutata.