diff --git a/java/java-impl/src/META-INF/JavaPlugin.xml b/java/java-impl/src/META-INF/JavaPlugin.xml index 0903aa242874..58f85900efdd 100644 --- a/java/java-impl/src/META-INF/JavaPlugin.xml +++ b/java/java-impl/src/META-INF/JavaPlugin.xml @@ -1376,6 +1376,12 @@ bundle="messages.JavaBundle" key="inspection.redundant.explicit.close" implementationClass="com.intellij.codeInspection.RedundantExplicitCloseInspection"/> + + +Reports vararg method calls that use a ternary operator with mixed array and non-array branches.

+When compiled, both branches are wrapped in arrays. As a result, the array branch is turned into +a two-dimensional array, which may indicate a problem.

+The quick-fix wraps the non-array branch in an array to prevent the compiler from doing the conversion.

+Example:

+
+// reported call
+method(condition ? new String[] {"arg1"} : "arg2");
+// after the quick-fix
+method(condition ? new String[] {"arg1"} : new String[] {"arg2"});
+
+ +

New in 2020.3

+ + \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/suspiciousTernaryOperatorInVarargsCall/afterObject.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/suspiciousTernaryOperatorInVarargsCall/afterObject.java new file mode 100644 index 000000000000..333ee51a937c --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/suspiciousTernaryOperatorInVarargsCall/afterObject.java @@ -0,0 +1,16 @@ +// "Replace with 'new Object[]{b}'" "true" + +class Test { + public static void main(String[] args) { + Object[] a = {1, 2}; + Object b = "hello"; + foo(0, a); + foo(0, b); + for (boolean flag : new boolean[]{true, false}) { + foo(0, flag ? a : new Object[]{b}); + foo(0, 1, flag ? a : b); + } + } + static void foo(int x, Object... xs) { + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/suspiciousTernaryOperatorInVarargsCall/afterSerializable.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/suspiciousTernaryOperatorInVarargsCall/afterSerializable.java new file mode 100644 index 000000000000..d77db9921a56 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/suspiciousTernaryOperatorInVarargsCall/afterSerializable.java @@ -0,0 +1,18 @@ +// "Replace with 'new Serializable[]{b}'" "true" + +import java.io.Serializable; + +class Test { + public static void main(String[] args) { + Serializable[] a = {1, 2}; + Serializable b = "hello"; + foo(0, a); + foo(0, b); + for (boolean flag : new boolean[]{true, false}) { + foo(0, flag ? a : new Serializable[]{b}); + foo(0, 1, flag ? a : b); + } + } + static void foo(int x, Serializable... xs) { + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/suspiciousTernaryOperatorInVarargsCall/beforeObject.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/suspiciousTernaryOperatorInVarargsCall/beforeObject.java new file mode 100644 index 000000000000..9175f76a9bb8 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/suspiciousTernaryOperatorInVarargsCall/beforeObject.java @@ -0,0 +1,16 @@ +// "Replace with 'new Object[]{b}'" "true" + +class Test { + public static void main(String[] args) { + Object[] a = {1, 2}; + Object b = "hello"; + foo(0, a); + foo(0, b); + for (boolean flag : new boolean[]{true, false}) { + foo(0, flag ? a : b); + foo(0, 1, flag ? a : b); + } + } + static void foo(int x, Object... xs) { + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/suspiciousTernaryOperatorInVarargsCall/beforeSerializable.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/suspiciousTernaryOperatorInVarargsCall/beforeSerializable.java new file mode 100644 index 000000000000..183de7d4ba3d --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/suspiciousTernaryOperatorInVarargsCall/beforeSerializable.java @@ -0,0 +1,18 @@ +// "Replace with 'new Serializable[]{b}'" "true" + +import java.io.Serializable; + +class Test { + public static void main(String[] args) { + Serializable[] a = {1, 2}; + Serializable b = "hello"; + foo(0, a); + foo(0, b); + for (boolean flag : new boolean[]{true, false}) { + foo(0, flag ? a : b); + foo(0, 1, flag ? a : b); + } + } + static void foo(int x, Serializable... xs) { + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/SuspiciousTernaryOperatorInVarargsCallInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/SuspiciousTernaryOperatorInVarargsCallInspectionTest.java new file mode 100644 index 000000000000..b7058afadfbb --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/SuspiciousTernaryOperatorInVarargsCallInspectionTest.java @@ -0,0 +1,22 @@ +// Copyright 2000-2020 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. +package com.intellij.java.codeInsight.daemon.quickFix; + +import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase; +import com.intellij.codeInspection.LocalInspectionTool; +import com.intellij.codeInspection.SuspiciousTernaryOperatorInVarargsCallInspection; +import org.jetbrains.annotations.NotNull; + +public class SuspiciousTernaryOperatorInVarargsCallInspectionTest extends LightQuickFixParameterizedTestCase { + + @Override + protected LocalInspectionTool @NotNull [] configureLocalInspectionTools() { + return new LocalInspectionTool[]{ + new SuspiciousTernaryOperatorInVarargsCallInspection() + }; + } + + @Override + protected String getBasePath() { + return "/codeInsight/daemonCodeAnalyzer/quickFix/suspiciousTernaryOperatorInVarargsCall"; + } +} diff --git a/java/openapi/resources/messages/JavaBundle.properties b/java/openapi/resources/messages/JavaBundle.properties index 14afb914a6ac..9dda8170c88a 100644 --- a/java/openapi/resources/messages/JavaBundle.properties +++ b/java/openapi/resources/messages/JavaBundle.properties @@ -466,6 +466,9 @@ inspection.handle.signature.use.method.fix.family.name=Use one of method overloa inspection.handle.signature.use.method.fix.name=Use method ''{0}'' inspection.idempotent.loop.body=Idempotent loop body inspection.illegal.character=Illegal character +inspection.suspicious.ternary.in.varargs.display.name=Suspicious ternary operator in varargs method call +inspection.suspicious.ternary.in.varargs.description=Ternary operator in varargs call contains array and non-array branches +inspection.suspicious.ternary.in.varargs.quickfix=Wrap in array initializer inspection.insert.literal.underscores.display.name=Unreadable numeric literal inspection.insert.literal.underscores.family.name=Insert underscores into numeric literal inspection.javadoc.dialog.title=Edit Additional Javadoc Tags