mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-30 10:20:15 +07:00
StringConcatenationInLoopsInspection: null-safe fixes
Fixes IDEA-183139 "String to StringBuilder" quick fix may cause NPE
This commit is contained in:
@@ -5,7 +5,7 @@ public class Main {
|
||||
StringBuilder res = null;
|
||||
for (String s : strings) {
|
||||
if(res == null) {
|
||||
res = s == null ? null : new StringBuilder(s);
|
||||
res = new StringBuilder(s);
|
||||
} else {
|
||||
res.append(s);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
// "Convert variable 'res' from String to StringBuilder (null-safe)" "true"
|
||||
|
||||
class Main {
|
||||
String test(String[] strings) {
|
||||
StringBuilder res = null;
|
||||
for (String s : strings) {
|
||||
if(res == null) {
|
||||
res = s == null ? null : new StringBuilder(s);
|
||||
} else {
|
||||
res.append(s);
|
||||
}
|
||||
}
|
||||
return res == null ? null : res.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// "Convert variable 'res' from String to StringBuilder (null-safe)" "true"
|
||||
|
||||
public class Main {
|
||||
String test(String[] strings) {
|
||||
StringBuilder res = null;
|
||||
for (String s : strings) {
|
||||
if (s == null) continue;
|
||||
if(res == null) {
|
||||
res = new StringBuilder(s);
|
||||
} else {
|
||||
res.append(s);
|
||||
}
|
||||
}
|
||||
return res == null ? null : res.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// "Convert variable 'res' from String to StringBuilder (null-safe)" "true"
|
||||
|
||||
public class Main {
|
||||
String test(String[] strings) {
|
||||
StringBuilder res = null;
|
||||
res = (res == null ? new StringBuilder("null") : res).append("foo");
|
||||
for (String s : strings) {
|
||||
res.append(", ");
|
||||
res.append(s);
|
||||
}
|
||||
return res.toString(); // known to be not-null at this point
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// "Convert variable 'res' from String to StringBuilder (null-safe)" "true"
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class Main {
|
||||
String test(String[] strings) {
|
||||
StringBuilder res = null;
|
||||
for (String s : strings) {
|
||||
if(res == null) {
|
||||
res = Optional.ofNullable(s.isEmpty() ? null : s).map(StringBuilder::new).orElse(null);
|
||||
} else {
|
||||
res.append(", ").append(s);
|
||||
}
|
||||
res = (res == null ? new StringBuilder("null") : res).append(", ");
|
||||
res.append(s);
|
||||
}
|
||||
System.out.println(res);
|
||||
consume(res.toString());
|
||||
return res.toString(); // known to be not-null at this point
|
||||
}
|
||||
|
||||
// NotNull parameter inferred
|
||||
static void consume(String s) {
|
||||
System.out.println(s.trim());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// "Convert variable 's1' from String to StringBuilder (null-safe)" "false"
|
||||
|
||||
import java.util.List;
|
||||
|
||||
class Test {
|
||||
static void test(List<String> list) {
|
||||
String s2 = "bar";
|
||||
|
||||
String s1 = s2;
|
||||
|
||||
for (String s : list) {
|
||||
s1 +<caret>= "baz";
|
||||
}
|
||||
|
||||
s1 += "foo";
|
||||
System.out.println(s1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// "Convert variable 'res' from String to StringBuilder (null-safe)" "true"
|
||||
|
||||
class Main {
|
||||
String test(String[] strings) {
|
||||
String res = null;
|
||||
for (String s : strings) {
|
||||
if(res == null) {
|
||||
res = s;
|
||||
} else {
|
||||
res<caret>+=s;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// "Convert variable 'res' from String to StringBuilder (null-safe)" "true"
|
||||
|
||||
public class Main {
|
||||
String test(String[] strings) {
|
||||
String res = null;
|
||||
for (String s : strings) {
|
||||
if (s == null) continue;
|
||||
if(res == null) {
|
||||
res = s;
|
||||
} else {
|
||||
res<caret>+=s;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// "Convert variable 'res' from String to StringBuilder (null-safe)" "true"
|
||||
|
||||
public class Main {
|
||||
String test(String[] strings) {
|
||||
String res = null;
|
||||
res += "foo";
|
||||
for (String s : strings) {
|
||||
res+<caret>=", ";
|
||||
res+=s;
|
||||
}
|
||||
return res; // known to be not-null at this point
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// "Convert variable 'res' from String to StringBuilder (null-safe)" "true"
|
||||
|
||||
public class Main {
|
||||
String test(String[] strings) {
|
||||
String res = null;
|
||||
for (String s : strings) {
|
||||
if(res == null) {
|
||||
res = s.isEmpty() ? null : s;
|
||||
} else {
|
||||
res<caret>+=", "+s;
|
||||
}
|
||||
res+=", ";
|
||||
res+=s;
|
||||
}
|
||||
System.out.println(res);
|
||||
consume(res);
|
||||
return res; // known to be not-null at this point
|
||||
}
|
||||
|
||||
// NotNull parameter inferred
|
||||
static void consume(String s) {
|
||||
System.out.println(s.trim());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user