[java-dfa] Do not suggest 'Nullable' annotation if SUGGEST_NULLABLE_ANNOTATIONS is turned off and NotNull is inferred

GitOrigin-RevId: f1b30c8502b8b596d82ad8fea7b9a7aaceef6b80
This commit is contained in:
Tagir Valeev
2021-05-06 02:50:24 +00:00
committed by intellij-monorepo-bot
parent 5e681a6393
commit 906dfbdffa
3 changed files with 21 additions and 3 deletions
@@ -994,9 +994,10 @@ public abstract class DataFlowInspectionBase extends AbstractBaseJavaLocalInspec
PsiAnnotation anno = info == null ? null : info.getAnnotation();
Nullability nullability = info == null ? Nullability.UNKNOWN : info.getNullability();
if (nullability == Nullability.NULLABLE) {
if (!AnnotationUtil.isInferredAnnotation(anno)) return;
if (DfaPsiUtil.getTypeNullability(method.getReturnType()) == Nullability.NULLABLE) return;
if (!info.isInferred() || DfaPsiUtil.getTypeNullability(method.getReturnType()) == Nullability.NULLABLE) return;
}
// In rare cases, inference may produce different result (e.g. if nullable method overrides non-null method)
if (nullability == Nullability.NOT_NULL && info.isInferred()) return;
if (nullability != Nullability.NOT_NULL && (!SUGGEST_NULLABLE_ANNOTATIONS || block.getParent() instanceof PsiLambdaExpression)) return;
@@ -1013,7 +1014,7 @@ public abstract class DataFlowInspectionBase extends AbstractBaseJavaLocalInspec
boolean exactlyNull = isNullLiteralExpression(expr) || expressions.get(expr) == ConstantResult.NULL;
if (!REPORT_UNSOUND_WARNINGS && !exactlyNull) continue;
if (nullability == Nullability.NOT_NULL && !info.isInferred()) {
if (nullability == Nullability.NOT_NULL) {
String presentable = NullableStuffInspectionBase.getPresentableAnnoName(anno);
final String text = exactlyNull
? JavaAnalysisBundle.message("dataflow.message.return.null.from.notnull", presentable)
@@ -0,0 +1,16 @@
import org.jetbrains.annotations.*;
public class InferenceNullityMismatch {
static String getData(Super obj) {
if (!(obj instanceof Sub)) {
throw new IllegalArgumentException();
}
return obj.calculate();
}
}
class Super {
native @NotNull String calculate();
}
class Sub extends Super {
native @Nullable String calculate();
}
@@ -700,4 +700,5 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase {
public void testCastInCatch() { doTest(); }
public void testInitArrayInConstructor() { doTest(); }
public void testGetterNullityAfterCheck() { doTest(); }
public void testInferenceNullityMismatch() { doTestWith(insp -> insp.SUGGEST_NULLABLE_ANNOTATIONS = false); }
}