StringConcatenationInLoopsInspection: null-safe fixes

Fixes IDEA-183139 "String to StringBuilder" quick fix may cause NPE
This commit is contained in:
Tagir Valeev
2017-12-26 17:15:41 +07:00
parent 74ce1e1a43
commit 2e7492686e
12 changed files with 306 additions and 40 deletions

View File

@@ -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);
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -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
}
}

View File

@@ -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());
}
}

View File

@@ -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);
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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
}
}

View File

@@ -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());
}
}