OptionalToIfInspection: added inspection to desugar optional chain to sequence of if statements (IDEA-212269)

GitOrigin-RevId: c83b70e05544529b3dfffe24bc87997910edcb56
This commit is contained in:
Artemiy Sartakov
2019-08-07 17:02:43 +03:00
committed by intellij-monorepo-bot
parent 987f5c84d3
commit 6f1efb8fc0
51 changed files with 2953 additions and 553 deletions
@@ -0,0 +1,28 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
private String returnStatement(String in) {
if (in != null) return in;
return "foo";
}
private void assignment(String in) {
String out = null;
out = "foo";
if (in != null) out = in;
}
private void declaration(String in) {
String out = "foo";
if (in != null) out = in;
}
private void statement(String in) {
if (in != null) System.out.println(in);
}
private void statementWithResult(String in) {
if (in == null) throw new NullPointerException();
}
}
@@ -0,0 +1,9 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
void statementWithGetGeneratesThrowStatement() {
Object empty = null;
throw new NoSuchElementException("No value present");
}
}
@@ -0,0 +1,28 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
String checkNonConstantCondition(String in) {
if (in != null && in.length() > 42) return in;
return "foo";
}
String removeCheckForConstantCondition(String in) {
if (in != null) return in;
return "foo";
}
String removeAlwaysFalseCheckForConstantCondition(String in) {
return "foo";
}
String twoFiltersInARowGenerateOneIf(boolean b, String in) {
if (in != null && in.length() > 42 && getIfTrue(in, b) != null) return in;
return "foo";
}
private String getIfTrue(String str, boolean b) {
return b ? str : null;
}
}
@@ -0,0 +1,54 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
private void reusesVariable(String in) {
if (in == null) throw new NullPointerException();
String id = id(in);
if (id == null) throw new NoSuchElementException("No value present");
Object out = id;
}
private void checkIsRemoved(String in) {
if (in == null) throw new NullPointerException();
String out = in;
}
void simple(String in) {
String out = "bar";
if (in != null) out = in;
}
void simpleWithMap(String in) {
String out = "bar";
if (in != null) {
String id = id(in);
if (id != null) out = id;
}
}
void nested(String in) {
String out = "bar";
if (in != null) out = in;
}
void outer(String in, String p) {
String out = "bar";
if (in != null) {
String value = in + in + p;
out = value;
}
}
void nullableOuter(String in, String p) {
String out = "bar";
if (in != null) {
if (p == null) throw new NullPointerException();
out = p;
}
}
<T> T id(T t) {
return t;
}
}
@@ -0,0 +1,10 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
String exceptionIsThrownIfNull(String in) {
if (in == null || in.length() <= 2) throw new NoSuchElementException("No value present");
return in;
}
}
@@ -0,0 +1,15 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
void simple(String in) {
if (in != null) System.out.println(in);
}
void lambdaIsNotSimplified(String in, String p1, String p2) {
if (in == null || p1 == null) throw new IllegalArgumentException();
String tmp = "foo";
tmp = "bar";
}
}
@@ -0,0 +1,24 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
void inStatement(String in) {
String value = null;
if (in != null && in.length > 2) {
String ss = in.substring(3);
String strOrNull = getStrOrNull(ss);
if (strOrNull != null) value = strOrNull;
}
if (value == null) {
System.out.println("value is null");
} else {
System.out.println("found value %s", value);
}
}
private String getStrOrNull(String s) {
return s.length() > 2 ? s : null;
}
}
@@ -0,0 +1,19 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
boolean isPresent(String in) {
if (in == null) throw new NullPointerException();
String s = in.substring(3);
if (s.startsWith("1")) return true;
return false;
}
boolean isEmpty(String in) {
if (in == null) throw new NullPointerException();
String s = in.substring(3);
if (s.startsWith("1")) return false;
return true;
}
}
@@ -0,0 +1,39 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
String checkForNullable(String in) {
if (in == null) throw new NullPointerException();
String strOrNull = getStrOrNull(in);
if (strOrNull == null) throw new NoSuchElementException("No value present");
return strOrNull;
}
String checkIsRemovedForNotNull(String in) {
if (in == null) throw new NullPointerException();
String s = id(in);
if (s.length() <= 2) throw new NoSuchElementException("No value present");
return s;
}
String twoMapsProduceTwoVariables(String in, boolean b) {
if (in == null) throw new NullPointerException();
String s = id(in);
String strIfTrue = getStrIfTrue(s, b);
if (strIfTrue == null || strIfTrue.length <= 2) throw new NoSuchElementException("No value present");
return strIfTrue;
}
private String id(String s) {
return s;
}
private String getStrOrNull(String s) {
return s.length() > 2 ? s : null;
}
private String getStrIfTrue(String s, boolean b) {
return b ? s : null;
}
}
@@ -0,0 +1,29 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
void exceptionIsThrownOnNullValue(String in) {
if (in == null) throw new NullPointerException();
String out = in;
}
void exceptionIsTheSameWithOrElseThrow(String in) {
if (in == null) throw new NullPointerException();
Integer len = getLen(in);
if (len == null) throw new IllegalArgumentException("value is null");
Integer out = len;
}
void redundantCheckIsRemoved() {
String in = "not null value";
String out = in;
}
private Integer getLen(String s) {
return s.startsWith("abc") ? null : s;
}
private Integer filterLen(String s) {
return s.length() > 42 ? s.length() : null;
}
}
@@ -0,0 +1,8 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
String notNullValueCheckIsRemoved(String in) {
if (in == null) return "foo";
return in;
}
}
@@ -0,0 +1,34 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
String reusesVariable(String in) {
String s1 = null;
if (in == null) throw new NullPointerException();
String s = toName(in);
if (s != null) s1 = s;
if (s1 == null) {
String value = toDefaultName();
s1 = value;
}
return s1;
}
String removesRedundantAssignment(String in) {
String s1 = null;
String s = null;
if (in == null) throw new NullPointerException();
s = in;
return s;
}
private String toName(String str) {
if (str.startsWith("name")) return str.substring(4);
return null;
}
private String toDefaultName() {
return "defaultName";
}
}
@@ -0,0 +1,15 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
String returnOfOrElseValue(String in) {
if (in != null) return in;
return "foo";
}
void assignmentOfOrElseValue(String in) {
String out = "foo";
if (in != null) out = in;
}
}
@@ -0,0 +1,24 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
void assignment(String in) {
String out = "foo";
if (in != null && in.length() > 2) out = in;
}
void assignmentWithSideEffect(String in) {
String result = null;
if (in != null && in.length() > 2) result = in;
if (result == null) result = sideEffect();
String out = result;
}
private String sideEffect() {
System.out.println("side effect");
return "foo";
}
}
@@ -0,0 +1,27 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
String orElseThrowDefault(String in) {
if (in == null || in.length() <= 12) throw new NoSuchElementException("No value present");
return in;
}
String orElseLambda(String in) {
if (in == null || in.length() <= 12) throw new IllegalArgumentException("value is null");
return in;
}
String orElseThrowWithSideEffect(String in) {
if (in == null) throw sideEffect();
String s = in.substring(3);
if (s.length() <= 12) throw sideEffect();
return s;
}
private RuntimeException sideEffect() {
System.out.println("side effect!");
return new IllegalArgumentException("value is null")
}
}
@@ -0,0 +1,14 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
import java.util.stream.Stream;
class Test {
void stream(String in) {
Stream<String> out = Stream.empty();
if (in == null) throw new NullPointerException();
String s = in.length() > 2 ? in : null;
if (s != null) out = Stream.of(s);
}
}
@@ -0,0 +1,25 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
private String returnStatement(String in) {
return Optional.ofNullable<caret>(in).orElse("foo");
}
private void assignment(String in) {
String out = null;
out = Optional.ofNullable(in).orElse("foo");
}
private void declaration(String in) {
String out = Optional.ofNullable(in).orElse("foo");
}
private void statement(String in) {
Optional.ofNullable(in).ifPresent(v -> System.out.println(v));
}
private void statementWithResult(String in) {
Optional.of(in).orElse("foo");
}
}
@@ -0,0 +1,8 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
void statementWithGetGeneratesThrowStatement() {
Optional.empty<caret>().get();
}
}
@@ -0,0 +1,25 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
String checkNonConstantCondition(String in) {
return Optional.ofNullable<caret>(in).filter(s -> s.length() > 42).orElse("foo");
}
String removeCheckForConstantCondition(String in) {
return Optional.ofNullable(in).filter(s -> s != null).orElse("foo");
}
String removeAlwaysFalseCheckForConstantCondition(String in) {
return Optional.ofNullable(in).filter(s -> s == null).orElse("foo");
}
String twoFiltersInARowGenerateOneIf(boolean b, String in) {
return Optional.ofNullable(in).filter(s -> s.length() > 42).filter(s -> getIfTrue(s, b) != null).orElse("foo");
}
private String getIfTrue(String str, boolean b) {
return b ? str : null;
}
}
@@ -0,0 +1,36 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
private void reusesVariable(String in) {
Object out = Optional.of<caret>(in).flatMap(o -> Optional.of(o)).map(o -> id(o)).get();
}
private void checkIsRemoved(String in) {
String out = Optional.of(in).flatMap(s -> Optional.of(in)).get();
}
void simple(String in) {
String out = Optional.ofNullable<caret>(in).flatMap(s -> Optional.of(s)).orElse("bar");
}
void simpleWithMap(String in) {
String out = Optional.ofNullable<caret>(in).flatMap(s -> Optional.of(s).map(v -> id(v))).orElse("bar");
}
void nested(String in) {
String out = Optional.ofNullable(in).flatMap(s1 -> Optional.of(s1).flatMap(s2 -> Optional.of(s2))).orElse("bar");
}
void outer(String in, String p) {
String out = Optional.ofNullable(in).flatMap(s1 -> Optional.of(s1).flatMap(s2 -> Optional.of(s2 + s1 + p))).orElse("bar");
}
void nullableOuter(String in, String p) {
String out = Optional.ofNullable(in).flatMap(s1 -> Optional.of(p)).orElse("bar");
}
<T> T id(T t) {
return t;
}
}
@@ -0,0 +1,9 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
String exceptionIsThrownIfNull(String in) {
return Optional.ofNullable<caret>(in).filter(s -> s.length() > 2).get();
}
}
@@ -0,0 +1,17 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
void simple(String in) {
Optional.ofNullable<caret>(in).ifPresent(System.out::println);
}
void lambdaIsNotSimplified(String in, String p1, String p2) {
if (in == null || p1 == null) throw new IllegalArgumentException();
Optional.ofNullable(in).ifPresent(s -> {
String tmp = "foo";
tmp = "bar";
});
}
}
@@ -0,0 +1,16 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
void inStatement(String in) {
Optional.ofNullable<caret>(in).filter(s -> s.length > 2).map(s -> s.substring(3)).map(ss -> getStrOrNull(ss))
.ifPresentOrElse(value -> System.out.println("found value %s", value),
() -> System.out.println("value is null"));
}
private String getStrOrNull(String s) {
return s.length() > 2 ? s : null;
}
}
@@ -0,0 +1,13 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
boolean isPresent(String in) {
return Optional.of<caret>(in).map(in -> in.substring(3)).filter(s -> s.startsWith("1")).isPresent();
}
boolean isEmpty(String in) {
return Optional.of(in).map(in -> in.substring(3)).filter(s -> s.startsWith("1")).isEmpty();
}
}
@@ -0,0 +1,29 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
String checkForNullable(String in) {
return Optional.<caret>of(in).map(s -> getStrOrNull(s)).get();
}
String checkIsRemovedForNotNull(String in) {
return Optional.of(in).map(s -> id(s)).filter(s -> s.length() > 2).get();
}
String twoMapsProduceTwoVariables(String in, boolean b) {
return Optional.of(in).map(s -> id(s)).map(s -> getStrIfTrue(s, b)).filter(s -> s.length > 2).get();
}
private String id(String s) {
return s;
}
private String getStrOrNull(String s) {
return s.length() > 2 ? s : null;
}
private String getStrIfTrue(String s, boolean b) {
return b ? s : null;
}
}
@@ -0,0 +1,25 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
void exceptionIsThrownOnNullValue(String in) {
String out = Optional.of<caret>(in).get();
}
void exceptionIsTheSameWithOrElseThrow(String in) {
Integer out = Optional.of(in).map(s -> getLen(s)).orElseThrow(() -> new IllegalArgumentException("value is null"));
}
void redundantCheckIsRemoved() {
String in = "not null value";
String out = Optional.of(in).orElseThrow(() -> new IllegalArgumentException("value is null"));
}
private Integer getLen(String s) {
return s.startsWith("abc") ? null : s;
}
private Integer filterLen(String s) {
return s.length() > 42 ? s.length() : null;
}
}
@@ -0,0 +1,8 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
String notNullValueCheckIsRemoved(String in) {
if (in == null) return "foo";
return Optional.ofNullable<caret>(in).orElse("bar");
}
}
@@ -0,0 +1,22 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
String reusesVariable(String in) {
return Optional.of<caret>(in).map(s -> toName(s)).or(() -> Optional.of(toDefaultName())).get();
}
String removesRedundantAssignment(String in) {
return Optional.of(in).or(() -> Optional.of(toDefaultName())).or(() -> Optional.of(toDefaultName())).get();
}
private String toName(String str) {
if (str.startsWith("name")) return str.substring(4);
return null;
}
private String toDefaultName() {
return "defaultName";
}
}
@@ -0,0 +1,13 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
String returnOfOrElseValue(String in) {
return Optional.ofNullable<caret>(in).orElse("foo");
}
void assignmentOfOrElseValue(String in) {
String out = Optional.ofNullable(in).orElse("foo");
}
}
@@ -0,0 +1,20 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
void assignment(String in) {
String out = Optional.ofNullable<caret>(in).filter(s -> s.length() > 2).orElseGet(() -> "foo");
}
void assignmentWithSideEffect(String in) {
String out = Optional.ofNullable(in).filter(s -> s.length() > 2).orElseGet(() -> sideEffect());
}
private String sideEffect() {
System.out.println("side effect");
return "foo";
}
}
@@ -0,0 +1,22 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
class Test {
String orElseThrowDefault(String in) {
return Optional.ofNullable<caret>(in).filter(s -> s.length() > 12).orElseThrow();
}
String orElseLambda(String in) {
return Optional.ofNullable(in).filter(s -> s.length() > 12).orElseThrow(() -> new IllegalArgumentException("value is null"));
}
String orElseThrowWithSideEffect(String in) {
return Optional.ofNullable(in).map(s -> s.substring(3)).filter(s -> s.length() > 12).orElseThrow(() -> sideEffect());
}
private RuntimeException sideEffect() {
System.out.println("side effect!");
return new IllegalArgumentException("value is null")
}
}
@@ -0,0 +1,12 @@
// "Fix all 'Optional can be replaced with sequence of if statements' problems in file" "true"
import java.util.Optional;
import java.util.stream.Stream;
class Test {
void stream(String in) {
Stream<String> out = Optional.<caret>of(in).map(s -> s.length() > 2 ? s : null).stream();
}
}