IDEA-177515 Optional.isPresent() refactoring should not be suggested for ternary with incompatible branch types

This commit is contained in:
Tagir Valeev
2017-08-14 17:51:24 +07:00
parent 04ff01385d
commit b1d286f8f6
2 changed files with 20 additions and 2 deletions
@@ -368,8 +368,12 @@ public class OptionalIsPresentInspection extends BaseJavaBatchLocalInspectionToo
if(!(trueElement instanceof PsiExpression) || !(falseElement instanceof PsiExpression)) return ProblemType.NONE;
PsiExpression trueExpression = (PsiExpression)trueElement;
PsiExpression falseExpression = (PsiExpression)falseElement;
return (isSimpleOrUnchecked(falseExpression)) ?
getTypeByLambdaCandidate(optionalVariable, trueExpression, falseExpression) : ProblemType.NONE;
PsiType trueType = trueExpression.getType();
PsiType falseType = falseExpression.getType();
if (trueType == null || falseType == null || !trueType.isAssignableFrom(falseType) || !isSimpleOrUnchecked(falseExpression)) {
return ProblemType.NONE;
}
return getTypeByLambdaCandidate(optionalVariable, trueExpression, falseExpression);
}
@Override
@@ -0,0 +1,14 @@
// "Replace Optional.isPresent() condition with functional style expression" "false"
import java.time.LocalDate;
import java.util.Optional;
public class OptionalTest {
Optional<LocalDate> date = Optional.of(LocalDate.now());
@Override
public String toString() {
final String x = "{date:" + (date.isPres<caret>ent() ? date.get() : "<missing>") + '}';
return x;
}
}