You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
java/local-shadows-field: a local declared inside a lambda or anonymous class body is not reported #22969
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.
publicclassShadowSeed {
String[] arr;
String[] getArr() {
String[] arr; // shadows the field; the query fires herereturnthis.arr;
}
}
variant/ShadowVariant.java:
publicclassShadowVariant {
String[] arr;
String[] getArr() {
Runnabler = () -> {
String[] arr; // the same local inside a lambda body; not reported
};
r.run();
returnthis.arr;
}
}
control/ShadowControl.java:
publicclassShadowControl {
String[] arr;
String[] getArr() {
Runnabler = newRunnable() {
publicvoidrun() {
String[] arr; // inside an anonymous class; not reported either
}
};
r.run();
returnthis.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)
Activity
sam-robson commentedon Nov 3, 2026
Hi @MarkLee131, thanks for the detailed report. We'll take a look into this and follow up.