From e31945ac57644b922f40bff9ccefe77f473a9768 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Thu, 20 Jul 2017 19:36:53 +0200 Subject: [PATCH] IG: fix IOE when if has no condition (IDEA-176308) --- .../siyeh/ig/asserttoif/IfCanBeAssertionInspection.java | 4 +++- .../src/inspectionDescriptions/IfCanBeAssertion.html | 2 +- .../siyeh/igfixes/asserttoif/if_to_assert/NoCondition.java | 7 +++++++ .../fixes/asserttoif/IfCanBeAssertionInspectionTest.java | 3 ++- 4 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/asserttoif/if_to_assert/NoCondition.java diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/asserttoif/IfCanBeAssertionInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/asserttoif/IfCanBeAssertionInspection.java index 0330b4aa8427..02e6779c7b37 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/asserttoif/IfCanBeAssertionInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/asserttoif/IfCanBeAssertionInspection.java @@ -78,7 +78,9 @@ public class IfCanBeAssertionInspection extends BaseInspection { @Override public void visitIfStatement(PsiIfStatement statement) { super.visitIfStatement(statement); - if (statement.getElseBranch() == null && getThrownNewException(statement.getThenBranch()) != null) { + if (statement.getCondition() != null && + statement.getElseBranch() == null && + getThrownNewException(statement.getThenBranch()) != null) { registerStatementError(statement); } } diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/IfCanBeAssertion.html b/plugins/InspectionGadgets/src/inspectionDescriptions/IfCanBeAssertion.html index 2519f12a6c0e..75224616cdff 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/IfCanBeAssertion.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/IfCanBeAssertion.html @@ -1,6 +1,6 @@ -This inspection reports if statements (with no else branch) throwing java.lang.Throwable.
+Reports if statements (with no else branch) throwing java.lang.Throwable.
For example:
if (param == 2) throw new Exception(); diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/asserttoif/if_to_assert/NoCondition.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/asserttoif/if_to_assert/NoCondition.java new file mode 100644 index 000000000000..b06e622d9183 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/asserttoif/if_to_assert/NoCondition.java @@ -0,0 +1,7 @@ +class NoCondition { + void m(Object o) { + if () { + throw new NullPointerException("wtf?"); + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/asserttoif/IfCanBeAssertionInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/asserttoif/IfCanBeAssertionInspectionTest.java index 5f195b9f6cd4..1900224016d0 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/asserttoif/IfCanBeAssertionInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/asserttoif/IfCanBeAssertionInspectionTest.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. @@ -35,4 +35,5 @@ public class IfCanBeAssertionInspectionTest extends IGQuickFixesTestCase { public void testRandomThrowable() { doTest(); } public void testParentheses() { doTest(); } + public void testNoCondition() { assertQuickfixNotAvailable(); } }