UnrollLoopAction: fixes according to review IDEA-CR-24513

1. Check that loop parameter is not written
2. Support List.of, Collections.singleton, Collections.singletonList
3. Support if w/o braces
4. Support break not in last position
This commit is contained in:
Tagir Valeev
2017-09-18 16:37:40 +07:00
parent 8928ef54a4
commit 7345e8b0ef
19 changed files with 264 additions and 30 deletions
@@ -0,0 +1,19 @@
// "Unroll loop" "true"
class Test {
void test() {
if (!(Math.random() > 0.5)) {
System.out.println((Object) "one");
if (!(Math.random() > 0.5)) {
System.out.println((Object) 1);
if (!(Math.random() > 0.5)) {
System.out.println((Object) 1.0);
if (!(Math.random() > 0.5)) {
System.out.println((Object) 1.0f);
}
}
}
}
}
void foo(boolean b) {}
}
@@ -0,0 +1,25 @@
// "Unroll loop" "true"
class Test {
void test(String s1, String s2, String s3) {
if (s1.length() <= 5) {
System.out.println("Long string: " + s1);
if (s1.length() <= 20) {
System.out.println("Very long string: " + s1);
if (s2.length() <= 5) {
System.out.println("Long string: " + s2);
if (s2.length() <= 20) {
System.out.println("Very long string: " + s2);
if (s3.length() <= 5) {
System.out.println("Long string: " + s3);
if (s3.length() <= 20) {
System.out.println("Very long string: " + s3);
}
}
}
}
}
}
}
void foo(boolean b) {}
}
@@ -0,0 +1,14 @@
// "Unroll loop" "true"
class Test {
void test(String s1, String s2, String s3) {
if ((i += s1.length()) <= 10) {
if ((i += s2.length()) <= 10) {
if ((i += s3.length()) <= 10) {
}
}
}
System.out.println(i);
}
void foo(boolean b) {}
}
@@ -0,0 +1,19 @@
// "Unroll loop" "true"
import java.util.*;
class Test {
void test() {
if (!"foo".isEmpty()) {
System.out.println("foo");
}
if (!"bar".isEmpty()) {
System.out.println("bar");
}
if (!"baz".isEmpty()) {
System.out.println("baz");
}
if (!"".isEmpty()) {
System.out.println("");
}
}
}
@@ -0,0 +1,10 @@
// "Unroll loop" "true"
import java.util.*;
class Test {
void test() {
if (!"foo".isEmpty()) {
System.out.println("foo");
}
}
}
@@ -0,0 +1,11 @@
// "Unroll loop" "true"
import java.util.Arrays;
class Test {
void test() {
System.out.println("foo");
System.out.println("bar");
}
void foo(boolean b) {}
}
@@ -0,0 +1,10 @@
// "Unroll loop" "true"
import java.util.*;
class Test {
void test() {
if (!"xyz".isEmpty()) {
System.out.println("xyz");
}
}
}
@@ -1,4 +1,4 @@
// "Unroll loop" "false"
// "Unroll loop" "true"
class Test {
void test() {
fo<caret>r(Object x : new Object[] {"one", 1, 1.0, 1.0f}) {
@@ -0,0 +1,15 @@
// "Unroll loop" "false"
class Test {
void test(String s1, String s2, String s3) {
fo<caret>r(String s : new String[] {s1, s2, s3}) {
if (!s.isEmpty()) {
if(s.length() > 5) break;
}
System.out.println("Long string: "+s);
if(s.length() > 20) break;
System.out.println("Very long string: "+s);
}
}
void foo(boolean b) {}
}
@@ -0,0 +1,13 @@
// "Unroll loop" "true"
class Test {
void test(String s1, String s2, String s3) {
fo<caret>r(String s : new String[] {s1, s2, s3}) {
if(s.length() > 5) break;
System.out.println("Long string: "+s);
if(s.length() > 20) break;
System.out.println("Very long string: "+s);
}
}
void foo(boolean b) {}
}
@@ -0,0 +1,10 @@
// "Unroll loop" "true"
class Test {
void test(String s1, String s2, String s3) {
fo<caret>r(String s : new String[] {s1, s2, s3})
if((i+=s.length()) > 10) break;
System.out.println(i);
}
void foo(boolean b) {}
}
@@ -0,0 +1,12 @@
// "Unroll loop" "false"
import java.util.*;
class Test {
void test(String[] data) {
fo<caret>r(String s : Arrays.asList(data)) {
if(!s.isEmpty()) {
System.out.println(s);
}
}
}
}
@@ -0,0 +1,12 @@
// "Unroll loop" "true"
import java.util.*;
class Test {
void test() {
fo<caret>r(String s : List.of("foo", "bar", "baz", "")) {
if(!s.isEmpty()) {
System.out.println(s);
}
}
}
}
@@ -0,0 +1,12 @@
// "Unroll loop" "true"
import java.util.*;
class Test {
void test() {
fo<caret>r(String s : List.of("foo")) {
if(!s.isEmpty()) {
System.out.println(s);
}
}
}
}
@@ -0,0 +1,10 @@
// "Unroll loop" "true"
import java.util.Arrays;
class Test {
void test() {
fo<caret>r(String s : Arrays.asList("foo", "bar")) System.out.println(s);
}
void foo(boolean b) {}
}
@@ -0,0 +1,12 @@
// "Unroll loop" "true"
import java.util.*;
class Test {
void test() {
fo<caret>r(String s : Collections.singleton("xyz")) {
if(!s.isEmpty()) {
System.out.println(s);
}
}
}
}
@@ -0,0 +1,13 @@
// "Unroll loop" "false"
class Test {
void test() {
f<caret>or(int x : new int[]{1, 2, 3}) {
System.out.println(x);
x = 6;
x++;
System.out.println(x);
}
}
void foo(boolean b) {}
}