github / github/codeql

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

未关闭
#9,242 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
acknowledged false-positive JS
主要语言
CodeQL
星标
10.1k
派生
2.1k
平均合并
2 天 15 小时
30 天内合并 PR
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 摘要。