java/local-shadows-field: a local declared inside a lambda or anonymous class body is not reported
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
The likely cause is the shadows predicate in Shadowing.qll, which requires the local's callable to be declared by the class that owns the field (method.getDeclaringType() = c). Start by running the three samples (ShadowSeed, ShadowVariant, ShadowControl) against java/local-shadows-field and confirming only the seed fires. Done when the local inside the lambda and inside the anonymous class is reported, and the seed case still is.
由索引模型根据 Issue 内容生成。
描述
Description of the issue
The query reports the local String[] arr in ShadowSeed.getArr(), which has the same name as the field the method reads as this.arr. Declare the same local inside a lambda in that method (ShadowVariant) or inside an anonymous class (ShadowControl) and nothing is reported, though it still shadows the field and the method still reads this.arr.
Probably the shadows predicate in Shadowing.qll: it requires the local's callable to be declared by the class that declares the field (method.getDeclaringType() = c). Inside a lambda the callable is the lambda's own method, and inside an anonymous class it's that class's method.
Query: java/local-shadows-field (codeql/java-queries 1.1.10)
CodeQL CLI: 2.27.2
Code samples
seed/ShadowSeed.java:
public class ShadowSeed {
String[] arr;
String[] getArr() {
String[] arr; // shadows the field; the query fires here
return this.arr;
}
}
variant/ShadowVariant.java:
public class ShadowVariant {
String[] arr;
String[] getArr() {
Runnable r = () -> {
String[] arr; // the same local inside a lambda body; not reported
};
r.run();
return this.arr;
}
}
control/ShadowControl.java:
public class ShadowControl {
String[] arr;
String[] getArr() {
Runnable r = new Runnable() {
public void run() {
String[] arr; // inside an anonymous class; not reported either
}
};
r.run();
return this.arr;
}
}
How to reproduce
codeql database create db --language=java --build-mode=none \
--source-root=<one directory> --overwrite
codeql database analyze db \
codeql/java-queries:codeql-suites/java-security-and-quality.qls \
--format=csv --output=out.csv
# one database per directory
Expected result
"Potentially confusing name: method ... also refers to field arr (as this.arr)" on the local in ShadowVariant.java (line 5) and in ShadowControl.java (line 6), as on ShadowSeed.java (line 4).
Output of the commands above:
### CodeQL 2.27.2 query: java/local-shadows-field
### suite: java-security-and-quality.qls, --build-mode=none, per-file isolated databases
### extraction JDK: openjdk version "21.0.11" 2026-04-21 LTS
--- SEED -- expected: query fires (seed/)
"Possible confusion of local and field","A method in which a variable is declared with the same name as a fiel
--- VARIANT -- expected: query fires, actual: not reported (the bug) (variant/)
(query reported nothing)
--- CONTROL -- the same construct in an anonymous class, expected: query fires (control/)
(query reported nothing)
- 主要语言
- CodeQL
- 星标
- 10.3k
- 派生
- 2.1k
- 平均合并
- 2 天 6 小时
- 30 天内合并 PR
- 137
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
github/codeql 的其他 Issue
-
JavaScript externs model StorageEvent() with no type argument可能已有人在做 @asgerf 于 14 天前认领。 未关闭false-positive javascript
难度 2/5 1-3 小时 新手友好度 84/100
github/codeql#22632 · 3 条评论 · 1 个 reaction · 已指派 2 人 ·
维护者通常 1 天内回复
-
Add AlertSuppression.ql for Rust (inline // codeql[...] suppression)可能已有人在做 @cnuss 于 215 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
false-positive
难度 2/5 1-3 小时 新手友好度 70/100
github/codeql#21076 · 3 条评论 · 3 个 reaction ·
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 47/100
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 47/100
维护者通常 1 天内回复
相似的 Issue
-
normalizePath removes the Windows device namespace prefix可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
ai-driven-dev/framework#1147 ·
维护者通常 1 天内回复
-
divergence lane:functions
难度 2/5 1-3 小时 新手友好度 84/100
维护者通常 1 天内回复
-
cleanup
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 82/100
pytorch/tensordict#3456 ·
维护者通常 1 天内回复