diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/SingleStatementInBlockInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/SingleStatementInBlockInspection.java index 41802581d029..3943c0bd0ee8 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/SingleStatementInBlockInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/SingleStatementInBlockInspection.java @@ -21,6 +21,7 @@ import com.intellij.openapi.util.Pair; import com.intellij.psi.*; import com.intellij.psi.util.FileTypeUtils; import com.intellij.psi.util.PsiUtil; +import com.intellij.psi.util.PsiUtilCore; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; @@ -97,7 +98,11 @@ public class SingleStatementInBlockInspection extends BaseInspection { @Override protected boolean isApplicable(PsiStatement body) { if (body instanceof PsiBlockStatement) { - final PsiStatement[] statements = ((PsiBlockStatement)body).getCodeBlock().getStatements(); + final PsiCodeBlock codeBlock = ((PsiBlockStatement)body).getCodeBlock(); + if (PsiUtilCore.hasErrorElementChild(codeBlock)) { + return false; + } + final PsiStatement[] statements = codeBlock.getStatements(); if (statements.length == 1 && !(statements[0] instanceof PsiDeclarationStatement) && !isDanglingElseProblem(statements[0], body)) { final PsiFile file = body.getContainingFile(); //this inspection doesn't work in JSP files, as it can't tell about tags diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/single_statement_block/ForMalformed.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/single_statement_block/ForMalformed.java new file mode 100644 index 000000000000..1bbf4832c518 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/single_statement_block/ForMalformed.java @@ -0,0 +1,28 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +class X { + public int toIntegerValue() { + int result = 0; + for (CssPrefix prefix : myEnabledPrefixes) { + float (getValue) { + result |= prefix.myIntMask; + } + } + return result; + } + + void anotherMethod() {} +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/braces/SingleStatementInBlockFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/braces/SingleStatementInBlockFixTest.java index d9ebda08bae7..de82aa9ebfa5 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/braces/SingleStatementInBlockFixTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/braces/SingleStatementInBlockFixTest.java @@ -33,6 +33,7 @@ public class SingleStatementInBlockFixTest extends IGQuickFixesTestCase { public void testWhile() { doTest("while"); } public void testForEach() { doTest("for"); } public void testForIndex() { doTest("for"); } + public void testForMalformed() { assertQuickfixNotAvailable(getMessage("for"));} public void testDoWhile() { doTest("do"); } public void testIfWithLoop() { doTest("if"); } public void testElseWithLoop() { doTest("else"); }