Merge remote-tracking branch 'origin/master'

This commit is contained in:
Dmitry Jemerov
2012-05-17 22:43:39 +02:00
8 changed files with 95 additions and 57 deletions
@@ -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;
}
@@ -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;
@@ -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));
}
}
}
@@ -2,7 +2,7 @@
<body>
This inspection reports on any calls to <b>String.valueOf()</b>
used in string concatenations. The conversion to string is handled automatically by the compiler
without a call to <b>String.valueOf()</b>, thus it is not needed.
without a call to <b>String.valueOf()</b>, making it unnecessary.
<p>
<small>Powered by InspectionGadgets</small>
</body>
@@ -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
}
}
@@ -41,4 +41,12 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Assignment replaceable with operator assignment</problem_class>
<description>&lt;code&gt;x = x * 2 * 2&lt;/code&gt; could be simplified to 'x *= 2 * 2' #loc</description>
</problem>
<problem>
<file>ReplaceAssignmentWithOperatorAssignment.java</file>
<line>33</line>
<entry_point TYPE="method" FQNAME="com.siyeh.igtest.assignment.replace_assignment_with_operator_assignment.ReplaceAssignmentWithOperatorAssignment void foo()" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Assignment replaceable with operator assignment</problem_class>
<description>&lt;code&gt;(a) = (int)(a + (d - d))&lt;/code&gt; could be simplified to '(a) += (d - d)' #loc</description>
</problem>
</problems>
@@ -17,4 +17,8 @@ public class UnnecessaryCallToStringValueOf {
System.out.println(String.valueOf(d) + c);
}
void polyadic(String s) {
s = "abc" + String.valueOf('d') + "efg";
}
}
@@ -8,4 +8,11 @@
<description>&lt;code&gt;String.valueOf(7)&lt;/code&gt; can be simplified to '7' #loc</description>
</problem>
<problem>
<file>UnnecessaryCallToStringValueOf.java</file>
<line>21</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary call to 'String.valueOf()'</problem_class>
<description>&lt;code&gt;String.valueOf('d')&lt;/code&gt; can be simplified to ''d'' #loc</description>
</problem>
</problems>