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 51b500abd81f..469037a96be2 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/SingleStatementInBlockInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/SingleStatementInBlockInspection.java @@ -172,12 +172,12 @@ public class SingleStatementInBlockInspection extends BaseInspection { if (startElement instanceof PsiLoopStatement) { body = ((PsiLoopStatement)startElement).getBody(); } - else if (startParent instanceof PsiLoopStatement) { - body = ((PsiLoopStatement)startParent).getBody(); - } else if (startElement instanceof PsiIfStatement) { body = ((PsiIfStatement)startElement).getThenBranch(); } + else if (startParent instanceof PsiLoopStatement) { + body = ((PsiLoopStatement)startParent).getBody(); + } else if (startElement instanceof PsiKeyword) { assert startParent instanceof PsiIfStatement; PsiIfStatement ifStatement = (PsiIfStatement)startParent; diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/single_statement_block/IfWithLoop4.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/single_statement_block/IfWithLoop4.after.java new file mode 100644 index 000000000000..cffddafcf2b0 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/single_statement_block/IfWithLoop4.after.java @@ -0,0 +1,8 @@ +class X { + void f(int[] a) { + for(int aa : a) + if(aa > 0) System.out.println( + aa + ); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/single_statement_block/IfWithLoop4.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/single_statement_block/IfWithLoop4.java new file mode 100644 index 000000000000..6989560694fd --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/single_statement_block/IfWithLoop4.java @@ -0,0 +1,10 @@ +class X { + void f(int[] a) { + for(int aa : a) + if(aa > 0) { + System.out.println( + aa + ); + } + } +} \ 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 5b7c2be6386e..1ebef42a85d4 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/braces/SingleStatementInBlockFixTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/braces/SingleStatementInBlockFixTest.java @@ -38,6 +38,7 @@ public class SingleStatementInBlockFixTest extends IGQuickFixesTestCase { public void testIfWithLoop() { doTest("if"); } public void testIfWithLoop2() { doTest("else"); } public void testIfWithLoop3() { assertQuickfixNotAvailable(getMessage("else")); } + public void testIfWithLoop4() { doTest("if"); } public void testElseWithLoop() { doTest("else"); } public void testTwoComments() { doTest("if"); } public void testIncompleteIf() { assertQuickfixNotAvailable(getMessage("for")); }