[java-inspections] IDEA-289267 Change highlight to unused code on RedundantArrayForVarargsCall and RedundantLambdaParameterType

Also, new tests

GitOrigin-RevId: 5dbb45fc400ba8de0a360e05a4eaf0aee07e0e83
This commit is contained in:
Fabrice Tiercelin
2023-06-16 20:28:52 +00:00
committed by intellij-monorepo-bot
parent 1cae1fd6b5
commit c2b459a91e
16 changed files with 229 additions and 115 deletions
@@ -1,4 +1,4 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInspection.lambda;
import com.intellij.codeInsight.intention.impl.RemoveRedundantParameterTypesFix;
@@ -6,25 +6,27 @@ import com.intellij.codeInspection.AbstractBaseJavaLocalInspectionTool;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.java.JavaBundle;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.JavaElementVisitor;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiLambdaExpression;
import com.intellij.psi.PsiParameterList;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;
public class RedundantLambdaParameterTypeInspection extends AbstractBaseJavaLocalInspectionTool {
public static final Logger LOG = Logger.getInstance(RedundantLambdaParameterTypeInspection.class);
@Override
public @NotNull PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new JavaElementVisitor() {
@Override
public void visitParameterList(@NotNull PsiParameterList parameterList) {
super.visitParameterList(parameterList);
if (parameterList.getParent() instanceof PsiLambdaExpression &&
RemoveRedundantParameterTypesFix.isApplicable(parameterList)) {
holder.registerProblem(parameterList, JavaBundle.message("inspection.message.lambda.parameter.type.is.redundant"),
new RemoveRedundantParameterTypesFix((PsiLambdaExpression)parameterList.getParent()));
for (PsiParameter parameter : parameterList.getParameters()) {
if (parameter.getTypeElement() != null) {
holder.registerProblem(parameter.getTypeElement(), JavaBundle.message("inspection.message.lambda.parameter.type.is.redundant"),
new RemoveRedundantParameterTypesFix((PsiLambdaExpression)parameterList.getParent()));
}
}
}
}
};
@@ -1,9 +1,10 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInspection.miscGenerics;
import com.intellij.codeInspection.*;
import com.intellij.java.JavaBundle;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.CommonJavaRefactoringUtil;
@@ -41,7 +42,7 @@ public class RedundantArrayForVarargsCallInspection extends AbstractBaseJavaLoca
private static final CallMatcher LOGGER_MESSAGE_CALL = exactInstanceCall("org.slf4j.Logger", LOGGER_NAMES)
.parameterTypes(String.class.getName(), "java.lang.Object...");
private static final LocalQuickFix myQuickFixAction = new MyQuickFix();
private static final LocalQuickFix redundantArrayForVarargsCallFixAction = new RedundantArrayForVarargsCallFix();
private @NotNull final ProblemsHolder myHolder;
@@ -68,19 +69,33 @@ public class RedundantArrayForVarargsCallInspection extends AbstractBaseJavaLoca
!CommonJavaRefactoringUtil.isSafeToFlattenToVarargsCall(expression, initializers)) {
return;
}
final String message = JavaBundle.message("inspection.redundant.array.creation.for.varargs.call.descriptor");
PsiExpressionList argumentList = expression.getArgumentList();
PsiExpression[] args = Objects.requireNonNull(argumentList).getExpressions();
myHolder.registerProblem(Objects.requireNonNull(PsiUtil.skipParenthesizedExprDown(args[args.length - 1])), message, myQuickFixAction);
PsiExpression arrayCreation = Objects.requireNonNull(PsiUtil.skipParenthesizedExprDown(args[args.length - 1]));
if (!(arrayCreation instanceof PsiNewExpression)) return;
final String message = JavaBundle.message("inspection.redundant.array.creation.for.varargs.call.descriptor");
PsiArrayInitializerExpression arrayInitializer = ((PsiNewExpression)arrayCreation).getArrayInitializer();
if (arrayInitializer == null) {
myHolder.registerProblem(
arrayCreation,
message,
redundantArrayForVarargsCallFixAction);
} else {
myHolder.registerProblem(
arrayCreation,
new TextRange(0, arrayInitializer.getStartOffsetInParent()),
message,
redundantArrayForVarargsCallFixAction);
}
}
private static final class MyQuickFix implements LocalQuickFix {
private static final class RedundantArrayForVarargsCallFix implements LocalQuickFix {
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiNewExpression arrayCreation = (PsiNewExpression)descriptor.getPsiElement();
if (arrayCreation == null) return;
CommonJavaRefactoringUtil.inlineArrayCreationForVarargs(arrayCreation);
PsiElement arrayCreation = descriptor.getPsiElement();
if (!(arrayCreation instanceof PsiNewExpression)) return;
CommonJavaRefactoringUtil.inlineArrayCreationForVarargs((PsiNewExpression)arrayCreation);
}
@Override
+2 -2
View File
@@ -1524,7 +1524,7 @@
<localInspection groupPath="Java" language="JAVA" shortName="RedundantLambdaParameterType"
groupBundle="messages.InspectionsBundle"
groupKey="group.names.declaration.redundancy" enabledByDefault="true" level="INFORMATION"
implementationClass="com.intellij.codeInspection.lambda.RedundantLambdaParameterTypeInspection"
implementationClass="com.intellij.codeInspection.lambda.RedundantLambdaParameterTypeInspection" editorAttributes="NOT_USED_ELEMENT_ATTRIBUTES"
key="inspection.redundant.lambda.parameter.type.display.name" bundle="messages.JavaBundle"/>
<localInspection groupPath="Java" language="JAVA" shortName="ReplaceInefficientStreamCount"
groupBundle="messages.InspectionsBundle"
@@ -1814,7 +1814,7 @@
key="inspection.redundant.array.creation.display.name"
groupKey="group.names.verbose.or.redundant.code.constructs" groupBundle="messages.InspectionsBundle"
enabledByDefault="true" level="WARNING" cleanupTool="true"
implementationClass="com.intellij.codeInspection.miscGenerics.RedundantArrayForVarargsCallInspection"/>
editorAttributes="NOT_USED_ELEMENT_ATTRIBUTES" implementationClass="com.intellij.codeInspection.miscGenerics.RedundantArrayForVarargsCallInspection"/>
<localInspection groupPath="Java" language="JAVA" shortName="CharsetObjectCanBeUsed" bundle="messages.JavaBundle"
key="inspection.charset.object.can.be.used.display.name"
groupKey="group.names.code.style.issues" groupBundle="messages.InspectionsBundle"
@@ -0,0 +1,14 @@
// "Remove explicit array creation" "true"
import java.util.Arrays;
import java.util.List;
import java.util.Date;
public class RedundantArrayForVarargsCall {
{
try {
String.class.getConstructor(String.class);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@@ -0,0 +1,10 @@
// "Remove explicit array creation" "true"
import java.util.Arrays;
import java.util.List;
import java.util.Date;
public class RedundantArrayForVarargsCall {
public List<Date> severalValues() {
return Arrays.asList(new Date(), new Date());
}
}
@@ -0,0 +1,14 @@
// "Remove explicit array creation" "true"
import java.util.Arrays;
import java.util.List;
import java.util.Date;
public class RedundantArrayForVarargsCall {
{
try {
String.class.getConstructor(new Class<caret>[]{String.class});
} catch (Exception e) {
e.printStackTrace();
}
}
}
@@ -0,0 +1,10 @@
// "Remove explicit array creation" "true"
import java.util.Arrays;
import java.util.List;
import java.util.Date;
public class RedundantArrayForVarargsCall {
public List<Date> severalValues() {
return Arrays.asList(new <caret>Date[] {new Date(), new Date()});
}
}
@@ -1,6 +1,6 @@
public enum CheckEnumConstant {
A(<warning descr="Redundant array creation for calling varargs method">new String[]{"1", "2"}</warning>);
A(<warning descr="Redundant array creation for calling varargs method">new String[]</warning>{"1", "2"});
CheckEnumConstant(String... ss) {
}
@@ -7,7 +7,7 @@ class Generic {
B b = new B();
C<A> l = asC(new A[]{b});
A a = new A();
C<A> m = asC(<warning descr="Redundant array creation for calling varargs method">new A[]{a}</warning>);
C<A> m = asC(<warning descr="Redundant array creation for calling varargs method">new A[]</warning>{a});
}
public static <T> C<T> asC(T... ts) {
@@ -17,6 +17,6 @@ class Generic {
class C<T> {}
void m() {
System.out.println(String.format("%s %s", <warning descr="Redundant array creation for calling varargs method">new Object[] {"Z", "X"}</warning>));
System.out.println(String.format("%s %s", <warning descr="Redundant array creation for calling varargs method">new Object[] </warning>{"Z", "X"}));
}
}
@@ -15,6 +15,6 @@ public class IDEADEV15215 {
public static void extra(String... args) {
extra(<warning descr="Redundant array creation for calling varargs method">new String[]{"vvv","aaa"}</warning>);
extra(<warning descr="Redundant array creation for calling varargs method">new String[]</warning>{"vvv","aaa"});
}
}
@@ -12,8 +12,8 @@ public class NestedArray {
public void main(String[] args) {
String[] params = new String[]{ "0", "1" };
method(new Object[]{params});
method(<warning descr="Redundant array creation for calling varargs method">new Object[]{"2", params}</warning>);
method(<warning descr="Redundant array creation for calling varargs method">new Object[]{params, params}</warning>);
method(<warning descr="Redundant array creation for calling varargs method">new Object[]</warning>{"2", params});
method(<warning descr="Redundant array creation for calling varargs method">new Object[]</warning>{params, params});
}
public static Collection quickFixErrorIDEA165068() {
@@ -26,11 +26,11 @@ public class NestedArray {
}
public static Collection quickFixError2() {
return Arrays.asList(<warning descr="Redundant array creation for calling varargs method">new String[][]{
return Arrays.asList(<warning descr="Redundant array creation for calling varargs method">new String[][]</warning>{
new String[] {"bla", " bla"},
new String[] {"bla", " bla"},
new String[] {"bla", " bla"},
new String[] {"bla", " bla"},
}</warning>);
});
}
}
@@ -1,7 +1,7 @@
public class RawArray {
{
try {
String.class.getConstructor(<warning descr="Redundant array creation for calling varargs method">new Class[]{String.class}</warning>);
String.class.getConstructor(<warning descr="Redundant array creation for calling varargs method">new Class[]</warning>{String.class});
} catch (Exception e) {
e.printStackTrace();
}
@@ -0,0 +1,12 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
import java.util.List;
import java.util.Map;
class C {
void singleParameter(List<String> list) {
list.forEach((<warning descr="Lambda parameter type is redundant">String</warning> s) -> System.out.println("#" + s));
}
void twoParameters(Map<String, Integer> map) {
map.forEach((<warning descr="Lambda parameter type is redundant">String</warning> s, <warning descr="Lambda parameter type is redundant">Integer</warning> i) -> System.out.println(s + "=" + i));
}
}
@@ -0,0 +1,21 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.java.codeInspection;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.miscGenerics.RedundantArrayForVarargsCallInspection;
import org.jetbrains.annotations.NotNull;
public class RedundantArrayForVarargsCallFixTest extends LightQuickFixParameterizedTestCase {
@Override
protected LocalInspectionTool @NotNull [] configureLocalInspectionTools() {
return new LocalInspectionTool[]{
new RedundantArrayForVarargsCallInspection()
};
}
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/redundantArrayForVarargsCall";
}
}
@@ -0,0 +1,97 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.java.codeInspection;
import com.intellij.JavaTestUtil;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInspection.lambda.RedundantLambdaParameterTypeInspection;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class RedundantLambdaParameterTypeFixTest extends LightJavaCodeInsightFixtureTestCase {
private static final String ourIntentionName = "Remove redundant parameter types";
@Override
protected String getBasePath() {
return JavaTestUtil.getRelativeJavaTestDataPath() + "/codeInspection/redundantLambdaParameterType";
}
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return JAVA_8;
}
@Override
protected void setUp() throws Exception {
super.setUp();
myFixture.enableInspections(new RedundantLambdaParameterTypeInspection());
}
public void testAssignment() {
doTest();
}
public void testAssignmentNoParams() {
assertIntentionNotAvailable();
}
public void testAssignmentNoTypes() {
assertIntentionNotAvailable();
}
public void testAtVarargPlace() {
assertIntentionNotAvailable();
}
public void testCallNoTypeArgs() {
assertIntentionNotAvailable();
}
public void testCallNoTypeArgs1() {
assertIntentionNotAvailable();
}
public void testCallNoTypeArgs2() {
assertIntentionNotAvailable();
}
public void testCallWithTypeArgs() {
doTest();
}
public void testInferredFromOtherArgs() {
doTest();
}
public void testNoSelfTypeParam() {
doTest();
}
public void testTypeParam() {
assertIntentionNotAvailable();
}
public void testInChain() { // disabled till the functionality is available
doTest();
}
public void testNotApplicableDueToChainedCall() {
assertIntentionNotAvailable();
}
private void doTest() {
myFixture.configureByFiles(getTestName(false) + ".java");
final IntentionAction singleIntention = myFixture.findSingleIntention(ourIntentionName);
myFixture.launchAction(singleIntention);
myFixture.checkResultByFile(getTestName(false) + ".java", getTestName(false) + "_after.java", true);
}
private void assertIntentionNotAvailable() {
myFixture.configureByFiles(getTestName(false) + ".java");
final List<IntentionAction> intentionActions = myFixture.filterAvailableIntentions(ourIntentionName);
assertEmpty(ourIntentionName + " is not expected", intentionActions);
}
}
@@ -1,35 +1,16 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.java.codeInspection;
import com.intellij.JavaTestUtil;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInspection.lambda.RedundantLambdaParameterTypeInspection;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class RedundantLambdaParameterTypeInspectionTest extends LightJavaCodeInsightFixtureTestCase {
private static final String ourIntentionName = "Remove redundant parameter types";
@Override
protected String getBasePath() {
return JavaTestUtil.getRelativeJavaTestDataPath() + "/codeInspection/redundantLambdaParameterType";
return JavaTestUtil.getRelativeJavaTestDataPath() + "/inspection/redundantLambdaParameterType";
}
@NotNull
@@ -38,74 +19,12 @@ public class RedundantLambdaParameterTypeInspectionTest extends LightJavaCodeIns
return JAVA_8;
}
@Override
protected void setUp() throws Exception {
super.setUp();
myFixture.enableInspections(new RedundantLambdaParameterTypeInspection());
}
public void testAssignment() {
doTest();
}
public void testAssignmentNoParams() {
assertIntentionNotAvailable();
}
public void testAssignmentNoTypes() {
assertIntentionNotAvailable();
}
public void testAtVarargPlace() {
assertIntentionNotAvailable();
}
public void testCallNoTypeArgs() {
assertIntentionNotAvailable();
}
public void testCallNoTypeArgs1() {
assertIntentionNotAvailable();
}
public void testCallNoTypeArgs2() {
assertIntentionNotAvailable();
}
public void testCallWithTypeArgs() {
doTest();
}
public void testInferredFromOtherArgs() {
doTest();
}
public void testNoSelfTypeParam() {
doTest();
}
public void testTypeParam() {
assertIntentionNotAvailable();
}
public void testInChain() { // disabled till the functionality is available
doTest();
}
public void testNotApplicableDueToChainedCall() {
assertIntentionNotAvailable();
}
private void doTest() {
myFixture.configureByFiles(getTestName(false) + ".java");
final IntentionAction singleIntention = myFixture.findSingleIntention(ourIntentionName);
myFixture.launchAction(singleIntention);
myFixture.checkResultByFile(getTestName(false) + ".java", getTestName(false) + "_after.java", true);
myFixture.enableInspections(new RedundantLambdaParameterTypeInspection() {});
myFixture.testHighlighting(getTestName(false) + ".java");
}
private void assertIntentionNotAvailable() {
myFixture.configureByFiles(getTestName(false) + ".java");
final List<IntentionAction> intentionActions = myFixture.filterAvailableIntentions(ourIntentionName);
assertEmpty(ourIntentionName + " is not expected", intentionActions);
public void testRedundantLambdaParameterType() {
doTest();
}
}
}