Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.checkerframework.dataflow.expression.ThisReference;
import org.checkerframework.dataflow.util.NodeUtils;
import org.checkerframework.framework.flow.CFAbstractAnalysis;
import org.checkerframework.framework.qual.AnnotatedFor;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.checkerframework.framework.qual.TypeUseLocation;
import org.checkerframework.framework.type.AnnotatedTypeFactory;
Expand Down Expand Up @@ -416,6 +417,15 @@ public NullnessNoInitAnnotatedTypeFactory(BaseTypeChecker checker) {
"org.jspecify.nullness.NullMarked",
DefaultQualifier.class.getCanonicalName(),
nullMarkedDefaultQual);

AnnotationMirror annotatedForNullness =
Comment thread
aosen-xiong marked this conversation as resolved.
new AnnotationBuilder(processingEnv, AnnotatedFor.class)
.setValue("value", new String[] {"nullness"})
.build();
addAliasedDeclAnnotation(
"org.jspecify.annotations.NullMarked",
AnnotatedFor.class.getCanonicalName(),
annotatedForNullness);
}

boolean permitClearProperty =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ public class NullnessNullMarkedTest extends CheckerFrameworkPerDirectoryTest {
* @param testFiles the files containing test code, which will be type-checked
*/
public NullnessNullMarkedTest(List<File> testFiles) {
super(testFiles, org.checkerframework.checker.nullness.NullnessChecker.class, "nullness");
super(
testFiles,
org.checkerframework.checker.nullness.NullnessChecker.class,
"nullness",
// TODO: Replace this `-AonlyAnnotatedFor` flag after EISOP#1290 is merged.
"-AuseConservativeDefaultsForUncheckedCode=source");
}

@Parameters
Expand Down
11 changes: 11 additions & 0 deletions checker/tests/nullness-nullmarked/NullMarkedSuppressError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class NullMarkedSuppressError {
@org.jspecify.annotations.NullMarked
Comment thread
aosen-xiong marked this conversation as resolved.
Outdated
class A {
// :: error: (assignment.type.incompatible)
Object o = null;
}

class B {
Object o = null;
Comment thread
aosen-xiong marked this conversation as resolved.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.checker.signature.qual.ClassGetName;
import org.checkerframework.dataflow.cfg.visualize.CFGVisualizer;
import org.checkerframework.framework.qual.AnnotatedFor;
import org.checkerframework.framework.qual.SubtypeOf;
import org.checkerframework.framework.source.SourceChecker;
import org.checkerframework.framework.type.AnnotatedTypeFactory;
Expand All @@ -25,6 +26,7 @@
import java.util.Set;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;

/**
* An abstract {@link SourceChecker} that provides a simple {@link
Expand Down Expand Up @@ -313,4 +315,31 @@ public BaseTypeChecker getUltimateParentChecker() {
causeMessage);
}
}

/**
* This method is almost similar to {@link

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to find a solution without this duplication.

@aosen-xiong aosen-xiong Aug 26, 2025

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With code refactoring in #1331, we can get rid of this code duplication.

EDIT: tested at aosen-xiong#13.

* org.checkerframework.framework.util.defaults.QualifierDefaults#isElementAnnotatedForThisChecker(Element)}.
* except for early return if element is null, the conservative default for "source" is not used
* and do not recursively check enclosing elements because the logic is implemented at call
* sites.
*
* @param elt the source code element to check, or null
* @return true if the element is annotated for this checker or an upstream checker
*/
@Override
protected boolean isAnnotatedForThisCheckerOrUpstreamChecker(@Nullable Element elt) {
if (elt == null || !useConservativeDefault("source")) {
return false;
}

boolean elementAnnotatedForThisChecker = false;
AnnotationMirror annotatedFor = getTypeFactory().getDeclAnnotation(elt, AnnotatedFor.class);

if (annotatedFor != null) {
elementAnnotatedForThisChecker =
getTypeFactory().doesAnnotatedForApplyToThisChecker(annotatedFor);
}

return elementAnnotatedForThisChecker;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2984,7 +2984,7 @@ protected boolean messageKeyMatches(
* @param elt the source code element to check, or null
* @return true if the element is annotated for this checker or an upstream checker
*/
private boolean isAnnotatedForThisCheckerOrUpstreamChecker(@Nullable Element elt) {
protected boolean isAnnotatedForThisCheckerOrUpstreamChecker(@Nullable Element elt) {
if (elt == null || !useConservativeDefault("source")) {
return false;
}
Expand Down