warn about null passed to non annotated parameter (IDEA-35867)

This commit is contained in:
anna
2011-04-08 09:29:01 +02:00
parent 838bc72ab6
commit 09fb925c01
2 changed files with 42 additions and 3 deletions
@@ -20,8 +20,10 @@ import com.intellij.codeInsight.NullableNotNullManager;
import com.intellij.codeInsight.daemon.GroupNames;
import com.intellij.codeInsight.intention.AddAnnotationFix;
import com.intellij.codeInsight.intention.impl.AddNotNullAnnotationFix;
import com.intellij.codeInsight.intention.impl.AddNullableAnnotationFix;
import com.intellij.codeInspection.*;
import com.intellij.codeInspection.ex.BaseLocalInspectionTool;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
@@ -51,6 +53,7 @@ public class NullableStuffInspection extends BaseLocalInspectionTool {
@SuppressWarnings({"WeakerAccess"}) public boolean REPORT_NOT_ANNOTATED_GETTER = true;
@SuppressWarnings({"WeakerAccess"}) public boolean REPORT_NOT_ANNOTATED_SETTER_PARAMETER = true;
@SuppressWarnings({"WeakerAccess"}) public boolean REPORT_ANNOTATION_NOT_PROPAGATED_TO_OVERRIDERS = true;
@SuppressWarnings({"WeakerAccess"}) public boolean REPORT_NULLS_PASSED_TO_NON_ANNOTATED_METHOD = true;
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
@@ -63,6 +66,30 @@ public class NullableStuffInspection extends BaseLocalInspectionTool {
checkNullableStuffForMethod(method, holder);
}
@Override
public void visitMethodCallExpression(PsiMethodCallExpression expression) {
if (!PsiUtil.isLanguageLevel5OrHigher(expression) || !REPORT_NULLS_PASSED_TO_NON_ANNOTATED_METHOD) return;
final PsiMethod psiMethod = expression.resolveMethod();
if (psiMethod != null) {
final PsiParameterList parameterList = psiMethod.getParameterList();
final PsiParameter[] parameters = parameterList.getParameters();
final PsiExpression[] expressions = expression.getArgumentList().getExpressions();
for (int i = 0, expressionsLength = expressions.length; i < Math.max(expressionsLength, parameters.length); i++) {
PsiExpression psiExpression = expressions[i];
if (psiExpression.getType() == PsiType.NULL) {
if (!AnnotationUtil.isNullable(parameters[i]) && !AnnotationUtil.isNotNull(parameters[i])) {
holder.registerProblem(psiExpression, "Null is passed to parameter which is not yet @Nullable", new AddNullableAnnotationFix(parameters[i]){
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
return true;
}
});
}
}
}
}
}
@Override public void visitField(PsiField field) {
if (!PsiUtil.isLanguageLevel5OrHigher(field)) return;
final PsiType type = field.getType();
@@ -402,6 +429,7 @@ public class NullableStuffInspection extends BaseLocalInspectionTool {
private JCheckBox myReportNotAnnotatedSetterParameter;
private JCheckBox myReportNotAnnotatedGetter;
private JCheckBox myReportAnnotationNotPropagated;
private JCheckBox myReportNullsPassedToNonAnnotatedParameter;
private OptionsPanel() {
super(new BorderLayout());
@@ -419,6 +447,7 @@ public class NullableStuffInspection extends BaseLocalInspectionTool {
myReportNotAnnotatedSetterParameter.addActionListener(actionListener);
myReportNotAnnotatedGetter.addActionListener(actionListener);
myReportAnnotationNotPropagated.addActionListener(actionListener);
myReportNullsPassedToNonAnnotatedParameter.addActionListener(actionListener);
reset();
}
@@ -430,6 +459,7 @@ public class NullableStuffInspection extends BaseLocalInspectionTool {
myReportNotAnnotatedGetter.setSelected(REPORT_NOT_ANNOTATED_GETTER);
myReportNotAnnotatedSetterParameter.setSelected(REPORT_NOT_ANNOTATED_SETTER_PARAMETER);
myReportAnnotationNotPropagated.setSelected(REPORT_ANNOTATION_NOT_PROPAGATED_TO_OVERRIDERS);
myReportNullsPassedToNonAnnotatedParameter.setSelected(REPORT_NULLS_PASSED_TO_NON_ANNOTATED_METHOD);
}
private void apply() {
@@ -440,6 +470,7 @@ public class NullableStuffInspection extends BaseLocalInspectionTool {
REPORT_NOT_ANNOTATED_SETTER_PARAMETER = myReportNotAnnotatedSetterParameter.isSelected();
REPORT_NOT_ANNOTATED_GETTER = myReportNotAnnotatedGetter.isSelected();
REPORT_ANNOTATION_NOT_PROPAGATED_TO_OVERRIDERS = myReportAnnotationNotPropagated.isSelected();
REPORT_NULLS_PASSED_TO_NON_ANNOTATED_METHOD = myReportNullsPassedToNonAnnotatedParameter.isSelected();
}
}
}
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.codeInspection.nullable.NullableStuffInspection.OptionsPanel">
<grid id="cc1c9" binding="myPanel" layout-manager="GridLayoutManager" row-count="8" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="cc1c9" binding="myPanel" layout-manager="GridLayoutManager" row-count="9" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="69" y="57" width="333" height="208"/>
<xy x="69" y="57" width="333" height="235"/>
</constraints>
<properties/>
<border type="none"/>
@@ -34,7 +34,7 @@
</component>
<vspacer id="c3eef">
<constraints>
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="8" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="52f90" class="javax.swing.JCheckBox" binding="myNAParameterOverridesNN">
@@ -69,6 +69,14 @@
<text resource-bundle="messages/InspectionsBundle" key="inspection.nullable.problems.annotation.not.propagated"/>
</properties>
</component>
<component id="690c8" class="javax.swing.JCheckBox" binding="myReportNullsPassedToNonAnnotatedParameter" default-binding="true">
<constraints>
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="report nulls passed to non annotated parameter"/>
</properties>
</component>
</children>
</grid>
</form>