better way to check for null literal

This commit is contained in:
Bas Leijdekkers
2014-09-01 11:44:41 +02:00
parent 1852e8e102
commit af137663d1
4 changed files with 12 additions and 17 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2014 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.
@@ -18,7 +18,6 @@ package com.siyeh.ig.bugs;
import com.intellij.psi.PsiBinaryExpression;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiJavaToken;
import com.intellij.psi.PsiKeyword;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
@@ -73,12 +72,7 @@ public class StringEqualityInspection extends BaseInspection {
if (rhs == null || !ExpressionUtils.hasStringType(rhs)) {
return;
}
final String lhsText = lhs.getText();
if (PsiKeyword.NULL.equals(lhsText)) {
return;
}
final String rhsText = rhs.getText();
if (PsiKeyword.NULL.equals(rhsText)) {
if (ExpressionUtils.isNullLiteral(lhs) || ExpressionUtils.isNullLiteral(rhs)) {
return;
}
final PsiJavaToken sign = expression.getOperationSign();
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2013 Bas Leijdekkers
* Copyright 2006-2014 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@ import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.BoolUtils;
import com.siyeh.ig.psiutils.ExpressionUtils;
import com.siyeh.ig.psiutils.IteratorUtils;
import com.siyeh.ig.psiutils.VariableAccessUtils;
import org.jetbrains.annotations.NotNull;
@@ -118,8 +119,7 @@ public class LoopConditionNotUpdatedInsideLoopInspection
if (condition == null) {
return false;
}
if (PsiUtil.isConstantExpression(condition) ||
PsiKeyword.NULL.equals(condition.getText())) {
if (PsiUtil.isConstantExpression(condition) || ExpressionUtils.isNullLiteral(condition)) {
return true;
}
if (condition instanceof PsiInstanceOfExpression) {
@@ -1,5 +1,5 @@
/*
* Copyright 2005-2013 Bas Leijdekkers
* Copyright 2005-2014 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -198,11 +198,8 @@ public class ExpressionUtils {
}
public static boolean isNullLiteral(@Nullable PsiExpression expression) {
if (!(expression instanceof PsiLiteralExpression)) {
return false;
}
final String text = expression.getText();
return PsiKeyword.NULL.equals(text);
expression = ParenthesesUtils.stripParentheses(expression);
return expression != null && PsiType.NULL.equals(expression.getType());
}
public static boolean isZero(@Nullable PsiExpression expression) {
@@ -7,4 +7,8 @@ public class StringEquality {
final boolean b = t <warning descr="String values are compared using '==', not 'equals()'">==</warning> s;
final boolean c = t ==<EOLError descr="Expression expected"></EOLError><EOLError descr="';' expected"></EOLError>
}
void notEquals(String s, String t) {
boolean a = s <warning descr="String values are compared using '!=', not 'equals()'">!=</warning> t;
}
}