mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
ObviousNullCheck: defer resolve; isVoidContext
This commit is contained in:
@@ -30,10 +30,13 @@ public class ObviousNullCheckInspection extends AbstractBaseJavaLocalInspectionT
|
||||
return new JavaElementVisitor() {
|
||||
@Override
|
||||
public void visitMethodCallExpression(PsiMethodCallExpression call) {
|
||||
PsiExpression[] args = call.getArgumentList().getExpressions();
|
||||
// Avoid method resolve if no argument is a candidate for obvious non-null warning
|
||||
// (checking this is easier than resolving and calls without arguments are excluded at all)
|
||||
if (!ContainerUtil.exists(args, arg -> getObviouslyNonNullExplanation(PsiUtil.skipParenthesizedExprDown(arg)) != null)) return;
|
||||
NullCheckParameter nullCheckParameter = NullCheckParameter.fromCall(call);
|
||||
if (nullCheckParameter == null) return;
|
||||
if (!(call.getParent() instanceof PsiExpressionStatement || nullCheckParameter.myReturnsParameter)) return;
|
||||
PsiExpression[] args = call.getArgumentList().getExpressions();
|
||||
if (!ExpressionUtils.isVoidContext(call) && !nullCheckParameter.myReturnsParameter) return;
|
||||
if (args.length <= nullCheckParameter.myIndex) return;
|
||||
PsiExpression nullArg = PsiUtil.skipParenthesizedExprDown(args[nullCheckParameter.myIndex]);
|
||||
String explanation = getObviouslyNonNullExplanation(nullArg);
|
||||
|
||||
@@ -24,6 +24,16 @@ abstract class ObviousNullCheck {
|
||||
System.out.println(inferred(<warning descr="Redundant null-check: literal is never null">"foo"</warning>));
|
||||
}
|
||||
|
||||
static String concat(String s, String m) {
|
||||
if(s == null) throw new NullPointerException();
|
||||
return s+m;
|
||||
}
|
||||
|
||||
void testStrings(List<String> list) {
|
||||
list.forEach(s -> concat(<warning descr="Redundant null-check: literal is never null">"Not null!"</warning>, s));
|
||||
list.stream().map(s -> concat("Not null!", s)).forEach(System.out::println);
|
||||
}
|
||||
|
||||
@Contract(value="null -> fail", pure=true)
|
||||
String trim(String s) {
|
||||
return s.trim();
|
||||
|
||||
Reference in New Issue
Block a user