[java-highlighting] IJ-CR-165049 IDEA-374019 Don't show error about 'main' method if there is an error with braces

- don't highlight if there is package statement
- skip duplicated nested class for implicit classes

GitOrigin-RevId: eadb287b7e52bfedd04340b8841597b9d6839ee5
This commit is contained in:
Mikhail Pyltsin
2025-06-09 13:42:30 +02:00
committed by intellij-monorepo-bot
parent ab4901834d
commit 708f2a6328
6 changed files with 28 additions and 5 deletions

View File

@@ -197,7 +197,10 @@ final class ClassChecker {
}
parent = element;
if (element instanceof PsiDeclarationStatement) element = PsiTreeUtil.getChildOfType(element, PsiClass.class);
if (element instanceof PsiDeclarationStatement){ element = PsiTreeUtil.getChildOfType(element, PsiClass.class);}
if (element instanceof PsiImplicitClass) return;
if (element instanceof PsiClass psiClass && name.equals(psiClass.getName())) {
myVisitor.report(JavaErrorKinds.CLASS_DUPLICATE.create(aClass, psiClass));
}
@@ -403,6 +406,8 @@ final class ClassChecker {
PsiMethod[] methods = implicitClass.getMethods();
boolean hasMainMethod = ContainerUtil.exists(methods, method -> "main".equals(method.getName()) && PsiMethodUtil.isMainMethod(method));
if (!hasMainMethod) {
//don't show errors if there is a package, this package will be highlighted
if (file.getPackageStatement() != null) return;
//don't show errors if the file contains broken {}
if(hasErrorElementWithBraces(file)) return;
myVisitor.report(JavaErrorKinds.CLASS_IMPLICIT_NO_MAIN_METHOD.create(file, implicitClass));

View File

@@ -1,5 +1,3 @@
package com.example;
public class A {
}

View File

@@ -0,0 +1,7 @@
<error descr="Package statement is not allowed for implicitly declared class">package a;</error>
int ab;
class AB {
}

View File

@@ -0,0 +1,5 @@
<error descr="Implicitly declared class contains no 'main' method">public class SameNameInnerClass {
}
int a = 1;</error>

View File

@@ -1,5 +1,5 @@
<error descr="Implicitly declared class contains no 'main' method"><error descr="Package statement is not allowed for implicitly declared class">package pack.bar;</error>
<error descr="Package statement is not allowed for implicitly declared class">package pack.bar;</error>
void foo() {
}</error>
}

View File

@@ -176,6 +176,14 @@ class ImplicitClassHighlightingTest : LightJavaCodeInsightFixtureTestCase() {
doTest()
}
fun testBrokenFileNoHighlightingWithPackage() {
doTest()
}
fun testSameNameInnerClass() {
doTest()
}
private fun doTest() {
myFixture.configureByFile(getTestName(false) + ".java")
myFixture.checkHighlighting()