IDEA-181080 Result of expression is ignored for Optional.orElseThrow

This commit is contained in:
Tagir Valeev
2017-11-07 10:40:40 +07:00
parent 9ec08698c8
commit b16d922304
5 changed files with 21 additions and 8 deletions
@@ -36,7 +36,7 @@ public abstract class ContractValue {
/**
* @return true if this contract value represents a bounds-checking condition
*/
boolean isBoundCheckingCondition() {
public boolean isBoundCheckingCondition() {
return false;
}
@@ -168,7 +168,7 @@ public abstract class ContractValue {
}
@Override
boolean isBoundCheckingCondition() {
public boolean isBoundCheckingCondition() {
switch (myRelationType) {
case LE:
case LT:
@@ -71,7 +71,7 @@ public abstract class MethodContract {
abstract String getArgumentsPresentation();
abstract List<ContractValue> getConditions();
public abstract List<ContractValue> getConditions();
@Override
public String toString() {
@@ -91,7 +91,7 @@ public abstract class MethodContract {
}
@Override
List<ContractValue> getConditions() {
public List<ContractValue> getConditions() {
return Collections.emptyList();
}
};
@@ -114,7 +114,7 @@ public abstract class MethodContract {
}
@Override
List<ContractValue> getConditions() {
public List<ContractValue> getConditions() {
return Collections.singletonList(condition);
}
};
@@ -81,7 +81,7 @@ public final class StandardMethodContract extends MethodContract {
}
@Override
protected List<ContractValue> getConditions() {
public List<ContractValue> getConditions() {
return IntStreamEx.ofIndices(arguments).mapToObj(idx -> arguments[idx].getCondition(idx)).without(ContractValue.booleanValue(true))
.toList();
}
@@ -209,8 +209,9 @@ public class SideEffectChecker {
if (method.getName().startsWith("assert") || method.getName().startsWith("check")) {
return true;
}
return ControlFlowAnalyzer.getMethodContracts(method).stream()
.anyMatch(mc -> mc.returnValue == MethodContract.ValueConstraint.THROW_EXCEPTION);
return ControlFlowAnalyzer.getMethodCallContracts(method, null).stream()
.filter(mc -> mc.getConditions().stream().noneMatch(cv -> cv.isBoundCheckingCondition()))
.anyMatch(mc -> mc.getReturnValue() == MethodContract.ValueConstraint.THROW_EXCEPTION);
}
private static boolean isSideEffectFreeConstructor(@NotNull PsiNewExpression newExpression) {
@@ -281,4 +281,16 @@ class Test {
}
"""
}
void testOptionalOrElseThrow() {
doTest """
import java.util.Optional;
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
class Test {
void test(Optional<String> opt) {
opt.orElseThrow(RuntimeException::new);
}
}"""
}
}