mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Support parentheses, fix bug when the array branch has a primitive array, more tests: IDEA-244144
GitOrigin-RevId: 4c644d8b1e11869e4f3853763d09d7e0b91b5740
This commit is contained in:
committed by
intellij-monorepo-bot
parent
25c8da92e3
commit
9845c0a0c2
+6
-4
@@ -4,12 +4,13 @@ package com.intellij.codeInspection;
|
||||
import com.intellij.java.JavaBundle;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import com.siyeh.ig.psiutils.ClassUtils;
|
||||
import com.siyeh.ig.psiutils.CommentTracker;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.java.generate.psi.PsiAdapter;
|
||||
|
||||
public class SuspiciousTernaryOperatorInVarargsCallInspection extends AbstractBaseJavaLocalInspectionTool {
|
||||
@NotNull
|
||||
@@ -25,7 +26,7 @@ public class SuspiciousTernaryOperatorInVarargsCallInspection extends AbstractBa
|
||||
if (argumentList.isEmpty()) return;
|
||||
|
||||
final PsiExpression[] args = argumentList.getExpressions();
|
||||
PsiExpression varargsExpression = ArrayUtil.getLastElement(args);
|
||||
PsiExpression varargsExpression = PsiUtil.skipParenthesizedExprDown(ArrayUtil.getLastElement(args));
|
||||
final PsiConditionalExpression conditional = ObjectUtils.tryCast(varargsExpression, PsiConditionalExpression.class);
|
||||
if (conditional == null) return;
|
||||
|
||||
@@ -45,14 +46,15 @@ public class SuspiciousTernaryOperatorInVarargsCallInspection extends AbstractBa
|
||||
if (isThenArray == elseType instanceof PsiArrayType) return;
|
||||
|
||||
final PsiExpression nonArray = isThenArray ? elseExpression : thenExpression;
|
||||
final PsiType nonArrayType = nonArray.getType();
|
||||
final PsiExpression array = isThenArray ? thenExpression : elseExpression;
|
||||
|
||||
PsiClassType varargsType = ObjectUtils.tryCast(varargsExpression.getType(), PsiClassType.class);
|
||||
if (varargsType == null) return;
|
||||
|
||||
String typeName = varargsType.getName();
|
||||
final String replacementText = String.format("new %s[]{%s}", typeName, nonArray.getText());
|
||||
final LocalQuickFix fix = ClassUtils.isPrimitive(nonArrayType) ? null : new WrapInArrayInitializerFix(replacementText, typeName);
|
||||
final LocalQuickFix fix = PsiAdapter.isPrimitiveArrayType(array.getType()) ? null :
|
||||
new WrapInArrayInitializerFix(replacementText, typeName);
|
||||
|
||||
holder.registerProblem(nonArray,
|
||||
JavaBundle.message("inspection.suspicious.ternary.in.varargs.description"),
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Replace with 'new Object[]{a}'" "false"
|
||||
// "Replace with 'new Object[]{b}'" "false"
|
||||
|
||||
class Test {
|
||||
static void bar(boolean flag) {
|
||||
Object[] a = {1, 2};
|
||||
Object b = "hello";
|
||||
foo(0, 1, flag ? a : b);
|
||||
}
|
||||
static void foo(int x, Object... xs) {
|
||||
}
|
||||
}
|
||||
+2
-7
@@ -1,15 +1,10 @@
|
||||
// "Replace with 'new Object[]{b}'" "true"
|
||||
|
||||
class Test {
|
||||
public static void main(String[] args) {
|
||||
static void bar(boolean flag) {
|
||||
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);
|
||||
}
|
||||
foo(0, flag ? a : new Object[]{b});
|
||||
}
|
||||
static void foo(int x, Object... xs) {
|
||||
}
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Replace with 'new Object[]{b}'" "false"
|
||||
|
||||
class Test {
|
||||
static void bar(boolean flag) {
|
||||
int[] a = {1, 2};
|
||||
Integer b = 42;
|
||||
foo(0, flag ? a : b);
|
||||
}
|
||||
static void foo(int x, Object... xs) {
|
||||
}
|
||||
}
|
||||
+2
-7
@@ -3,15 +3,10 @@
|
||||
import java.io.Serializable;
|
||||
|
||||
class Test {
|
||||
public static void main(String[] args) {
|
||||
static void bar(boolean flag) {
|
||||
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);
|
||||
}
|
||||
foo(0, flag ? a : new Serializable[]{b});
|
||||
}
|
||||
static void foo(int x, Serializable... xs) {
|
||||
}
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Replace with 'new Object[]{getObject(/*empty*/)}'" "true"
|
||||
|
||||
class Test {
|
||||
static void foo(Object... data) { }
|
||||
|
||||
void test(boolean b, Object[] obj2) {
|
||||
foo(b ? new Object[]{getObject(/*empty*/)} : obj2);
|
||||
}
|
||||
|
||||
native Object getObject();
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Replace with 'new Object[]{b}'" "true"
|
||||
|
||||
class Test {
|
||||
static void bar(boolean flag) {
|
||||
Integer[] a = {1, 2};
|
||||
Integer b = 42;
|
||||
foo(0, (((flag ? a : new Object[]{b}))));
|
||||
}
|
||||
static void foo(int x, Object... xs) {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
bar(true);
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Replace with 'new Object[]{a}'" "false"
|
||||
// "Replace with 'new Object[]{b}'" "false"
|
||||
|
||||
class Test {
|
||||
static void bar(boolean flag) {
|
||||
Object[] a = {1, 2};
|
||||
Object b = "hello";
|
||||
foo(0, 1, flag ? a : b<caret>);
|
||||
}
|
||||
static void foo(int x, Object... xs) {
|
||||
}
|
||||
}
|
||||
+2
-7
@@ -1,15 +1,10 @@
|
||||
// "Replace with 'new Object[]{b}'" "true"
|
||||
|
||||
class Test {
|
||||
public static void main(String[] args) {
|
||||
static void bar(boolean flag) {
|
||||
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<caret>);
|
||||
foo(0, 1, flag ? a : b);
|
||||
}
|
||||
foo(0, flag ? a : b<caret>);
|
||||
}
|
||||
static void foo(int x, Object... xs) {
|
||||
}
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Replace with 'new Object[]{b}'" "false"
|
||||
|
||||
class Test {
|
||||
static void bar(boolean flag) {
|
||||
int[] a = {1, 2};
|
||||
Integer b = 42;
|
||||
foo(0, flag ? a : b<caret>);
|
||||
}
|
||||
static void foo(int x, Object... xs) {
|
||||
}
|
||||
}
|
||||
+2
-7
@@ -3,15 +3,10 @@
|
||||
import java.io.Serializable;
|
||||
|
||||
class Test {
|
||||
public static void main(String[] args) {
|
||||
static void bar(boolean flag) {
|
||||
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<caret>);
|
||||
foo(0, 1, flag ? a : b);
|
||||
}
|
||||
foo(0, flag ? a : b<caret>);
|
||||
}
|
||||
static void foo(int x, Serializable... xs) {
|
||||
}
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Replace with 'new Object[]{getObject(/*empty*/)}'" "true"
|
||||
|
||||
class Test {
|
||||
static void foo(Object... data) { }
|
||||
|
||||
void test(boolean b, Object[] obj2) {
|
||||
foo(b ? getObject(/*empty*/<caret>) : obj2);
|
||||
}
|
||||
|
||||
native Object getObject();
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Replace with 'new Object[]{b}'" "true"
|
||||
|
||||
class Test {
|
||||
static void bar(boolean flag) {
|
||||
Integer[] a = {1, 2};
|
||||
Integer b = 42;
|
||||
foo(0, (((flag ? a : b<caret>))));
|
||||
}
|
||||
static void foo(int x, Object... xs) {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
bar(true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user