RedundantCastUtil: report safe widening primitive casts

Fixes IDEA-154832 IDEA add unnecessary cast to double on method inline

GitOrigin-RevId: 1e8dbd1466f74bdab9584a860f4e188b025fba1c
This commit is contained in:
Tagir Valeev
2019-04-23 13:15:33 +03:00
committed by intellij-monorepo-bot
parent 505bdd1c87
commit bfde8b00af
6 changed files with 45 additions and 1 deletions
@@ -761,7 +761,7 @@ public class RedundantCastUtil {
PsiType castType = typeElement.getType();
if (castType instanceof PsiPrimitiveType) {
if (opType instanceof PsiPrimitiveType) {
return !opType.equals(castType); // let's suppose all not equal primitive casts are necessary
return !TypeConversionUtil.isSafeConversion(castType, opType); // let's suppose that casts losing precision are important
}
final PsiPrimitiveType unboxedOpType = PsiPrimitiveType.getUnboxedType(opType);
if (unboxedOpType != null && !unboxedOpType.equals(castType) ) {
@@ -0,0 +1,18 @@
import org.jetbrains.annotations.*;
class A {
double widen(int x) {
return (<warning descr="Casting 'x' to 'double' is redundant">double</warning>) x;
}
double widenDataLoss(long x) {
return (double) x;
}
void call(short a, byte b, long c) {
widen((<warning descr="Casting 'a' to 'int' is redundant">int</warning>)a);
widen((<warning descr="Casting 'b' to 'int' is redundant">int</warning>)b);
widen((int)c);
}
}
@@ -0,0 +1,12 @@
class UnnecessaryDoubleCast {
public void check() {
<caret>eq(25, 25.49);
}
private static void eq(double expected, double actual) {
assertDoubleEquals(expected, actual);
}
public static void assertDoubleEquals(double v, double v2) {
}
}
@@ -0,0 +1,8 @@
class UnnecessaryDoubleCast {
public void check() {
assertDoubleEquals(25, 25.49);
}
public static void assertDoubleEquals(double v, double v2) {
}
}
@@ -78,4 +78,6 @@ public class RedundantCast15Test extends LightCodeInsightFixtureTestCase {
myFixture.testHighlighting(getTestName(false) + ".java");
}
public void testDifferentNullness() { doTest();}
public void testPrimitiveWidening() { doTest(); }
}
@@ -422,6 +422,10 @@ public class InlineMethodTest extends LightRefactoringTestCase {
public void testPrivateFieldInSuperClassInSameFile() {
doTest();
}
public void testWidenArgument() {
doTest();
}
public void testInlineMultipleOccurrencesInFieldInitializer() {
doTest();