mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-101944 ("Pointless boolean expression" inspection with multiple '&&' fails)
This commit is contained in:
+16
-6
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
|
||||
* Copyright 2003-2013 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.
|
||||
@@ -183,17 +183,19 @@ public class PointlessBooleanExpressionInspection extends BaseInspection {
|
||||
}
|
||||
buildSimplifiedExpression(expressions, "==", negate, out);
|
||||
}
|
||||
else {
|
||||
out.append(expression.getText());
|
||||
}
|
||||
}
|
||||
|
||||
private void buildSimplifiedExpression(List<PsiExpression> expressions, String token, boolean negate, StringBuilder out) {
|
||||
if (expressions.size() == 1) {
|
||||
final PsiExpression expression = expressions.get(0);
|
||||
final String expressionText = expression.getText();
|
||||
if (isBoxedTypeComparison(token, expression)) {
|
||||
out.append(expressionText).append(" != null && ");
|
||||
out.append(expression.getText()).append(" != null && ");
|
||||
}
|
||||
if (!negate) {
|
||||
out.append(expressionText);
|
||||
out.append(expression.getText());
|
||||
return;
|
||||
}
|
||||
if (ComparisonUtils.isComparison(expression)) {
|
||||
@@ -206,10 +208,10 @@ public class PointlessBooleanExpressionInspection extends BaseInspection {
|
||||
}
|
||||
else {
|
||||
if (ParenthesesUtils.getPrecedence(expression) > ParenthesesUtils.PREFIX_PRECEDENCE) {
|
||||
out.append("!(").append(expressionText).append(')');
|
||||
out.append("!(").append(expression.getText()).append(')');
|
||||
}
|
||||
else {
|
||||
out.append('!').append(expressionText);
|
||||
out.append('!').append(expression.getText());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -221,11 +223,19 @@ public class PointlessBooleanExpressionInspection extends BaseInspection {
|
||||
for (PsiExpression expression : expressions) {
|
||||
if (useToken) {
|
||||
out.append(token);
|
||||
final PsiElement previousSibling = expression.getPrevSibling();
|
||||
if (previousSibling instanceof PsiWhiteSpace) {
|
||||
out.append(previousSibling.getText());
|
||||
}
|
||||
}
|
||||
else {
|
||||
useToken = true;
|
||||
}
|
||||
buildSimplifiedExpression(expression, out);
|
||||
final PsiElement nextSibling = expression.getNextSibling();
|
||||
if (nextSibling instanceof PsiWhiteSpace) {
|
||||
out.append(nextSibling.getText());
|
||||
}
|
||||
}
|
||||
if (negate) {
|
||||
out.append(')');
|
||||
|
||||
+7
@@ -28,4 +28,11 @@ class PointlessBooleanExpression {
|
||||
boolean y = false || c;
|
||||
boolean z = b != true;
|
||||
}
|
||||
}
|
||||
class Presley {
|
||||
void elvis(Object king) {
|
||||
if (true && king != null && king.hashCode() > 1) {
|
||||
// blah
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
@@ -42,4 +42,11 @@
|
||||
<description><code>b != true</code> can be simplified to '!b' #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>PointlessBooleanExpression.java</file>
|
||||
<line>34</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless boolean expression</problem_class>
|
||||
<description><code>true && king != null && king.hashCode() > 1</code> can be simplified to 'king != null && king.hashCode() > 1' #loc</description>
|
||||
</problem>
|
||||
|
||||
</problems>
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2012 JetBrains s.r.o.
|
||||
* Copyright 2000-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -25,6 +25,6 @@ public class PointlessBooleanExpressionInspectionTest extends IGInspectionTestCa
|
||||
public void test() throws Exception {
|
||||
final PointlessBooleanExpressionInspection inspection = new PointlessBooleanExpressionInspection();
|
||||
inspection.m_ignoreExpressionsContainingConstants = true;
|
||||
doTest("com/siyeh/igtest/controlflow/pointless_boolean_expression_ignore_cont_const", inspection);
|
||||
doTest("com/siyeh/igtest/controlflow/pointless_boolean_expression", inspection);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user