Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of eclipse external annotations (EEA) can cause false positive warning: Null type safety: generic needs unchecked conversion to conform to @NonNull type. #1008

Closed
krallus opened this issue Apr 26, 2023 · 0 comments · Fixed by #1913
Assignees
Labels
null Issues related to null pointer analysis

Comments

@krallus
Copy link

krallus commented Apr 26, 2023

Use of eclipse external annotations (EEA) can cause false positive warning: Null type safety: generic needs unchecked conversion to conform to @nonnull type.

Environment
Eclipse IDE for Enterprise Java and Web Developers
Version: 2023-03 (4.27.0)
Build id: 20230309-1520

OS: Linux Mint 21.1 Cinnamon

Project is set to compile using Java 1.8, although I'm not sure it matters.

Issue
Just as the title says. Most easily demonstrated in code.

However, note first that I have the Integer.valueOf(String) method annotated in EEA as both taking and returning NonNull. Specifically in the file <path-to-eea-directory>/java/lang/Integer.eea:

class java/lang/Integer
valueOf
 (Ljava/lang/String;)Ljava/lang/Integer;
 (L1java/lang/String;)L1java/lang/Integer;

Now for the code:

import org.eclipse.jdt.annotation.Checks;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

public class UncheckedConversionFalsePositive {

	public static @NonNull Integer doSomething(@NonNull final String someValue) {
		return Integer.valueOf(someValue);
	}

	public static void main(final String[] args) {
		final @Nullable String nullableString = "12";
		@Nullable Integer result;

		// This first example that uses my own annotated method and not eclipse external annotations
		// works no problem...

		result = Checks.applyIfNonNull(nullableString, UncheckedConversionFalsePositive::doSomething);

		// But now, if I do this, which relies on EEA annotations instead of my in-code annotations....

		result = Checks.applyIfNonNull(nullableString, Integer::valueOf);

		// Then Integer::valueOf is flagged with the following message:

		/*
		 * Null type safety: parameter 1 provided via method descriptor
		 * Function<String,Integer>.apply(String) needs unchecked conversion to conform to '@NonNull
		 * String'
		 */

		// Note that the same warning is shown on "someValue" below when using a lambda expression
		// instead of a method reference.

		result = Checks.applyIfNonNull(nullableString, someValue -> Integer.valueOf(someValue));

		// The workaround to eliminate this warning without suppressing it is to make the
		// generic parameters explicit with @NonNull but this is very verbose and should
		// ideally be unnecessary...

		result = Checks.<@NonNull String, Integer>applyIfNonNull(nullableString, Integer::valueOf);
	}
}

EDIT: Just a note that this warning is generated when the following Java compiler warning is enabled: Unchecked conversion from non-annotated type to @NonNull type.

@stephan-herrmann stephan-herrmann added the null Issues related to null pointer analysis label Nov 28, 2023
@stephan-herrmann stephan-herrmann self-assigned this Nov 28, 2023
stephan-herrmann added a commit to stephan-herrmann/eclipse.jdt.core that referenced this issue Jan 25, 2024
warning: Null type safety: generic needs unchecked conversion to conform
to @nonnull type.

fixes eclipse-jdt#1008

Improve propagation of annotations during inference
stephan-herrmann added a commit that referenced this issue Jan 25, 2024
…rning: Null type safety: generic needs unchecked conversion to conform to @nonnull type. (#1913)

fixes #1008

Improve propagation of annotations during inference
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
null Issues related to null pointer analysis
Projects
None yet
2 participants