redhat-developer / redhat-developer/vscode-java
@Nonnull fields are assumed to be non-null in the constructor
オープン
まだ誰も着手していません。
bug
code analysis
upstream
- 主要言語
- TypeScript
- スター
- 2.3k
- フォーク
- 546
- 平均マージ
- 20時間 1分
- マージ済み PR(30日)
- 11
説明
Class fields annotated with @Nonnull are currently assumed to never be null in the class constructor.
@Nonnull only provides guarantees after construction has finished, and checking these fields in the constructor is often useful to avoid boilerplate variables.
A toy example illustrating the issue:
public class AnnotatedClass {
private @Nonnull String specialValue;
private @Nonnull Map<String, String> allValues = new HashMap<>();
/* this constructor incorrectly warns that specialValue might not have been initialized */
public AnnotatedClass(Iterable<String> values) {
for (String entry : values)
{
String[] split = entry.split("=", 1);
if (split.length != 2) throw new IllegalArgumentException("Value improperly formatted");
final String key = split[0];
final String value = split[1];
if (value == null) /* needed to avoid warning on the assignment to specialValue later */
continue; /* side note: ideally we wouldn't need this, and the IDE */
/* would recognize that entry.split returns array of non-null */
this.allValues.put(key, value);
if ("specialKey".equals(key))
this.specialValue = value;
}
if (this.specialValue == null) /* the next line incorrectly produces a dead code warning */
throw new IllegalArgumentException("The provided iterable does not have a special value!");
}
}
Environment
- Operating System: Windows 10 Enterprise
- JDK version: openjdk 11.0.12 2021-07-20
- Visual Studio Code version: 1.71.2
- Java extension version: v1.11.0
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、提供されている AnnotatedClass の例を、Java 拡張機能と報告された JDK 環境を使用して VS Code で実行し、警告を再現します。構築中の @Nonnull フィールドに対する null 解析の動作を追跡します。null チェックと後続の代入で誤った警告が生成されなくなり、実際の問題は引き続き報告されれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- java
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100