mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 13:02:30 +07:00
[java-inspections] IDEA-356982 PsiImplicitClass is created for certain invalid code and renders the whole file gray
- skip implicit classes in unused analysis GitOrigin-RevId: 6eb880108723b3623b0cc449d76f9f87942fc8b7
This commit is contained in:
committed by
intellij-monorepo-bot
parent
941f396ef2
commit
ccf2a6bae5
@@ -246,6 +246,8 @@ public class UnusedDeclarationInspectionBase extends GlobalInspectionTool {
|
||||
return true;
|
||||
}
|
||||
|
||||
if(element instanceof PsiImplicitClass) return true;
|
||||
|
||||
return owner instanceof RefClass &&
|
||||
(isAddAppletEnabled() && ((RefClass)owner).isApplet()
|
||||
|| isAddServletEnabled() && ((RefClass)owner).isServlet());
|
||||
@@ -264,6 +266,7 @@ public class UnusedDeclarationInspectionBase extends GlobalInspectionTool {
|
||||
}
|
||||
Project project = element.getProject();
|
||||
if (element instanceof PsiClass aClass) {
|
||||
if (element instanceof PsiImplicitClass) return true;
|
||||
JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
|
||||
if (isAddAppletEnabled()) {
|
||||
PsiClass applet = psiFacade.findClass("java.applet.Applet", GlobalSearchScope.allScope(project));
|
||||
|
||||
@@ -53,7 +53,7 @@ import java.util.Map;
|
||||
import static com.intellij.codeInspection.options.OptPane.*;
|
||||
|
||||
/**
|
||||
* Local counterpart of {@link com.intellij.codeInspection.deadCode.UnusedDeclarationInspectionBase}
|
||||
* Local counterpart of {@link UnusedDeclarationInspectionBase}
|
||||
*/
|
||||
public final class UnusedSymbolLocalInspection extends AbstractBaseJavaLocalInspectionTool {
|
||||
@NonNls public static final String SHORT_NAME = HighlightInfoType.UNUSED_SYMBOL_SHORT_NAME;
|
||||
@@ -355,6 +355,7 @@ public final class UnusedSymbolLocalInspection extends AbstractBaseJavaLocalInsp
|
||||
|
||||
@Override
|
||||
public void visitClass(@NotNull PsiClass aClass) {
|
||||
if (aClass instanceof PsiImplicitClass) return;
|
||||
String acceptedVisibility = aClass.getContainingClass() == null ? getClassVisibility()
|
||||
: getInnerClassVisibility();
|
||||
if (!compareVisibilities(aClass, acceptedVisibility)) return;
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
<error descr="Implicitly declared class contains no 'main' method"><error descr="Package statement is not allowed for implicitly declared class">package p1;</error>
|
||||
|
||||
class <warning descr="Class 'Demo' is never used">Demo</warning>{
|
||||
public static void <warning descr="Method 't1()' is never used">t1</warning>(){}
|
||||
public static void <warning descr="Method 't2()' is never used">t2</warning>(){}
|
||||
public static void <warning descr="Method 't3()' is never used">t3</warning>(){}
|
||||
public static void <warning descr="Method 't4()' is never used">t4</warning>(){
|
||||
if<error descr="'(' expected"> </error>}
|
||||
}
|
||||
public static void <warning descr="Method 't5()' is never used">t5</warning>(){}
|
||||
public static void <warning descr="Method 't6()' is never used">t6</warning>(){}
|
||||
<error descr="'class' or 'interface' expected">}</error></error>
|
||||
@@ -0,0 +1,53 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
<problem>
|
||||
<file>Test.java</file>
|
||||
<line>4</line>
|
||||
<highlighted_element>t1</highlighted_element>
|
||||
<problem_class id="unused" severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">unused declaration</problem_class>
|
||||
<description>Method is never used.</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>Test.java</file>
|
||||
<line>5</line>
|
||||
<highlighted_element>t2</highlighted_element>
|
||||
<problem_class id="unused" severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">unused declaration</problem_class>
|
||||
<description>Method is never used.</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>Test.java</file>
|
||||
<line>6</line>
|
||||
<highlighted_element>t3</highlighted_element>
|
||||
<problem_class id="unused" severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">unused declaration</problem_class>
|
||||
<description>Method is never used.</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>Test.java</file>
|
||||
<line>7</line>
|
||||
<highlighted_element>t4</highlighted_element>
|
||||
<problem_class id="unused" severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">unused declaration</problem_class>
|
||||
<description>Method is never used.</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>Test.java</file>
|
||||
<line>10</line>
|
||||
<highlighted_element>t5</highlighted_element>
|
||||
<problem_class id="unused" severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">unused declaration</problem_class>
|
||||
<description>Method is never used.</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>Test.java</file>
|
||||
<line>11</line>
|
||||
<highlighted_element>t6</highlighted_element>
|
||||
<problem_class id="unused" severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">unused declaration</problem_class>
|
||||
<description>Method is never used.</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>Test.java</file>
|
||||
<line>3</line>
|
||||
<highlighted_element>Demo</highlighted_element>
|
||||
<problem_class id="unused" severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">unused declaration</problem_class>
|
||||
<description>Class is never used.</description>
|
||||
</problem>
|
||||
</problems>
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package p1;
|
||||
|
||||
class Demo{
|
||||
public static void t1(){}
|
||||
public static void t2(){}
|
||||
public static void t3(){}
|
||||
public static void t4(){
|
||||
if}
|
||||
}
|
||||
public static void t5(){}
|
||||
public static void t6(){}
|
||||
}
|
||||
@@ -6,9 +6,11 @@ import com.intellij.codeInsight.CodeInsightBundle;
|
||||
import com.intellij.codeInsight.daemon.ImplicitUsageProvider;
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection;
|
||||
import com.intellij.pom.java.JavaFeature;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiField;
|
||||
import com.intellij.psi.PsiNamedElement;
|
||||
import com.intellij.testFramework.IdeaTestUtil;
|
||||
import com.intellij.testFramework.LightProjectDescriptor;
|
||||
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -56,6 +58,13 @@ public class LightUnusedHighlightingFixtureTest extends LightJavaCodeInsightFixt
|
||||
myFixture.checkResultByFile(testFileName + "_after.java", true);
|
||||
}
|
||||
|
||||
public void testBrokenClassToImplicitClass() {
|
||||
IdeaTestUtil.withLevel(getModule(), JavaFeature.IMPLICIT_CLASSES.getMinimumLevel(), ()->{
|
||||
myFixture.configureByFile(getTestName(false) + ".java");
|
||||
myFixture.checkHighlighting();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return JavaTestUtil.getRelativeJavaTestDataPath() + "/codeInsight/daemonCodeAnalyzer/advFixture";
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
package com.intellij.java.codeInspection;
|
||||
|
||||
import com.intellij.codeInspection.ex.EntryPointsManagerBase;
|
||||
import com.intellij.pom.java.JavaFeature;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.testFramework.IdeaTestUtil;
|
||||
|
||||
@@ -287,6 +288,12 @@ public class UnusedDeclarationInspectionTest extends AbstractUnusedDeclarationTe
|
||||
});
|
||||
}
|
||||
|
||||
public void testBrokenClassToImplicitClass() {
|
||||
IdeaTestUtil.withLevel(getModule(), JavaFeature.IMPLICIT_CLASSES.getMinimumLevel(), () -> {
|
||||
doTest();
|
||||
});
|
||||
}
|
||||
|
||||
private void doTest5() {
|
||||
IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_1_5, () -> doTest());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user