IDEA-153192 (Support @CheckReturnValue on classes and packages)

This commit is contained in:
Bas Leijdekkers
2016-04-22 13:54:00 +02:00
parent 4c6a43292b
commit 0ce4e471d6
3 changed files with 36 additions and 8 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -148,15 +148,29 @@ public class IgnoreResultOfCallInspectionBase extends BaseInspection {
registerMethodCallError(call, aClass);
return;
}
final PsiAnnotation anno2 =
AnnotationUtil.findAnnotationInHierarchy(method, Collections.singleton("javax.annotation.CheckReturnValue"));
if (anno2 != null) {
registerMethodCallError(call, aClass);
}
if (!myMethodMatcher.matches(method)) {
if (!myMethodMatcher.matches(method) && findAnnotationInTree(method, "javax.annotation.CheckReturnValue") == null) {
return;
}
registerMethodCallError(call, aClass);
}
private PsiAnnotation findAnnotationInTree(PsiMethod method, String fqAnnotationName) {
final PsiAnnotation methodAnnotation =
AnnotationUtil.findAnnotationInHierarchy(method, Collections.singleton(fqAnnotationName));
if (methodAnnotation != null) {
return methodAnnotation;
}
final PsiClass aClass = method.getContainingClass();
if (aClass == null) {
return null;
}
final PsiAnnotation classAnnotation = AnnotationUtil.findAnnotation(aClass, fqAnnotationName);
if (classAnnotation != null) {
return classAnnotation;
}
final PsiDirectory directory = aClass.getContainingFile().getContainingDirectory();
final PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(directory);
return AnnotationUtil.findAnnotation(aPackage, fqAnnotationName);
}
}
}
@@ -2,7 +2,8 @@
<body>
Reports any calls to specific methods where the result of that call is ignored.
Both methods specified in the inspection's settings and methods annotated with
<b>org.jetbrains.annotations.Contract(pure=true)</b> are checked.
<b>org.jetbrains.annotations.Contract(pure=true)</b> and <b>javax.annotation.CheckReturnValue</b> are checked.
Methods in a class or package annotated with <b>javax.annotation.CheckReturnValue</b> are also checked.
For many methods, ignoring the result is perfectly
legitimate, but for some methods it is almost certainly an error. Examples of methods where ignoring
the result of a call is likely to be an error include <b>java.io.inputStream.read()</b>,
@@ -82,6 +82,19 @@ public class IgnoreResultOfCallInspectionTest extends LightInspectionTestCase {
"}");
}
public void testJSR305Annotation2() {
doTest("import javax.annotation.CheckReturnValue;" +
"@CheckReturnValue " +
"class A {" +
" static Object a() {" +
" return null;" +
" }" +
" void b() {" +
" /*Result of 'A.a()' is ignored*/a/**/();" +
" }" +
"}");
}
public void testPureMethod() {
doTest """
import org.jetbrains.annotations.Contract;