java/local-shadows-field: a local declared inside a lambda or anonymous class body is not reported
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- Ein halber Tag
- Anfängerfreundlichkeit
- 55/100
Rechercherichtung
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.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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)
- Vorherrschende Sprache
- CodeQL
- Sterne
- 10.3k
- Forks
- 2.1k
- Ø Merge
- 1 T. 22 Std.
- Gemergte PRs (30 T.)
- 126
Entwicklungsumgebung
Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus github/codeql
-
JavaScript externs model StorageEvent() with no type argumentEvtl. vergeben @asgerf hat das vor 13 Tagen übernommen. Offenfalse-positive javascript
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 84/100
github/codeql#22632 · 3 Kommentare · 1 Reaktion · 2 zugewiesene Personen ·
Maintainer antworten meist innerhalb von 1 Tag
-
Add AlertSuppression.ql for Rust (inline // codeql[...] suppression)Evtl. vergeben @cnuss hat das vor 214 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
github/codeql#21637 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
false-positive
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
github/codeql#21076 · 3 Kommentare · 3 Reaktionen ·
Maintainer antworten meist innerhalb von 1 Tag
-
General issueOffenquestion
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 5/100
Maintainer antworten meist innerhalb von 1 Tag
-
False positive java/local-variable-is-never-read for Kotlin data object-generated equals temporariesOffen
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 35/100
Maintainer antworten meist innerhalb von 1 Tag
Ähnliche Issues
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
NousResearch/hermes-agent#148376 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 80/100
-
needs-area
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
Azure/typespec-azure#5936 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 Unter einer Stunde Anfängerfreundlichkeit 72/100
TencentCloud/octop-harness#215 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
bridgecrewio/checkov#7777 ·
Maintainer antworten meist innerhalb von 1 Tag