eclipse-jdt / eclipse-jdt/eclipse.jdt.core

EEAs applied incorrectly when using "Inherit null annotations" with NonNullByDefault

Open
#2,512 9 comments 0 reactions 1 assignee Claimed by @stephan-herrmann View on GitHub
null
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 10h
Merged PRs (30d)
49

Description

Given:

1. An EEA file for Map containing:
```java
class java/util/Map


get
(Ljava/lang/Object;)TV;
(Ljava/lang/Object;)T0V;
```

2. "Inherit null annotations" compiler option enabled

3. A test class with `@NonNullByDefault`

Then in the following example the compiler correctly determines that `Map#get` can return null values but incorrectly determines that `HashMap#get` or `AbstractMap#get` will always return non-null values.

```java
package test;

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;

public class Test {

@NonNullByDefault
public static void main(final String[] args) {

final HashMap hashmap = new HashMap<>();
if (hashmap.get("") == null) { // compiler warning: Redundant null check: comparing '@NonNull Object' against null
}

final AbstractMap abstractmap = new HashMap<>();
if (abstractmap.get("") == null) { // compiler warning: Redundant null check: comparing '@NonNull Object' against null
}

final Map map = new HashMap<>();
if (map.get("") == null) { // no warning
}
}
}
```

I am using Eclipse 2023-03 with these null analysis settings:

![image](https://github.com/eclipse-jdt/eclipse.jdt.core/assets/426959/fd84ebca-b3a6-4db9-9232-2a757e931127)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.