String () { String foo = ""; for(int i = 0; i < 5; i++) { (foo) = ((foo) <warning descr="String concatenation '+' in loop">+</warning> (" ") + (i)); foo += "abc"; // only first concatenation in the loop is reported for given variable } for(int i = 0; i < 5; i++) { foo <warning descr="String concatenation '+=' in loop">+=</warning> foo + " " + i; } for(int i = 0; i < 5; i++) { baz( foo + " " + i); } for(int i = 0; i < 5; i++) { if(bar()) { return baz(("foo" + "bar")); } } for(int i = 0; i < 5; i++) { if(bar()) { throw new Error("foo" + i); } } String s = ""; for(int i = 0; i < 5; i++) { if(i > 2) { s += i; { System.out.println(s); break; } } } for(int i = 0; i < 5; i++) { if(i > 2) { s += i; { System.out.println(s); { break; } } } } for(int i = 0; i < 5; i++) { if(i > 2) { s += i; { { System.out.println(i); } break; } } } for(int i = 0; i < 5; i++) { if(i > 2) { s <warning descr="String concatenation '+=' in loop">+=</warning> i; { System.out.println(i); } System.out.println(i); } } for (int i = 0; i < 10; i++) { if (i == 5) { // concatenated only on single iteration s += i; } System.out.println(i); } List<String> strings = new ArrayList<>(); for (int i = 0; i < 10; i++) { s += i; if (s.length() > 0) { strings.add(s); // the whole string is used on every iteration anyways: it's useless to create a StringBuilder here } } for (int i = 0; i < 10; i++) { s <warning descr="String concatenation '+=' in loop">+=</warning> i; if (i == 5 && s.length() > 0) { strings.add(s); // the whole string is used, but only once: it's still reasonable to migrate to StringBuilder } } for (int i = 0; i < 10; i++) { s <warning descr="String concatenation '+=' in loop">+=</warning> i; if (s.length() > 50) { strings.add(s); // the whole string is used, but it's recreated after the usage: it's still reasonable to migrate to StringBuilder s = ""; } } for (int i = 0; i < 10; i++) { s = (s == "") + "..."; } for (int i = 0; i < 10; i++) { s = ("xyz" <warning descr="String concatenation '+' in loop">+</warning> (i <warning descr="String concatenation '+' in loop">+</warning> s)) <warning descr="String concatenation '+' in loop">+</warning> "..."; } System.out.println(foo); return foo; }