diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/assignment/ReplaceAssignmentWithOperatorAssignmentInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/assignment/ReplaceAssignmentWithOperatorAssignmentInspection.java index e96975c718b7..3a6bef7275aa 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/assignment/ReplaceAssignmentWithOperatorAssignmentInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/assignment/ReplaceAssignmentWithOperatorAssignmentInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2011 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2012 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,6 @@ import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; import com.intellij.openapi.project.Project; import com.intellij.psi.*; import com.intellij.psi.tree.IElementType; -import com.intellij.psi.util.PsiUtil; import com.intellij.util.IncorrectOperationException; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; @@ -151,7 +150,15 @@ public class ReplaceAssignmentWithOperatorAssignmentInspection extends BaseInspe } final PsiAssignmentExpression expression = (PsiAssignmentExpression)element; final PsiExpression lhs = expression.getLExpression(); - final PsiExpression rhs = expression.getRExpression(); + PsiExpression rhs = ParenthesesUtils.stripParentheses(expression.getRExpression()); + if (rhs instanceof PsiTypeCastExpression) { + final PsiTypeCastExpression typeCastExpression = (PsiTypeCastExpression)rhs; + final PsiType castType = typeCastExpression.getType(); + if (castType == null || !castType.equals(lhs.getType())) { + return; + } + rhs = ParenthesesUtils.stripParentheses(typeCastExpression.getOperand()); + } if (!(rhs instanceof PsiPolyadicExpression)) { return; } @@ -176,7 +183,15 @@ public class ReplaceAssignmentWithOperatorAssignmentInspection extends BaseInspe return; } final PsiExpression lhs = assignment.getLExpression(); - final PsiExpression rhs = PsiUtil.deparenthesizeExpression(assignment.getRExpression()); + PsiExpression rhs = ParenthesesUtils.stripParentheses(assignment.getRExpression()); + if (rhs instanceof PsiTypeCastExpression) { + final PsiTypeCastExpression typeCastExpression = (PsiTypeCastExpression)rhs; + final PsiType castType = typeCastExpression.getType(); + if (castType == null || !castType.equals(lhs.getType())) { + return; + } + rhs = ParenthesesUtils.stripParentheses(typeCastExpression.getOperand()); + } if (!(rhs instanceof PsiPolyadicExpression)) { return; } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ResultSetIndexZeroInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ResultSetIndexZeroInspection.java index e6d52f074474..626d4508e731 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ResultSetIndexZeroInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ResultSetIndexZeroInspection.java @@ -15,8 +15,10 @@ */ package com.siyeh.ig.bugs; -import com.intellij.psi.*; -import com.intellij.psi.util.PsiUtil; +import com.intellij.psi.PsiExpression; +import com.intellij.psi.PsiExpressionList; +import com.intellij.psi.PsiMethodCallExpression; +import com.intellij.psi.PsiReferenceExpression; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; @@ -78,12 +80,6 @@ public class ResultSetIndexZeroInspection extends BaseInspection { return; } final PsiExpression argument = arguments[0]; - if (!TypeUtils.expressionHasType(argument, PsiKeyword.INT)) { - return; - } - if (!PsiUtil.isConstantExpression(argument)) { - return; - } final Object val = ExpressionUtils.computeConstantExpression(argument); if (!(val instanceof Integer) || ((Integer)val).intValue() != 0) { return; diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/style/UnnecessaryCallToStringValueOfInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/style/UnnecessaryCallToStringValueOfInspection.java index dcfb27f19cc5..4c75ce15866d 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/style/UnnecessaryCallToStringValueOfInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/style/UnnecessaryCallToStringValueOfInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2010 Bas Leijdekkers + * Copyright 2008-2012 Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; import com.siyeh.ig.InspectionGadgetsFix; +import com.siyeh.ig.psiutils.ParenthesesUtils; import com.siyeh.ig.psiutils.TypeUtils; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; @@ -34,28 +35,36 @@ public class UnnecessaryCallToStringValueOfInspection extends BaseInspection { @Nls @NotNull public String getDisplayName() { - return InspectionGadgetsBundle.message( - "unnecessary.call.to.string.valueof.display.name"); + return InspectionGadgetsBundle.message("unnecessary.call.to.string.valueof.display.name"); } @Override @NotNull protected String buildErrorString(Object... infos) { - final PsiExpression expression = (PsiExpression)infos[0]; - return InspectionGadgetsBundle.message( - "unnecessary.call.to.string.valueof.problem.descriptor", - expression.getText()); + final String text = (String)infos[0]; + return InspectionGadgetsBundle.message("unnecessary.call.to.string.valueof.problem.descriptor", text); } @Override @Nullable protected InspectionGadgetsFix buildFix(Object... infos) { - final PsiExpression expression = (PsiExpression)infos[0]; - return new UnnecessaryCallToStringValueOfFix(expression.getText()); + final String text = (String)infos[0]; + return new UnnecessaryCallToStringValueOfFix(text); } - private static class UnnecessaryCallToStringValueOfFix - extends InspectionGadgetsFix { + public static String calculateReplacementText(PsiExpression expression) { + if (!(expression instanceof PsiPolyadicExpression)) { + return expression.getText(); + } + final PsiType type = expression.getType(); + if (TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, type) || + ParenthesesUtils.getPrecedence(expression) < ParenthesesUtils.ADDITIVE_PRECEDENCE) { + return expression.getText(); + } + return '(' + expression.getText() + ')'; + } + + private static class UnnecessaryCallToStringValueOfFix extends InspectionGadgetsFix { private final String replacementText; @@ -65,24 +74,18 @@ public class UnnecessaryCallToStringValueOfInspection extends BaseInspection { @NotNull public String getName() { - return InspectionGadgetsBundle.message( - "unnecessary.call.to.string.valueof.quickfix", - replacementText); + return InspectionGadgetsBundle.message("unnecessary.call.to.string.valueof.quickfix", replacementText); } @Override - protected void doFix(Project project, ProblemDescriptor descriptor) - throws IncorrectOperationException { - final PsiMethodCallExpression methodCallExpression = - (PsiMethodCallExpression)descriptor.getPsiElement(); - final PsiExpressionList argumentList = - methodCallExpression.getArgumentList(); + protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { + final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)descriptor.getPsiElement(); + final PsiExpressionList argumentList = methodCallExpression.getArgumentList(); final PsiExpression[] arguments = argumentList.getExpressions(); if (arguments.length != 1) { return; } - final PsiExpression argument = arguments[0]; - methodCallExpression.replace(argument); + replaceExpression(methodCallExpression, calculateReplacementText(arguments[0])); } } @@ -91,41 +94,42 @@ public class UnnecessaryCallToStringValueOfInspection extends BaseInspection { return new UnnecessaryCallToStringValueOfVisitor(); } - private static class UnnecessaryCallToStringValueOfVisitor - extends BaseInspectionVisitor { + private static class UnnecessaryCallToStringValueOfVisitor extends BaseInspectionVisitor { @Override - public void visitMethodCallExpression( - PsiMethodCallExpression expression) { + public void visitMethodCallExpression(PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); - final PsiReferenceExpression methodExpression = - expression.getMethodExpression(); + final PsiReferenceExpression methodExpression = expression.getMethodExpression(); final String referenceName = methodExpression.getReferenceName(); if (!"valueOf".equals(referenceName)) { return; } final PsiElement parent = expression.getParent(); - if (!(parent instanceof PsiBinaryExpression)) { + if (!(parent instanceof PsiPolyadicExpression)) { return; } - final PsiBinaryExpression binaryExpression = - (PsiBinaryExpression)parent; - final PsiType type = binaryExpression.getType(); - if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, - type)) { + final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)parent; + final PsiType type = polyadicExpression.getType(); + if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, type)) { return; } - final PsiExpression lhs = binaryExpression.getLOperand(); - if (lhs == expression) { - final PsiExpression rhs = binaryExpression.getROperand(); - if (rhs == null || !TypeUtils.typeEquals( - CommonClassNames.JAVA_LANG_STRING, - rhs.getType())) { - return; + final PsiExpression[] operands = polyadicExpression.getOperands(); + int index = -1; + for (int i = 0, length = operands.length; i < length; i++) { + final PsiExpression operand = operands[i]; + if (expression.equals(operand)) { + index = i; } } - else if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, - lhs.getType())) { + if (index > 0) { + if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, operands[index - 1].getType())) { + return; + } + } else if (operands.length > 1) { + if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, operands[index + 1].getType())) { + return; + } + } else { return; } final PsiExpressionList argumentList = expression.getArgumentList(); @@ -154,7 +158,7 @@ public class UnnecessaryCallToStringValueOfInspection extends BaseInspection { if (!CommonClassNames.JAVA_LANG_STRING.equals(qualifiedName)) { return; } - registerError(expression, argument); + registerError(expression, calculateReplacementText(argument)); } } } diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessaryCallToStringValueOf.html b/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessaryCallToStringValueOf.html index dbcc81a1cc25..8da882fb6eab 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessaryCallToStringValueOf.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessaryCallToStringValueOf.html @@ -2,7 +2,7 @@ This inspection reports on any calls to String.valueOf() used in string concatenations. The conversion to string is handled automatically by the compiler -without a call to String.valueOf(), thus it is not needed. +without a call to String.valueOf(), making it unnecessary.

Powered by InspectionGadgets diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/assignment/replace_assignment_with_operator_assignment/ReplaceAssignmentWithOperatorAssignment.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/assignment/replace_assignment_with_operator_assignment/ReplaceAssignmentWithOperatorAssignment.java index 6d95a4c788bf..fbd91e165c47 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/assignment/replace_assignment_with_operator_assignment/ReplaceAssignmentWithOperatorAssignment.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/assignment/replace_assignment_with_operator_assignment/ReplaceAssignmentWithOperatorAssignment.java @@ -27,5 +27,9 @@ public class ReplaceAssignmentWithOperatorAssignment x = x * 2 * 2; float f = 1; f = f * 2 * 2; + int a = Integer.MAX_VALUE; + double d = Double.MAX_VALUE; + (a) = (byte)(a + (d - d));// should not warn here + (a) = (int)(a + (d - d));// should warn here } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/assignment/replace_assignment_with_operator_assignment/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/assignment/replace_assignment_with_operator_assignment/expected.xml index 0a0d8534142e..7b2de4634c31 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/assignment/replace_assignment_with_operator_assignment/expected.xml +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/assignment/replace_assignment_with_operator_assignment/expected.xml @@ -41,4 +41,12 @@ Assignment replaceable with operator assignment <code>x = x * 2 * 2</code> could be simplified to 'x *= 2 * 2' #loc + + + ReplaceAssignmentWithOperatorAssignment.java + 33 + + Assignment replaceable with operator assignment + <code>(a) = (int)(a + (d - d))</code> could be simplified to '(a) += (d - d)' #loc + \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_valueof/UnnecessaryCallToStringValueOf.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_valueof/UnnecessaryCallToStringValueOf.java index 6a072626205e..cf3f87cfae7a 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_valueof/UnnecessaryCallToStringValueOf.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_valueof/UnnecessaryCallToStringValueOf.java @@ -17,4 +17,8 @@ public class UnnecessaryCallToStringValueOf { System.out.println(String.valueOf(d) + c); } + void polyadic(String s) { + s = "abc" + String.valueOf('d') + "efg"; + } + } \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_valueof/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_valueof/expected.xml index 13d3e417709d..315e44ea81b7 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_valueof/expected.xml +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_valueof/expected.xml @@ -8,4 +8,11 @@ <code>String.valueOf(7)</code> can be simplified to '7' #loc + + UnnecessaryCallToStringValueOf.java + 21 + Unnecessary call to 'String.valueOf()' + <code>String.valueOf('d')</code> can be simplified to ''d'' #loc + + \ No newline at end of file