-
Notifications
You must be signed in to change notification settings - Fork 18
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
Array creation annotation checks #928
base: master
Are you sure you want to change the base?
Changes from 5 commits
13ba984
04f7dc3
beff18b
6f8ee2a
398ca37
95b896d
a8351aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -348,7 +348,11 @@ public Void visitNewArray(NewArrayTree tree, Void p) { | |
componentType.getAnnotations(), | ||
type.toString()); | ||
} | ||
|
||
List<? extends AnnotationMirror> annotations = | ||
TreeUtils.annotationsFromArrayCreation(tree, 0); | ||
if (AnnotationUtils.containsSame(annotations, NULLABLE)) { | ||
checker.reportWarning(tree, "new.array.nullable.ignored"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we use this error message? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, because it is not about the component type. |
||
return super.visitNewArray(tree, p); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -22,6 +22,7 @@ nulltest.redundant=redundant test against null; "%s" is non-null | |||||
instanceof.nullable=instanceof is only true for a non-null expression | ||||||
instanceof.nonnull.redundant=redundant @NonNull annotation on instanceof | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we have a similar warning for object and array creations? |
||||||
new.array.type.invalid=annotations %s may not be applied as component type for array "%s" | ||||||
new.array.nullable.ignored=nullable annotation on new array main modifier ignored, must be nonnull | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see any other message, here or in the basetype, refer to the Other messages in this file also don't use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also can't see any reference to main or primary anywhere else. Where does this wording originate from, what other contexts is it used in? We could use the JLS wording:
Suggested change
|
||||||
new.class.type.invalid=the annotations %s do not need be applied in object creations | ||||||
nullness.on.constructor=do not write nullness annotations on a constructor, whose result is always non-null | ||||||
nullness.on.enum=do not write nullness annotations on an enum constant, which is always non-null | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
public class ArrayMainModifierNullableAnnotation { | ||
void foo() { | ||
// :: warning: (new.array.nullable.ignored) | ||
int[] o = new int @Nullable [10]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's usually a good idea to include a test where you expect no error. Here, how about adding something with |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type
is the type of theNewArrayTree
, so wouldn't this perform the check you want?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
@PolyNull
? Is that allowed?MonotonicNonNull
?Are some of these checks maybe in
isValid
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type
is holding what the annotation defaulted to (i.e.@NonNull
) and I couldn't find a utility to retrieve the original@Nullable
annotation (i.e. getExplicitonAnnotation() does not hold onto it), so I pull the annotations directly from the 0th dim of the tree.Thanks for bringing up
@PolyNull
and@MonotonicNonNull
, both suggest possible nullness so I think they should be disallowed.