prohibit TestOnly & VisibleForTesting on a single method

because it makes little sense but confuses the inspection and important warnings are missed

GitOrigin-RevId: c4f52ee3089219bca922ed39886ef2b6ed530d8c
This commit is contained in:
Peter Gromov
2019-12-13 13:32:28 +01:00
committed by intellij-monorepo-bot
parent 1a50089f3b
commit 0080177eca
3 changed files with 35 additions and 7 deletions

View File

@@ -3,10 +3,7 @@ package com.intellij.codeInspection.testOnly;
import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInsight.TestFrameworks;
import com.intellij.codeInspection.AbstractBaseJavaLocalInspectionTool;
import com.intellij.codeInspection.InspectionsBundle;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.codeInspection.*;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VirtualFile;
@@ -81,6 +78,24 @@ public class TestOnlyInspection extends AbstractBaseJavaLocalInspectionTool {
PsiElement resolve = reference.resolve();
if (resolve instanceof PsiClass) validate(reference, (PsiClass)resolve, h);
}
@Override
public void visitElement(@NotNull PsiElement element) {
if (element instanceof PsiMember) {
PsiAnnotation vft = findVisibleForTestingAnnotation((PsiMember)element);
if (vft != null && isDirectlyTestOnly((PsiMember)element)) {
PsiElement toHighlight = null;
if (element instanceof PsiNameIdentifierOwner) {
toHighlight = ((PsiNameIdentifierOwner)element).getNameIdentifier();
}
if (toHighlight == null) {
toHighlight = element;
}
h.registerProblem(toHighlight, "@VisibleForTesting makes little sense on @TestOnly code", new RemoveAnnotationQuickFix(vft, (PsiModifierListOwner)element));
}
}
super.visitElement(element);
}
};
}
@@ -156,8 +171,11 @@ public class TestOnlyInspection extends AbstractBaseJavaLocalInspectionTool {
private static boolean isAnnotatedAsTestOnly(@Nullable PsiMember m) {
if (m == null) return false;
return AnnotationUtil.isAnnotated(m, AnnotationUtil.TEST_ONLY, CHECK_EXTERNAL)
|| isAnnotatedAsTestOnly(m.getContainingClass());
return isDirectlyTestOnly(m) || isAnnotatedAsTestOnly(m.getContainingClass());
}
private static boolean isDirectlyTestOnly(@NotNull PsiMember m) {
return AnnotationUtil.isAnnotated(m, AnnotationUtil.TEST_ONLY, CHECK_EXTERNAL);
}
private static boolean isInsideTestClass(PsiElement e) {