From 07381116636f3ff3d4d22680525b57367f37e269 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Wed, 26 Jul 2017 09:45:23 +0200 Subject: [PATCH] IG: disable "Assertion can be replaced with if statement" inspection on invalid code (IDEA-176562) --- .../AssertionCanBeIfInspection.java | 43 +++++++++++-------- .../assert_to_if/Incomplete.after.java | 8 ---- .../asserttoif/assert_to_if/Incomplete.java | 2 +- .../AssertionCanBeIfInspectionTest.java | 4 +- 4 files changed, 27 insertions(+), 30 deletions(-) delete mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/asserttoif/assert_to_if/Incomplete.after.java diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/asserttoif/AssertionCanBeIfInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/asserttoif/AssertionCanBeIfInspection.java index 5c3e46886258..ed19c22f91cc 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/asserttoif/AssertionCanBeIfInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/asserttoif/AssertionCanBeIfInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2013 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2017 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. @@ -17,9 +17,7 @@ package com.siyeh.ig.asserttoif; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.openapi.project.Project; -import com.intellij.psi.PsiAssertStatement; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiExpression; +import com.intellij.psi.*; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; @@ -56,24 +54,20 @@ public class AssertionCanBeIfInspection extends BaseInspection { return new AssertToIfFix(); } - private static void doFixImpl(@NotNull PsiElement element) { - final PsiAssertStatement assertStatement = (PsiAssertStatement)element; - @NonNls final StringBuilder newStatement = new StringBuilder(); - final PsiExpression condition = assertStatement.getAssertCondition(); - newStatement.append("if(").append(BoolUtils.getNegatedExpressionText(condition)).append(") throw new java.lang.AssertionError("); - final PsiExpression description = assertStatement.getAssertDescription(); - if (description != null) { - newStatement.append(description.getText()); - } - newStatement.append(");"); - PsiReplacementUtil.replaceStatement(assertStatement, newStatement.toString()); - } - private static class AssertToIfVisitor extends BaseInspectionVisitor { @Override public void visitAssertStatement(PsiAssertStatement assertStatement) { super.visitAssertStatement(assertStatement); - if (assertStatement.getAssertCondition() != null) { + if (assertStatement.getAssertCondition() == null) { + return; + } + if (assertStatement.getLastChild() instanceof PsiErrorElement) { + return; + } + if (isVisibleHighlight(assertStatement)) { + registerStatementError(assertStatement); + } + else { registerError(assertStatement); } } @@ -89,7 +83,18 @@ public class AssertionCanBeIfInspection extends BaseInspection { @Override protected void doFix(Project project, ProblemDescriptor descriptor) { - doFixImpl(descriptor.getPsiElement()); + final PsiElement element = descriptor.getPsiElement(); + final PsiAssertStatement assertStatement = + element instanceof PsiKeyword ? (PsiAssertStatement)element.getParent() : (PsiAssertStatement)element; + final PsiExpression condition = assertStatement.getAssertCondition(); + @NonNls final StringBuilder newStatement = + new StringBuilder("if(").append(BoolUtils.getNegatedExpressionText(condition)).append(") throw new java.lang.AssertionError("); + final PsiExpression description = assertStatement.getAssertDescription(); + if (description != null) { + newStatement.append(description.getText()); + } + newStatement.append(");"); + PsiReplacementUtil.replaceStatement(assertStatement, newStatement.toString()); } } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/asserttoif/assert_to_if/Incomplete.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/asserttoif/assert_to_if/Incomplete.after.java deleted file mode 100644 index 763148ba9a6b..000000000000 --- a/plugins/InspectionGadgets/test/com/siyeh/igfixes/asserttoif/assert_to_if/Incomplete.after.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.siyeh.ipp.asserttoif.assert_to_if; - -public class Incomplete { - - void x(Object o) { - if (o == null) throw new AssertionError(); - } -} diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/asserttoif/assert_to_if/Incomplete.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/asserttoif/assert_to_if/Incomplete.java index 9278a952ed52..ab2e7b81eab5 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igfixes/asserttoif/assert_to_if/Incomplete.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/asserttoif/assert_to_if/Incomplete.java @@ -3,6 +3,6 @@ package com.siyeh.ipp.asserttoif.assert_to_if; public class Incomplete { void x(Object o) { - assert o != null : + assert o != null : } } diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/asserttoif/AssertionCanBeIfInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/asserttoif/AssertionCanBeIfInspectionTest.java index badf0bd29e1f..cf63a97af583 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/asserttoif/AssertionCanBeIfInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/asserttoif/AssertionCanBeIfInspectionTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 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. @@ -32,6 +32,6 @@ public class AssertionCanBeIfInspectionTest extends IGQuickFixesTestCase { myDefaultHint = InspectionGadgetsBundle.message("assert.can.be.if.quickfix"); } - public void testIncomplete() { doTest(); } + public void testIncomplete() { assertQuickfixNotAvailable(); } public void testMessage() { doTest(); } }