diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/ForLoopsMustUseBracesRuleTest.java b/pmd/regress/test/net/sourceforge/pmd/rules/ForLoopsMustUseBracesRuleTest.java index d5908b2f29..1e326945a1 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/ForLoopsMustUseBracesRuleTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/rules/ForLoopsMustUseBracesRuleTest.java @@ -23,4 +23,19 @@ public class ForLoopsMustUseBracesRuleTest extends RuleTst { Report report = process("ForLoopsNeedBraces2.java", new ForLoopsMustUseBracesRule()); assertTrue(report.isEmpty()); } + public void test3() throws Throwable { + Report report = process("ForLoopsNeedBraces3.java", new ForLoopsMustUseBracesRule()); + assertEquals(1, report.size()); + } + + public void test4() throws Throwable { + Report report = process("ForLoopsNeedBraces4.java", new ForLoopsMustUseBracesRule()); + assertEquals(1, report.size()); + } + + public void test5() throws Throwable { + Report report = process("ForLoopsNeedBraces5.java", new ForLoopsMustUseBracesRule()); + assertEquals(1, report.size()); + } + } diff --git a/pmd/src/net/sourceforge/pmd/rules/ForLoopsMustUseBracesRule.java b/pmd/src/net/sourceforge/pmd/rules/ForLoopsMustUseBracesRule.java index b7382288e6..3c4cee8be9 100644 --- a/pmd/src/net/sourceforge/pmd/rules/ForLoopsMustUseBracesRule.java +++ b/pmd/src/net/sourceforge/pmd/rules/ForLoopsMustUseBracesRule.java @@ -16,7 +16,7 @@ public class ForLoopsMustUseBracesRule extends BracesRule { public Object visit(ASTForStatement node, Object data) { RuleContext ctx = (RuleContext)data; - SimpleNode loopBody = (SimpleNode)node.jjtGetChild(3); + SimpleNode loopBody = (SimpleNode)node.jjtGetChild(node.jjtGetNumChildren()-1); if (!hasBlockAsFirstChild(loopBody)) { ctx.getReport().addRuleViolation(createRuleViolation(ctx, node.getBeginLine())); diff --git a/pmd/test-data/ForLoopsNeedBraces3.java b/pmd/test-data/ForLoopsNeedBraces3.java new file mode 100644 index 0000000000..9960622229 --- /dev/null +++ b/pmd/test-data/ForLoopsNeedBraces3.java @@ -0,0 +1,7 @@ +public class ForLoopsNeedBraces3 { + public void foo() { + for (int i=0; i<42;) + foo(); + + } +} \ No newline at end of file diff --git a/pmd/test-data/ForLoopsNeedBraces4.java b/pmd/test-data/ForLoopsNeedBraces4.java new file mode 100644 index 0000000000..f5e7de3ec8 --- /dev/null +++ b/pmd/test-data/ForLoopsNeedBraces4.java @@ -0,0 +1,7 @@ +public class ForLoopsNeedBraces4 { + public void foo() { + for (int i=0;;) + foo(); + + } +} \ No newline at end of file diff --git a/pmd/test-data/ForLoopsNeedBraces5.java b/pmd/test-data/ForLoopsNeedBraces5.java new file mode 100644 index 0000000000..2560b65f26 --- /dev/null +++ b/pmd/test-data/ForLoopsNeedBraces5.java @@ -0,0 +1,7 @@ +public class ForLoopsNeedBraces5 { + public void foo() { + for (;;) + foo(); + + } +} \ No newline at end of file