Restore cast in possibly important context; fix ReplaceInefficientStreamCount tests

GitOrigin-RevId: 84ef59c2941462b3060c31cfea85f90ee6d39af8
This commit is contained in:
Tagir Valeev
2019-04-23 13:16:29 +03:00
committed by intellij-monorepo-bot
parent bec9b23504
commit 7bc2b6c529
4 changed files with 10 additions and 4 deletions
@@ -766,7 +766,13 @@ public class RedundantCastUtil {
PsiType castType = typeElement.getType();
if (castType instanceof PsiPrimitiveType) {
if (opType instanceof PsiPrimitiveType) {
return !TypeConversionUtil.isSafeConversion(castType, opType); // let's suppose that casts losing precision are important
PsiElement parent = PsiUtil.skipParenthesizedExprUp(typeCast.getParent());
if (parent instanceof PsiReturnStatement || parent instanceof PsiMethodCallExpression || parent instanceof PsiVariable ||
parent instanceof PsiAssignmentExpression) {
return !TypeConversionUtil.isSafeConversion(castType, opType); // let's suppose that casts losing precision are important
} else {
return !castType.equals(opType); // cast might be necessary (e.g. ((double)1)/5)
}
}
final PsiPrimitiveType unboxedOpType = PsiPrimitiveType.getUnboxedType(opType);
if (unboxedOpType != null && !unboxedOpType.equals(castType) ) {
@@ -5,6 +5,6 @@ import java.util.Arrays;
class Test {
long cnt() {
/*count*/
return (long) Arrays.asList('d', 'e', 'f')./*stream*/size()/*after*/;
return Arrays.asList('d', 'e', 'f')./*stream*/size()/*after*/;
}
}
@@ -4,5 +4,5 @@ import java.util.Arrays;
class Test {
/*count*/
long cnt = (long) Arrays.asList('d', 'e', 'f')./*stream*/size()/*after*/;
long cnt = Arrays.asList('d', 'e', 'f')./*stream*/size()/*after*/;
}
@@ -7,6 +7,6 @@ public class Main {
public static long test() {
List<String> s = new ArrayList<>();
/* unused parameter */
return (long) s.size();
return s.size();
}
}