Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 499596

Summary: [null][1.8] Bogus warning with NonNullByDefault
Product: [Eclipse Project] JDT Reporter: Clovis Seragiotto <clovis.seragiotto>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: CLOSED MOVED QA Contact:
Severity: normal    
Priority: P3 CC: manoj.palat, stephan.herrmann
Version: 4.7   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
See Also: https://github.com/eclipse-jdt/eclipse.jdt.core/issues/682
Whiteboard:

Description Clovis Seragiotto CLA 2016-08-12 06:23:17 EDT
In the following class, one gets the warning: "The nullness annotation is redundant with a default that applies to this location", which is right.

import static org.eclipse.jdt.annotation.DefaultLocation.*;
import org.eclipse.jdt.annotation.*;
import java.util.*;

class Foo {
	@NonNullByDefault({ PARAMETER, RETURN_TYPE, FIELD, TYPE_PARAMETER, TYPE_BOUND, TYPE_ARGUMENT, ARRAY_CONTENTS })
	static Collection<String[]> from(@NonNull String @NonNull [] @NonNull... elements) { // <-- warning here, ok
		return Collections.singleton(elements[0]); <-- warning here if any @NonNull is removed
	}
}

If, however, any of the redundant @NonNull annotations is removed, one gets another warning about null type safety ("the expression needs unchecked conversion")
Comment 1 Stephan Herrmann CLA 2016-08-13 17:46:53 EDT
Here's what I see in HEAD:

As shown in comment 0 the return statement has this warning:

Null type safety (type annotations): The expression of type 'Set<String @NonNull[]>' needs unchecked conversion to conform to '@NonNull Collection<String @NonNull[]>', corresponding supertype is 'Collection<String @NonNull[]>'

This concerns the toplevel type Set. This can be fixed using an external @NonNull annotation on the return of Collections.singleton().

After that, removing either the second or third annotation in the line above, makes a new warning appear:

Null type safety (type annotations): The expression of type '@NonNull String []' needs unchecked conversion to conform to 'String @NonNull[]'

Not only is this warning wrong, also showing the same warning in both cases looks bogus.

Next when I replace the @NNBD by spelling out:

   Collection<@NonNull String @NonNull[]> from(..)

I get no warning even with the second annotation on the parameter removed.
Comment 2 Manoj N Palat CLA 2018-05-17 03:23:54 EDT
bulk move out of 4.8
Comment 3 Manoj N Palat CLA 2018-08-16 00:08:32 EDT
Bulk move out of 4.9
Comment 4 Stephan Herrmann CLA 2020-06-10 03:34:49 EDT
.
Comment 5 Eclipse Genie CLA 2022-08-07 01:16:56 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 6 Stephan Herrmann CLA 2023-01-31 15:59:23 EST
Over to github: https://github.com/eclipse-jdt/eclipse.jdt.core/issues/682