IDEA-211702 Incorrect inspection "Can be replaced with single expression in functional style" when types differ

GitOrigin-RevId: 4006edb212486fa38b31b26ecbed881e6079f6e6
This commit is contained in:
Tagir Valeev
2019-04-29 13:28:39 +03:00
committed by intellij-monorepo-bot
parent 3ddbffb2db
commit 61ac76cf48
2 changed files with 24 additions and 4 deletions
@@ -191,10 +191,16 @@ public class OptionalIsPresentInspection extends AbstractBaseJavaLocalInspection
}
PsiType falseType = falseExpression.getType();
PsiType trueType = expression.getType();
// like x ? double_expression : integer_expression; support only if integer_expression is simple literal,
// so could be converted explicitly to double
if (falseType instanceof PsiPrimitiveType && trueType instanceof PsiPrimitiveType &&
!falseType.equals(trueType) && JavaPsiMathUtil.getNumberFromLiteral(falseExpression) == null) {
if (falseType == null || trueType == null) return ProblemType.NONE;
if (falseType instanceof PsiPrimitiveType && trueType instanceof PsiPrimitiveType) {
if (falseType.equals(trueType) || JavaPsiMathUtil.getNumberFromLiteral(falseExpression) != null) {
// like x ? double_expression : integer_expression; support only if integer_expression is simple literal,
// so could be converted explicitly to double
return ProblemType.WARNING;
}
return ProblemType.NONE;
}
if (!trueType.isAssignableFrom(falseType)) {
return ProblemType.NONE;
}
}
@@ -0,0 +1,14 @@
// "Replace Optional.isPresent() condition with functional style expression" "false"
import java.util.Optional;
class Test {
Object get() {
Optional<String> obj = Stream.of("one", "two").filter(s -> Math.sqrt(s.length()) > 100).findFirst();
if (obj.<caret>isPresent()) {
return obj.get();
} else {
return 7;
}
}
}