[java-highlighting] IDEA-365996 Constructor in an implicitly declared class is not highlighted as error

GitOrigin-RevId: 2de45c452608cea2ab314511060ac733259a8c18
This commit is contained in:
Mikhail Pyltsin
2025-01-22 10:29:22 +00:00
committed by intellij-monorepo-bot
parent 75f7bbf5da
commit c28f1d89cc
6 changed files with 41 additions and 1 deletions
@@ -1734,6 +1734,22 @@ public final class HighlightMethodUtil {
return false;
}
static HighlightInfo.@Nullable Builder checkConstructorInImplicitClass(@NotNull PsiMethod method) {
if (!method.isConstructor()) {
return null;
}
if (!(method.getContainingClass() instanceof PsiImplicitClass)) {
return null;
}
String description = JavaErrorBundle.message("implicit.class.with.explicit.constructor");
TextRange textRange = HighlightNamesUtil.getMethodDeclarationTextRange(method);
HighlightInfo.Builder builder =
HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description);
IntentionAction action = QuickFixFactory.getInstance().createDeleteFix(method);
builder.registerFix(action, null, null, null, null);
return builder;
}
private static final class ReturnModel {
final PsiReturnStatement myStatement;
final PsiType myType;
@@ -776,6 +776,7 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
@Override
public void visitMethod(@NotNull PsiMethod method) {
super.visitMethod(method);
if (!hasErrorResults()) add(HighlightMethodUtil.checkConstructorInImplicitClass(method));
if (!hasErrorResults()) add(HighlightControlFlowUtil.checkUnreachableStatement(method.getBody()));
if (!hasErrorResults()) add(HighlightMethodUtil.checkConstructorHandleSuperClassExceptions(method));
if (!hasErrorResults()) add(HighlightMethodUtil.checkRecordAccessorDeclaration(method));
@@ -451,4 +451,5 @@ too.many.array.dimensions=Too many array dimensions
error.cannot.infer.pattern.type=Cannot infer pattern type: {0}
error.extra.semicolons.between.import.statements.not.allowed=Extra semicolons between import statements are not allowed
remove.unused.imports.quickfix.text=Remove unused imports
incomplete.project.state.pending.reference=Not resolved until the project is fully loaded
incomplete.project.state.pending.reference=Not resolved until the project is fully loaded
implicit.class.with.explicit.constructor=Explicit constructor in implicitly declared class is not allowed
@@ -0,0 +1,7 @@
void main(String[] args) {
System.out.println("AAAAA");
}
<error descr="Explicit constructor in implicitly declared class is not allowed">public ImplicitClassConstructorNoParam()</error> {
System.out.println("BBBBB");
}
@@ -0,0 +1,7 @@
void main(String[] args) {
System.out.println("AAAAA");
}
<error descr="Explicit constructor in implicitly declared class is not allowed">public ImplicitClassConstructorParam(String a)</error> {
System.out.println("BBBBB");
}
@@ -164,6 +164,14 @@ class ImplicitClassHighlightingTest : LightJavaCodeInsightFixtureTestCase() {
})
}
fun testImplicitClassConstructorNoParam() {
doTest()
}
fun testImplicitClassConstructorParam() {
doTest()
}
private fun doTest() {
myFixture.configureByFile(getTestName(false) + ".java")
myFixture.checkHighlighting()