github / github/codeql

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

オープン
#9,242 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る
acknowledged false-positive JS
主要言語
CodeQL
スター
10.1k
フォーク
2.1k
平均マージ
2日 15時間
マージ済み PR(30日)
141

説明

**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

コントリビューションガイド

コントリビューションガイドを開く

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。