added ForLoopsMustUseBracesRule

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@456 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2002-07-20 03:42:55 +00:00
parent 57e232a615
commit 328b4089d5
6 changed files with 88 additions and 1 deletions

View File

@ -1,5 +1,5 @@
???? 2002 - 0.7:
Added new rules: UnusedPrivateMethodRule, WhileLoopsMustUseBracesRule
Added new rules: UnusedPrivateMethodRule, WhileLoopsMustUseBracesRule, ForLoopsMustUseBracesRule
Fixed bug 583482 - EmptyCatchBlock and EmptyFinallyBlock no longer report an incorrect line number.
July 18 2002 - 0.6:

View File

@ -0,0 +1,26 @@
/*
* User: tom
* Date: Jul 19, 2002
* Time: 11:32:02 PM
*/
package test.net.sourceforge.pmd.rules;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.rules.WhileLoopsMustUseBracesRule;
import net.sourceforge.pmd.rules.ForLoopsMustUseBracesRule;
public class ForLoopsMustUseBracesRuleTest extends RuleTst {
public ForLoopsMustUseBracesRuleTest(String name) {
super(name);
}
public void test1() throws Throwable {
Report report = process("ForLoopsNeedBraces1.java", new ForLoopsMustUseBracesRule());
assertEquals(1, report.size());
}
public void test2() throws Throwable {
Report report = process("ForLoopsNeedBraces2.java", new ForLoopsMustUseBracesRule());
assertTrue(report.isEmpty());
}
}

View File

@ -37,6 +37,22 @@ public class Something {
</example>
</rule>
<rule name="ForLoopsMustUseBracesRule"
message="Avoid using 'for' statements without curly braces"
class="net.sourceforge.pmd.rules.ForLoopsMustUseBracesRule">
<description>
Avoid using 'for' statements without using curly braces
</description>
<example>
<![CDATA[
public void foo() {
for (int i=0; i<42;i++)
foo();
}
]]>
</example>
</rule>
</ruleset>

View File

@ -0,0 +1,32 @@
/*
* User: tom
* Date: Jul 19, 2002
* Time: 11:30:15 PM
*/
package net.sourceforge.pmd.rules;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.ast.ASTWhileStatement;
import net.sourceforge.pmd.ast.SimpleNode;
import net.sourceforge.pmd.ast.ASTBlock;
import net.sourceforge.pmd.ast.ASTForStatement;
public class ForLoopsMustUseBracesRule extends AbstractRule {
private int lineNumberOfLastViolation;
public Object visit(ASTForStatement node, Object data) {
RuleContext ctx = (RuleContext)data;
SimpleNode loopBody = (SimpleNode)node.jjtGetChild(3);
if (!hasBlockAsFirstChild(loopBody) && (node.getBeginLine() != this.lineNumberOfLastViolation)) {
ctx.getReport().addRuleViolation(createRuleViolation(ctx, node.getBeginLine()));
lineNumberOfLastViolation = node.getBeginLine();
}
return super.visit(node,data);
}
private boolean hasBlockAsFirstChild(SimpleNode node) {
return (node.jjtGetNumChildren() != 0 && (node.jjtGetChild(0) instanceof ASTBlock));
}
}

View File

@ -0,0 +1,6 @@
public class ForLoopsNeedBraces1 {
public void foo() {
for (int i=0; i<42;i++)
foo();
}
}

View File

@ -0,0 +1,7 @@
public class ForLoopsNeedBraces2 {
public void foo() {
for (int i=0; i<42;i++) {
foo();
}
}
}