mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-05-06 05:10:22 +07:00
[java-intentions] ConvertSwitchToIf: fix the intention according a switch throws NPE if the selector expression is null
IDEA-300120 GitOrigin-RevId: 5d77f98e7931e32a06cb3c54076978d317f78a98
This commit is contained in:
committed by
intellij-monorepo-bot
parent
cd9e96567a
commit
108dc80b56
@@ -4,8 +4,8 @@ abstract class Test {
|
||||
|
||||
void foo() {
|
||||
Class<?> aClass = getObject().getClass();
|
||||
if (RuntimeException.class.equals(aClass)) {
|
||||
} else if (IOException.class.equals(aClass)) {
|
||||
if (aClass.equals(RuntimeException.class)) {
|
||||
} else if (aClass.equals(IOException.class)) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,9 @@
|
||||
class X {
|
||||
int m(String s, boolean r) {
|
||||
if (r) return 1;
|
||||
else if ("a".equals(s)) {
|
||||
else if (s.equals("a")) {
|
||||
return 1;
|
||||
} else if ("b".equals(s)) {
|
||||
} else if (s.equals("b")) {
|
||||
return 2;
|
||||
} else {
|
||||
return 3;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
// "Replace 'switch' with 'if'" "true-preview"
|
||||
class X {
|
||||
int m(String s, boolean r) {
|
||||
if ("a".equals(s)) {
|
||||
if (s.equals("a")) {
|
||||
return 1;
|
||||
} else if ("b".equals(s)) {
|
||||
} else if (s.equals("b")) {
|
||||
return 2;
|
||||
}
|
||||
return 3;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// "Replace 'switch' with 'if'" "true-preview"
|
||||
class X {
|
||||
void m(String s, boolean r) {
|
||||
if ("a".equals(s)) {
|
||||
if (s.equals("a")) {
|
||||
System.out.println("a");
|
||||
if (r) {
|
||||
return;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// "Replace 'switch' with 'if'" "true-preview"
|
||||
class X {
|
||||
void m(String s, boolean r) {
|
||||
if ("a".equals(s)) {
|
||||
if (s.equals("a")) {
|
||||
System.out.println("a");
|
||||
if (r) {
|
||||
} else {
|
||||
|
||||
@@ -4,7 +4,7 @@ class X {
|
||||
if (x > 0) {
|
||||
SWITCH:
|
||||
{
|
||||
if ("a".equals(s)) {
|
||||
if (s.equals("a")) {
|
||||
System.out.println("a");
|
||||
for (int i = 0; i < 10; i++) {
|
||||
System.out.println(i);
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
import java.util.Objects;
|
||||
|
||||
// "Replace 'switch' with 'if'" "true-preview"
|
||||
abstract class Test {
|
||||
abstract Object getObject();
|
||||
|
||||
void foo(Object o) {
|
||||
if (o instanceof String) {
|
||||
if (Objects.requireNonNull(o) instanceof String) {
|
||||
System.out.println("one");
|
||||
} else if (o instanceof Integer) {
|
||||
System.out.println("two");
|
||||
} else {
|
||||
System.out.println("default");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Replace 'switch' with 'if'" "true-preview"
|
||||
abstract class Test {
|
||||
abstract Object getObject();
|
||||
|
||||
void foo(Object o) {
|
||||
if (o instanceof String) {
|
||||
System.out.println("one");
|
||||
} else {
|
||||
System.out.println("default");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
// "Replace 'switch' with 'if'" "true-preview"
|
||||
class Test {
|
||||
void test(String str) {
|
||||
if (("foo" + "bar").equals(str)) {
|
||||
if (str.equals(("foo" + "bar"))) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
import java.util.Objects;
|
||||
|
||||
// "Replace 'switch' with 'if'" "true-preview"
|
||||
class Test {
|
||||
void test(Object obj) {
|
||||
if (obj == 1) {
|
||||
if (Objects.requireNonNull(obj) == 1) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
// "Replace 'switch' with 'if'" "true-preview"
|
||||
class X {
|
||||
void m(String s) {
|
||||
if ("foo".equals(s)) {
|
||||
if (s.equals("foo")) {
|
||||
System.out.println(1);
|
||||
} else if ("bar".equals(s)) {
|
||||
} else if (s.equals("bar")) {
|
||||
System.out.println(3);
|
||||
} else {
|
||||
System.out.println(2);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
class X {
|
||||
int m(String s, boolean r) {
|
||||
//ignore
|
||||
if ("x".equals(s)) {
|
||||
if (s.equals("x")) {
|
||||
System.out.println("foo");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import java.util.Objects;
|
||||
|
||||
// "Replace 'switch' with 'if'" "true-preview"
|
||||
class Test {
|
||||
void test(Object obj) {
|
||||
if (obj == obj instanceof String s) {
|
||||
if (Objects.requireNonNull(obj) == obj instanceof String s) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
import java.util.Objects;
|
||||
|
||||
// "Replace 'switch' with 'if'" "true-preview"
|
||||
class Test {
|
||||
void test(Object obj) {
|
||||
if (obj == obj instanceof String) {
|
||||
if (Objects.requireNonNull(obj) == obj instanceof String) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import java.util.Objects;
|
||||
|
||||
// "Replace 'switch' with 'if'" "true-preview"
|
||||
class Test {
|
||||
void test() {
|
||||
@@ -5,7 +7,7 @@ class Test {
|
||||
s;
|
||||
}
|
||||
P p = null;
|
||||
if (p == P.s) {
|
||||
if (Objects.requireNonNull(p) == P.s) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import java.util.Objects;
|
||||
|
||||
// "Replace 'switch' with 'if'" "true-preview"
|
||||
class Test {
|
||||
void test() {
|
||||
enum P {
|
||||
s, l;
|
||||
}
|
||||
P p = null;
|
||||
if (Objects.requireNonNull(p) == P.s || p == P.l) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Replace 'switch' with 'if'" "true-preview"
|
||||
class Test {
|
||||
void test() {
|
||||
enum P {
|
||||
s, l;
|
||||
}
|
||||
P p = null;
|
||||
if (p == P.s || p == null) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
class X {
|
||||
void m(String s, boolean r) {
|
||||
if (r) {
|
||||
if ("a".equals(s)) {
|
||||
if (s.equals("a")) {
|
||||
System.out.println("a");
|
||||
}
|
||||
System.out.println("d");
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
class X {
|
||||
|
||||
void m(String s, int a) throws IOException {
|
||||
if ("a".equals(s)) {
|
||||
if (s.equals("a")) {
|
||||
a();
|
||||
} else {
|
||||
d();
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
class X {
|
||||
void test4() throws IOException {
|
||||
String variable = "abc";
|
||||
if ("abc".equals(variable)) {
|
||||
if (variable.equals("abc")) {
|
||||
String s1 = "abcd";
|
||||
} else if ("def".equals(variable)) {
|
||||
} else if (variable.equals("def")) {
|
||||
String s1 = "abcd";
|
||||
myFunction(s1);
|
||||
} else {
|
||||
|
||||
@@ -3,10 +3,10 @@ class X {
|
||||
public void doSomething( String value) {
|
||||
//comment6
|
||||
//comment7
|
||||
if ("case1".equals(value)) {//comment1
|
||||
if (value.equals("case1")) {//comment1
|
||||
//comment2
|
||||
//comment3
|
||||
} else if ("case2".equals(value)) {//comment4
|
||||
} else if (value.equals("case2")) {//comment4
|
||||
//comment5
|
||||
}//comment8
|
||||
}
|
||||
|
||||
@@ -3,9 +3,9 @@ abstract class Test {
|
||||
abstract Object getObject();
|
||||
|
||||
void foo(String s) {
|
||||
if (s == null || "zero".equals(s)) {
|
||||
if (s == null || s.equals("zero")) {
|
||||
System.out.println(0);
|
||||
} else if ("one".equals(s)) {
|
||||
} else if (s.equals("one")) {
|
||||
System.out.println(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// "Replace 'switch' with 'if'" "true-preview"
|
||||
public class One {
|
||||
void f1(String a) {
|
||||
if ("one".equals(a)) {
|
||||
if (a.equals("one")) {
|
||||
System.out.println(1);
|
||||
}
|
||||
System.out.println("default");
|
||||
|
||||
@@ -3,7 +3,7 @@ abstract class Test {
|
||||
abstract Object getObject();
|
||||
|
||||
void foo() {
|
||||
if (RuntimeException.class.equals(getObject().getClass())) {
|
||||
if (getObject().getClass().equals(RuntimeException.class)) {
|
||||
System.out.println("RuntimeException");
|
||||
} else {
|
||||
System.out.println("Other");
|
||||
|
||||
@@ -5,7 +5,8 @@ abstract class Test {
|
||||
void foo(Object o) {
|
||||
<caret>switch (o) {
|
||||
case String s -> System.out.println("one");
|
||||
case null, default -> System.out.println("default");
|
||||
case Integer i -> System.out.println("two");
|
||||
case default -> System.out.println("default");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Replace 'switch' with 'if'" "true-preview"
|
||||
abstract class Test {
|
||||
abstract Object getObject();
|
||||
|
||||
void foo(Object o) {
|
||||
<caret>switch (o) {
|
||||
case String s -> System.out.println("one");
|
||||
case null, default -> System.out.println("default");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// "Replace 'switch' with 'if'" "true-preview"
|
||||
class Test {
|
||||
void test() {
|
||||
enum P {
|
||||
s, l;
|
||||
}
|
||||
P p = null;
|
||||
<caret>switch (p) {
|
||||
case s, l -> {}
|
||||
default -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// "Replace 'switch' with 'if'" "true-preview"
|
||||
class Test {
|
||||
void test() {
|
||||
enum P {
|
||||
s, l;
|
||||
}
|
||||
P p = null;
|
||||
<caret>switch (p) {
|
||||
case s, null -> {}
|
||||
default -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user