mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Java: make the "Unnecessary call to 'super()'" inspection Flexible Constructor Bodies aware (IDEA-381049)
GitOrigin-RevId: 4401dc7b2a72ca3273b9eb1cfd0caa0e1438abb3
This commit is contained in:
committed by
intellij-monorepo-bot
parent
34be1c7cd0
commit
2c90b9faa8
+11
-8
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2003-2018 Dave Griffith, Bas Leijdekkers
|
||||
* Copyright 2003-2025 Dave Griffith, Bas Leijdekkers
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -21,9 +21,8 @@ import com.intellij.java.syntax.parser.JavaKeywords;
|
||||
import com.intellij.modcommand.ModPsiUpdater;
|
||||
import com.intellij.modcommand.PsiUpdateModCommandQuickFix;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiMethodCallExpression;
|
||||
import com.intellij.psi.PsiReferenceExpression;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
@@ -72,10 +71,14 @@ public final class UnnecessarySuperConstructorInspection extends BaseInspection
|
||||
public void visitMethodCallExpression(@NotNull PsiMethodCallExpression call) {
|
||||
super.visitMethodCallExpression(call);
|
||||
final PsiReferenceExpression methodExpression = call.getMethodExpression();
|
||||
if (methodExpression.isQualified() || !JavaKeywords.SUPER.equals(methodExpression.getReferenceName())) {
|
||||
return;
|
||||
}
|
||||
if (!call.getArgumentList().isEmpty()) {
|
||||
if (methodExpression.isQualified()
|
||||
|| !JavaKeywords.SUPER.equals(methodExpression.getReferenceName())
|
||||
|| !call.getArgumentList().isEmpty()
|
||||
|| !(call.getParent() instanceof PsiExpressionStatement statement)
|
||||
|| !(statement.getParent() instanceof PsiCodeBlock block)
|
||||
|| !(block.getParent() instanceof PsiMethod method)
|
||||
|| !method.isConstructor()
|
||||
|| PsiTreeUtil.getPrevSiblingOfType(statement, PsiStatement.class) != null) {
|
||||
return;
|
||||
}
|
||||
registerError(call);
|
||||
|
||||
+34
-14
@@ -1,4 +1,4 @@
|
||||
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.siyeh.ig.style;
|
||||
|
||||
import com.intellij.codeInspection.InspectionProfileEntry;
|
||||
@@ -11,22 +11,42 @@ import org.jetbrains.annotations.Nullable;
|
||||
public class UnnecessarySuperConstructorInspectionTest extends LightJavaInspectionTestCase {
|
||||
|
||||
public void testQualifiedSuper() {
|
||||
doTest("class Outer {" +
|
||||
" class Super {}" +
|
||||
" class Inner extends Super {" +
|
||||
" Inner(Outer outer) {" +
|
||||
" outer.super();" +
|
||||
" }" +
|
||||
" }" +
|
||||
"}");
|
||||
doTest("""
|
||||
class Outer {
|
||||
class Super {}
|
||||
class Inner extends Super {
|
||||
Inner(Outer outer) {
|
||||
outer.super();
|
||||
}
|
||||
}
|
||||
}""");
|
||||
}
|
||||
|
||||
public void testSimple() {
|
||||
doTest("class Simple {" +
|
||||
" Simple() {" +
|
||||
" /*'super()' is unnecessary*/super()/**/;" +
|
||||
" }" +
|
||||
"}");
|
||||
doTest("""
|
||||
class Simple {
|
||||
Simple() {
|
||||
/*'super()' is unnecessary*/super()/**/;
|
||||
}
|
||||
}""");
|
||||
}
|
||||
|
||||
public void testFlexibleConstructorBody() {
|
||||
doTest("""
|
||||
class Friend {
|
||||
Friend() {
|
||||
System.out.println(0);
|
||||
}
|
||||
}
|
||||
class Flexible extends Friend {
|
||||
|
||||
Flexible() {
|
||||
System.out.println("before");
|
||||
super();
|
||||
System.out.println("after");
|
||||
}
|
||||
}
|
||||
""");
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
Reference in New Issue
Block a user