IDEA-116944 Constant Conditions & Exceptions doesn't report if statements with literal true/false condition

This commit is contained in:
peter
2013-12-15 20:04:52 +01:00
parent c50714a89b
commit cef372d03c
3 changed files with 15 additions and 1 deletions
@@ -103,6 +103,15 @@ public class DataFlowInspectionBase extends BaseJavaBatchLocalInspectionTool {
analyzeCodeBlock(initializer.getBody(), holder, isOnTheFly);
}
@Override
public void visitIfStatement(PsiIfStatement statement) {
PsiExpression condition = statement.getCondition();
if (BranchingInstruction.isBoolConst(condition)) {
LocalQuickFix fix = createSimplifyBooleanExpressionFix(condition, condition.textMatches(PsiKeyword.TRUE));
holder.registerProblem(condition, "Condition is always " + condition.getText(), fix);
}
}
@Override
public void visitAnnotation(PsiAnnotation annotation) {
if (!ControlFlowAnalyzer.ORG_JETBRAINS_ANNOTATIONS_CONTRACT.equals(annotation.getQualifiedName())) return;
@@ -66,7 +66,7 @@ public abstract class BranchingInstruction extends Instruction {
return !isConstTrue && myIsTrueReachable != myIsFalseReachable;
}
private static boolean isBoolConst(PsiElement condition) {
public static boolean isBoolConst(PsiElement condition) {
if (!(condition instanceof PsiLiteralExpression)) return false;
@NonNls String text = condition.getText();
return "true".equals(text) || "false".equals(text);
@@ -20,5 +20,10 @@
<line>49</line>
<description>Switch label 'case 2:' is unreachable</description>
</problem>
<problem>
<file>CaseAndNpe.java</file>
<line>80</line>
<description>Condition is always true</description>
</problem>
</problems>