IDEA-86056 (Unnecessary 'null' check before 'equals()' call quick fix changes the logic)

This commit is contained in:
Bas Leijdekkers
2012-05-16 14:49:38 +02:00
parent 12bae2d705
commit acf43dc967
4 changed files with 91 additions and 5 deletions
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.style;
package com.siyeh.ig.controlflow;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.project.Project;
@@ -71,7 +71,14 @@ public class SimplifiableEqualsExpressionInspection extends BaseInspection {
if (operands.length != 2) {
return;
}
final PsiExpression operand = ParenthesesUtils.stripParentheses(operands[1]);
PsiExpression operand = ParenthesesUtils.stripParentheses(operands[1]);
if (operand instanceof PsiPrefixExpression) {
final PsiPrefixExpression prefixExpression = (PsiPrefixExpression)operand;
if (!JavaTokenType.EXCL.equals(prefixExpression.getOperationTokenType())) {
return;
}
operand = ParenthesesUtils.stripParentheses(prefixExpression.getOperand());
}
if (!(operand instanceof PsiMethodCallExpression)) {
return;
}
@@ -170,7 +177,15 @@ public class SimplifiableEqualsExpressionInspection extends BaseInspection {
return;
}
final PsiExpression rhs = ParenthesesUtils.stripParentheses(operands[1]);
if (!isEqualsConstant(rhs, variable)) {
if (!(rhs instanceof PsiPrefixExpression)) {
return;
}
final PsiPrefixExpression prefixExpression = (PsiPrefixExpression)rhs;
if (!JavaTokenType.EXCL.equals(prefixExpression.getOperationTokenType())) {
return;
}
final PsiExpression operand = ParenthesesUtils.stripParentheses(prefixExpression.getOperand());
if (!isEqualsConstant(operand, variable)) {
return;
}
registerError(lhs);
@@ -226,8 +241,14 @@ public class SimplifiableEqualsExpressionInspection extends BaseInspection {
if (operands.length != 2) {
return null;
}
final PsiExpression lhs = operands[0];
final PsiExpression rhs = operands[1];
final PsiExpression lhs = ParenthesesUtils.stripParentheses(operands[0]);
if (lhs == null) {
return null;
}
final PsiExpression rhs = ParenthesesUtils.stripParentheses(operands[1]);
if (rhs == null) {
return null;
}
if (PsiType.NULL.equals(lhs.getType())) {
if (!(rhs instanceof PsiReferenceExpression)) {
return null;
@@ -0,0 +1,28 @@
package com.siyeh.igtest.controlflow.simplifiable_equals_expression;
public class SimplifiableEqualsExpression {
void foo(String namespace) {
if (namespace != null && namespace.equals("")) {
return;
}
}
void bar(String namespace) {
if (namespace == null || !namespace.equals("")) {
return;
}
}
void baz(Integer i) {
if (i != null && i.equals(1)) {
return;
}
}
void boz(String namespace) {
if (namespace == null || namespace.equals("")) { // don't warn here
return;
}
}
}
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>SimplifiableEqualsExpression.java</file>
<line>12</line>
<entry_point TYPE="method" FQNAME="com.siyeh.igtest.controlflow.simplifiable_equals_expression.SimplifiableEqualsExpression void bar(java.lang.String namespace)" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary 'null' check before 'equals()' call</problem_class>
<description>Unnecessary 'null' check #loc</description>
</problem>
<problem>
<file>SimplifiableEqualsExpression.java</file>
<line>18</line>
<entry_point TYPE="method" FQNAME="com.siyeh.igtest.controlflow.simplifiable_equals_expression.SimplifiableEqualsExpression void baz(java.lang.Integer i)" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary 'null' check before 'equals()' call</problem_class>
<description>Unnecessary 'null' check #loc</description>
</problem>
<problem>
<file>SimplifiableEqualsExpression.java</file>
<line>6</line>
<entry_point TYPE="method" FQNAME="com.siyeh.igtest.controlflow.simplifiable_equals_expression.SimplifiableEqualsExpression void foo(java.lang.String namespace)" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary 'null' check before 'equals()' call</problem_class>
<description>Unnecessary 'null' check #loc</description>
</problem>
</problems>
@@ -0,0 +1,11 @@
package com.siyeh.ig.controlflow;
import com.siyeh.ig.IGInspectionTestCase;
public class SimplifiableEqualsExpressionInspectionTest extends IGInspectionTestCase {
public void test() throws Exception {
doTest("com/siyeh/igtest/controlflow/simplifiable_equals_expression",
new SimplifiableEqualsExpressionInspection());
}
}