nullable problems: do not suggest to annotate as @Nullable when parameter is referenced (IDEA-93083)

This commit is contained in:
anna
2012-10-18 19:36:59 +02:00
parent 7bf48f5252
commit b87a66990c
4 changed files with 50 additions and 7 deletions
@@ -174,13 +174,25 @@ public class NullableStuffInspection extends BaseLocalInspectionTool {
new AddAnnotationFix(anno, parameter, ArrayUtil.toStringArray(annoToRemove)));
}
else if (annotated.isDeclaredNullable && manager.isNotNull(parameter, false)) {
final PsiIdentifier nameIdentifier2 = parameter.getNameIdentifier();
assert nameIdentifier2 != null : parameter;
holder.registerProblem(nameIdentifier2, InspectionsBundle.message(
"inspection.nullable.problems.annotated.field.constructor.parameter.conflict", StringUtil.getShortName(anno),
notNullSimpleName),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
new AddAnnotationFix(anno, parameter, ArrayUtil.toStringArray(annoToRemove)));
boolean usedAsQualifier = !ReferencesSearch.search(parameter).forEach(new Processor<PsiReference>() {
@Override
public boolean process(PsiReference reference) {
final PsiElement element = reference.getElement();
if (element instanceof PsiReferenceExpression && element.getParent() instanceof PsiReferenceExpression) {
return false;
}
return true;
}
});
if (!usedAsQualifier) {
final PsiIdentifier nameIdentifier2 = parameter.getNameIdentifier();
assert nameIdentifier2 != null : parameter;
holder.registerProblem(nameIdentifier2, InspectionsBundle.message(
"inspection.nullable.problems.annotated.field.constructor.parameter.conflict", StringUtil.getShortName(anno),
notNullSimpleName),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
new AddAnnotationFix(anno, parameter, ArrayUtil.toStringArray(annoToRemove)));
}
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>8</line>
<description>Constructor parameter for @Nullable field is annotated @NotNull</description>
</problem>
</problems>
@@ -0,0 +1,21 @@
import org.jetbrains.annotations.*;
class Test {
@Nullable private final String baseFile;
@Nullable private final String baseFile1;
public Test(@NotNull String baseFile) {
this.baseFile = baseFile;
this.baseFile1 = null;
}
public Test(@NotNull String baseFile1, boolean a) {
this.baseFile1 = baseFile1;
if (baseFile1.contains("foo")) {
this.baseFile = null;
} else {
this.baseFile = null;
}
}
}
@@ -43,6 +43,7 @@ public class NullableStuffInspectionTest extends InspectionTestCase {
public void testProblems() throws Exception{ doTest(); }
public void testProblems2() throws Exception{ doTest(); }
public void testNullableFieldNotnullParam() throws Exception{ doTest(); }
public void testJdk14() throws Exception{ doTest14(); }
public void testGetterSetterProblems() throws Exception{ doTest(); }