From 6e7abaa671105d65802e3b77b21e6cda2512aad8 Mon Sep 17 00:00:00 2001 From: Tom Copeland Date: Mon, 29 Jul 2002 15:21:41 +0000 Subject: [PATCH] fixed bug # 588083 git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@544 51baf565-9d33-0410-a72c-fc3788e3496d --- .../pmd/rules/ForLoopsMustUseBracesRuleTest.java | 15 +++++++++++++++ .../pmd/rules/ForLoopsMustUseBracesRule.java | 2 +- pmd/test-data/ForLoopsNeedBraces3.java | 7 +++++++ pmd/test-data/ForLoopsNeedBraces4.java | 7 +++++++ pmd/test-data/ForLoopsNeedBraces5.java | 7 +++++++ 5 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 pmd/test-data/ForLoopsNeedBraces3.java create mode 100644 pmd/test-data/ForLoopsNeedBraces4.java create mode 100644 pmd/test-data/ForLoopsNeedBraces5.java 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