From 2c90b9faa8211f431334144287aefa2ed9e848a1 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Wed, 22 Oct 2025 12:53:00 +0200 Subject: [PATCH] Java: make the "Unnecessary call to 'super()'" inspection Flexible Constructor Bodies aware (IDEA-381049) GitOrigin-RevId: 4401dc7b2a72ca3273b9eb1cfd0caa0e1438abb3 --- ...UnnecessarySuperConstructorInspection.java | 19 ++++---- ...cessarySuperConstructorInspectionTest.java | 48 +++++++++++++------ 2 files changed, 45 insertions(+), 22 deletions(-) diff --git a/java/java-analysis-impl/src/com/siyeh/ig/style/UnnecessarySuperConstructorInspection.java b/java/java-analysis-impl/src/com/siyeh/ig/style/UnnecessarySuperConstructorInspection.java index d86409bdfc13..fbc9fc9d3bcb 100644 --- a/java/java-analysis-impl/src/com/siyeh/ig/style/UnnecessarySuperConstructorInspection.java +++ b/java/java-analysis-impl/src/com/siyeh/ig/style/UnnecessarySuperConstructorInspection.java @@ -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); diff --git a/java/java-tests/testSrc/com/siyeh/ig/style/UnnecessarySuperConstructorInspectionTest.java b/java/java-tests/testSrc/com/siyeh/ig/style/UnnecessarySuperConstructorInspectionTest.java index 44ce56c80293..b06c25241958 100644 --- a/java/java-tests/testSrc/com/siyeh/ig/style/UnnecessarySuperConstructorInspectionTest.java +++ b/java/java-tests/testSrc/com/siyeh/ig/style/UnnecessarySuperConstructorInspectionTest.java @@ -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