java/local-shadows-field: a local declared inside a lambda or anonymous class body is not reported

オープン
#22,969 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
半日
初心者へのやさしさ
55/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
java
領域
devtools

調査の方向性

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
平均マージ
1日 22時間
マージ済み PR(30日)
126

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

github/codeql のほかの issue

github/codeql の issue をすべて見る

似ている issue

DevTools の issue をもっと見る

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

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