diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/UnnecessaryExplicitNumericCastInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/UnnecessaryExplicitNumericCastInspection.java index 7686bf6bb8de..6185124ac1f3 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/UnnecessaryExplicitNumericCastInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/UnnecessaryExplicitNumericCastInspection.java @@ -83,7 +83,7 @@ public class UnnecessaryExplicitNumericCastInspection extends BaseInspection { @Override protected void doFix(Project project, ProblemDescriptor descriptor) { final PsiElement element = descriptor.getPsiElement(); - final PsiElement parent = element.getParent(); + PsiElement parent = element.getParent(); if (!(parent instanceof PsiTypeCastExpression)) { return; } @@ -91,12 +91,17 @@ public class UnnecessaryExplicitNumericCastInspection extends BaseInspection { if (isPrimitiveNumericCastNecessary(typeCastExpression)) { return; } + PsiElement grandParent = parent.getParent(); + while (grandParent instanceof PsiParenthesizedExpression) { + parent = grandParent; + grandParent = parent.getParent(); + } final PsiExpression operand = typeCastExpression.getOperand(); if (operand == null) { - typeCastExpression.delete(); + parent.delete(); } else { - typeCastExpression.replace(operand); + parent.replace(operand); } } } @@ -163,9 +168,20 @@ public class UnnecessaryExplicitNumericCastInspection extends BaseInspection { } if (PsiType.LONG.equals(castType) || PsiType.FLOAT.equals(castType) || PsiType.DOUBLE.equals(castType)) { final PsiExpression[] operands = polyadicExpression.getOperands(); - for (PsiExpression operand1 : operands) { + int expressionIndex = -1; + for (int i = 0; i < operands.length; i++) { + if (expressionIndex == 0 && i > 1) { + return true; + } + final PsiExpression operand1 = operands[i]; if (PsiTreeUtil.isAncestor(operand1, expression, false)) { - continue; + if (i > 0) { + return true; + } + else { + expressionIndex = i; + continue; + } } final PsiType type = operand1.getType(); if (castType.equals(type)) { diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/unnecessary_explicit_numeric_cast/UnnecessaryExplicitNumericCast.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/unnecessary_explicit_numeric_cast/UnnecessaryExplicitNumericCast.java index 762768495d39..03b5c6f32edb 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/unnecessary_explicit_numeric_cast/UnnecessaryExplicitNumericCast.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/unnecessary_explicit_numeric_cast/UnnecessaryExplicitNumericCast.java @@ -101,4 +101,12 @@ class S { return (T) (Object) 0; } + void polyadic() { + int a=1; + int b=2; + System.out.println(((double) a) / b / 10.0); + double c = 3.5; + System.out.println((double)a / c / 10.0); + System.out.println(19/ (double)a / c / 10.0); + } } \ No newline at end of file