mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-08-26 03:13:40 +07:00
StreamToLoopInspection: reuse existing variable if we should reassign it in loop, but it's possible to use it as non-final
This commit is contained in:
+18
@@ -0,0 +1,18 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
public void test(List<Integer> list) {
|
||||
boolean x = false;
|
||||
for (Integer i : list) {
|
||||
if (i <= 2) {
|
||||
x = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(x) {
|
||||
System.out.println("found");
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-3
@@ -6,11 +6,10 @@ import java.util.stream.Stream;
|
||||
|
||||
public class Main<T> {
|
||||
void test() {
|
||||
Integer acc = 0;
|
||||
Integer totalLength = 0;
|
||||
for (String s : Arrays.asList("a", "bb", "ccc")) {
|
||||
Integer length = s.length();
|
||||
acc = acc + length;
|
||||
totalLength = totalLength + length;
|
||||
}
|
||||
Integer totalLength = acc;
|
||||
}
|
||||
}
|
||||
+3
-4
@@ -4,11 +4,10 @@ import java.util.List;
|
||||
|
||||
public class Main {
|
||||
public void test(List<String> list) {
|
||||
long count = 0;
|
||||
long x = 0;
|
||||
for (String s : list) {
|
||||
count++;
|
||||
x++;
|
||||
}
|
||||
long x = count;
|
||||
System.out.println(x);
|
||||
System.out.println(x);
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
public void test(List<String> list) {
|
||||
String res = "";
|
||||
for (String s : list) {
|
||||
String trim = s.trim();
|
||||
if (!trim.isEmpty()) {
|
||||
res = trim;
|
||||
break;
|
||||
}
|
||||
}
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
public void test(List<String> list) {
|
||||
// Cannot reuse the variable as user explicitly marked it as final
|
||||
String found = "";
|
||||
for (String s : list) {
|
||||
String trim = s.trim();
|
||||
if (!trim.isEmpty()) {
|
||||
found = trim;
|
||||
break;
|
||||
}
|
||||
}
|
||||
final String res = found;
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
+3
-1
@@ -1,6 +1,7 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.LongSupplier;
|
||||
|
||||
public class Main {
|
||||
private static void test(List<String> list) {
|
||||
@@ -12,7 +13,8 @@ public class Main {
|
||||
}
|
||||
long count = count1;
|
||||
if(count > 10) {
|
||||
long result = count*2;
|
||||
LongSupplier sup = () -> count*2;
|
||||
long result = sup.get();
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
public void test(List<Integer> list) {
|
||||
boolean x = !list.stream().all<caret>Match(i -> i > 2);
|
||||
if(x) {
|
||||
System.out.println("found");
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
public void test(List<String> list) {
|
||||
String res = list.stream().map(String::trim).filter(trim -> !trim.isEmpty()).fi<caret>ndFirst().orElse("");
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
public void test(List<String> list) {
|
||||
// Cannot reuse the variable as user explicitly marked it as final
|
||||
final String res = list.stream().map(String::trim).filter(trim -> !trim.isEmpty()).fi<caret>ndFirst().orElse("");
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
+3
-1
@@ -1,12 +1,14 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.LongSupplier;
|
||||
|
||||
public class Main {
|
||||
private static void test(List<String> list) {
|
||||
long count = list.stream().filter(s -> !s.isEmpty()).co<caret>unt();
|
||||
if(count > 10) {
|
||||
long result = count*2;
|
||||
LongSupplier sup = () -> count*2;
|
||||
long result = sup.get();
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user