IDEA-75018 ("Unclear Binary Expression" Code Inspection)

This commit is contained in:
Bas Leijdekkers
2011-10-04 14:13:10 +02:00
parent 6247eb5b98
commit 735da93290
6 changed files with 272 additions and 1 deletions
@@ -5,7 +5,6 @@
<category>inspection</category>
<version>1.0</version>
<idea-version min="4.0" max="6.0"/>
<resource-bundle>com.siyeh.InspectionGadgetsBundle</resource-bundle>
@@ -2219,6 +2218,11 @@
key="unqualified.static.usage.display.name" groupBundle="messages.InspectionsBundle"
groupKey="group.names.code.style.issues" enabledByDefault="false" level="WARNING"
implementationClass="com.siyeh.ig.style.UnqualifiedStaticUsageInspection"/>
<localInspection language="JAVA" shortName="UnclearBinaryExpression" bundle="com.siyeh.InspectionGadgetsBundle"
key="unclear.binary.expression.display.name" groupBundle="messages.InspectionsBundle"
groupKey="group.names.code.style.issues" enabledByDefault="false" level="WARNING"
implementationClass="com.siyeh.ig.style.UnclearBinaryExpressionInspection"/>
<localInspection language="JAVA" suppressId="AccessToNonThreadSafeStaticField" shortName="AccessToNonThreadSafeStaticFieldFromInstance"
bundle="com.siyeh.InspectionGadgetsBundle" key="access.to.non.thread.safe.static.field.from.instance.display.name"
groupBundle="messages.InspectionsBundle" groupKey="group.names.threading.issues" enabledByDefault="false"
@@ -1946,3 +1946,6 @@ properties.object.as.hashtable.get.quickfix=Replace with call to 'getProperty()'
ignored.junit.test.display.name=JUnit test annotated with '@Ignore'
ignored.junit.test.classproblem.descriptor=Test class ''{0}'' annotated with <code>#ref</code>
ignored.junit.test.method.problem.descriptor=Test method ''{0}()'' annotated with <code>#ref</code>
unclear.binary.expression.display.name=Unclear binary expression
unclear.binary.expression.problem.descriptor=Expression could use clarifying parentheses #loc
unclear.binary.expression.quickfix=Add clarifying parentheses
@@ -0,0 +1,179 @@
/*
* Copyright 2011 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.style;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
public class UnclearBinaryExpressionInspection extends BaseInspection {
@Nls
@NotNull
@Override
public String getDisplayName() {
return InspectionGadgetsBundle.message("unclear.binary.expression.display.name");
}
@NotNull
@Override
protected String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message("unclear.binary.expression.problem.descriptor");
}
@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
return new UnclearBinaryExpressionFix();
}
private static class UnclearBinaryExpressionFix extends InspectionGadgetsFix {
@NotNull
@Override
public String getName() {
return InspectionGadgetsBundle.message("unclear.binary.expression.quickfix");
}
@Override
protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
final PsiElement element = descriptor.getPsiElement();
if (!(element instanceof PsiPolyadicExpression)) {
return;
}
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)element;
final StringBuilder newExpressionText = createReplacementText(polyadicExpression, new StringBuilder());
replaceExpression(polyadicExpression, newExpressionText.toString());
}
private static StringBuilder createReplacementText(PsiExpression expression, StringBuilder out) {
if (expression instanceof PsiPolyadicExpression) {
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)expression;
final IElementType tokenType = polyadicExpression.getOperationTokenType();
final PsiElement parent = expression.getParent();
if (parent instanceof PsiPolyadicExpression) {
final PsiPolyadicExpression parentPolyadicExpression = (PsiPolyadicExpression)parent;
final IElementType parentOperationSign = parentPolyadicExpression.getOperationTokenType();
if (!tokenType.equals(parentOperationSign)) {
out.append('(');
createText(polyadicExpression, out);
out.append(')');
return out;
}
} else if (parent instanceof PsiConditionalExpression || parent instanceof PsiInstanceOfExpression) {
out.append('(');
createText(polyadicExpression, out);
out.append(')');
return out;
}
createText(polyadicExpression, out);
}
else if (expression instanceof PsiParenthesizedExpression) {
final PsiParenthesizedExpression parenthesizedExpression = (PsiParenthesizedExpression)expression;
final PsiExpression unwrappedExpression = parenthesizedExpression.getExpression();
out.append('(');
createReplacementText(unwrappedExpression, out);
out.append(')');
}
else if (expression instanceof PsiInstanceOfExpression) {
out.append('(');
out.append(expression.getText());
out.append(')');
}
else if (expression != null) {
out.append(expression.getText());
}
return out;
}
private static void createText(PsiPolyadicExpression polyadicExpression, StringBuilder out) {
final PsiExpression[] operands = polyadicExpression.getOperands();
for (PsiExpression operand : operands) {
if (operand == null) {
continue;
}
if (operand.getType() == PsiType.VOID) {
throw new ProcessCanceledException();
}
if (operands.length == 1) {
createReplacementText(operand, out);
}
final PsiJavaToken token = polyadicExpression.getTokenBeforeOperand(operand);
if (token != null) {
final PsiElement beforeToken = token.getPrevSibling();
if (beforeToken instanceof PsiWhiteSpace) {
out.append(beforeToken.getText());
}
out.append(token.getText());
final PsiElement afterToken = token.getNextSibling();
if (afterToken instanceof PsiWhiteSpace) {
out.append(afterToken.getText());
}
}
if (operands.length != 1) {
createReplacementText(operand, out);
}
}
}
}
@Override
public BaseInspectionVisitor buildVisitor() {
return new UnclearBinaryExpressionVisitor();
}
private static class UnclearBinaryExpressionVisitor extends BaseInspectionVisitor {
@Override
public void visitPolyadicExpression(PsiPolyadicExpression expression) {
final PsiElement parent = expression.getParent();
if (parent instanceof PsiInstanceOfExpression ||
parent instanceof PsiConditionalExpression) {
registerError(expression);
return;
}
if (parent instanceof PsiPolyadicExpression) {
return;
}
final IElementType tokenType = expression.getOperationTokenType();
final PsiExpression[] operands = expression.getOperands();
for (PsiExpression operand : operands) {
if (operand instanceof PsiInstanceOfExpression) {
registerError(expression);
return;
}
if (!(operand instanceof PsiPolyadicExpression)) {
continue;
}
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)operand;
final IElementType childTokenType = polyadicExpression.getOperationTokenType();
if (!tokenType.equals(childTokenType)) {
registerError(expression);
return;
}
}
super.visitPolyadicExpression(expression);
}
}
}
@@ -0,0 +1,44 @@
/*
* Copyright 2011 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.igtest.style.unclear_binary_expression;
import java.util.ArrayList;
import java.util.List;
public class UnclearBinaryExpression {
void foo() {
boolean b2 = "asdf" + "asdf" instanceof String;
int i = true ? 1 + 2 * 7 : 2;
boolean j = true ? false : true;
System.out.println(3 + 1 + 2 * 9 * 8 + 1);
}
boolean bar(String name, Condition condition, Operation operation) {
List<String> values = new ArrayList();
return name.equals(condition.name)
&& values.contains(condition.value) == (operation == Operation.equals)
&& name instanceof String;
}
class Condition {
String name;
String value;
}
static class Operation {
static Operation equals;
}
}
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>UnclearBinaryExpression.java</file>
<line>32</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unclear binary expression</problem_class>
<description>Expression could use clarifying parentheses #loc</description>
</problem>
<problem>
<file>UnclearBinaryExpression.java</file>
<line>24</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unclear binary expression</problem_class>
<description>Expression could use clarifying parentheses #loc</description>
</problem>
<problem>
<file>UnclearBinaryExpression.java</file>
<line>25</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unclear binary expression</problem_class>
<description>Expression could use clarifying parentheses #loc</description>
</problem>
<problem>
<file>UnclearBinaryExpression.java</file>
<line>27</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unclear binary expression</problem_class>
<description>Expression could use clarifying parentheses #loc</description>
</problem>
</problems>
@@ -0,0 +1,11 @@
package com.siyeh.ig.style;
import com.siyeh.ig.IGInspectionTestCase;
public class UnclearBinaryExpressionInspectionTest extends IGInspectionTestCase {
public void test() throws Exception {
doTest("com/siyeh/igtest/style/unclear_binary_expression",
new UnclearBinaryExpressionInspection());
}
}