[java-inspections] IDEA-364649 Warning both with and without @NotNull annotation on parameter

GitOrigin-RevId: 2d9b88faef2947ef3719040c6078821e3efbf9de
This commit is contained in:
Tagir Valeev
2025-01-10 15:58:52 +00:00
committed by intellij-monorepo-bot
parent 71f54717d0
commit e9fe15f4e9
3 changed files with 36 additions and 0 deletions
@@ -923,6 +923,10 @@ public class NullableStuffInspectionBase extends AbstractBaseJavaLocalInspection
private static boolean isSuperNotAnnotated(NullableNotNullManager nullableManager, PsiParameter parameter, PsiParameter superParameter) {
if (hasNullability(nullableManager, superParameter)) return false;
if (ContainerUtil.exists(getSuperAnnotationOwners(superParameter),
superSuperParameter -> hasNullability(nullableManager, superSuperParameter))) {
return false;
}
PsiType type = superParameter.getType();
if (TypeUtils.isTypeParameter(type)) {
PsiClass childClass = PsiUtil.getContainingClass(parameter);
@@ -0,0 +1,26 @@
import org.jetbrains.annotations.NotNull;
class Foo {
interface I {
// Warning: "Overriding method parameters are not annotated", expected
void m(<warning descr="Overriding method parameters are not annotated">@NotNull</warning> String s);
}
static class S implements I {
@Override
// Warning: "Not annotated parameter overrides @NotNull parameter", expected
public void m(String <warning descr="Not annotated parameter overrides @NotNull parameter">s</warning>) {
System.out.println(s);
}
}
static class C extends S {
@Override
// Warning: "Parameter annotated @NotNull should not override non-annotated parameter"
// undesired if we have no control over the S superclass.
// Having annotation is still preferred here
public void m(@NotNull String s) {
super.m(s);
}
}
}
@@ -434,4 +434,10 @@ public class NullableStuffInspectionTest extends LightJavaCodeInsightFixtureTest
DataFlowInspectionTestCase.addJetBrainsNotNullByDefault(myFixture);
doTest();
}
public void testNoNotNullWarningIfIndirectSuperMethodIsAnnotated() {
myInspection.REPORT_ANNOTATION_NOT_PROPAGATED_TO_OVERRIDERS = true;
myInspection.REPORT_NOTNULL_PARAMETERS_OVERRIDES_NOT_ANNOTATED = true;
doTest();
}
}