mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-211702 Incorrect inspection "Can be replaced with single expression in functional style" when types differ
GitOrigin-RevId: 4006edb212486fa38b31b26ecbed881e6079f6e6
This commit is contained in:
committed by
intellij-monorepo-bot
parent
3ddbffb2db
commit
61ac76cf48
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+14
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user