StringConcatenationInLoopsInspection: add null-check for unknown variables

IDEA-183139 "String to StringBuilder" quick fix may cause NPE
This commit is contained in:
Tagir Valeev
2017-12-06 11:57:10 +07:00
parent ab8419b387
commit f03d494538
5 changed files with 45 additions and 2 deletions

View File

@@ -5,7 +5,7 @@ public class Main {
StringBuilder res = null;
for (String s : strings) {
if(res == null) {
res = new StringBuilder(s);
res = s == null ? null : new StringBuilder(s);
} else {
res.append(s);
}

View File

@@ -0,0 +1,16 @@
// "Convert variable 'res' from String to StringBuilder" "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.toString();
}
}

View File

@@ -0,0 +1,16 @@
// "Convert variable 'res' from String to StringBuilder" "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;
}
}