forked from phoedos/pmd
added ForLoopsMustUseBracesRule
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@456 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -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:
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
@ -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>
|
||||
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
6
pmd/test-data/ForLoopsNeedBraces1.java
Normal file
6
pmd/test-data/ForLoopsNeedBraces1.java
Normal file
@ -0,0 +1,6 @@
|
||||
public class ForLoopsNeedBraces1 {
|
||||
public void foo() {
|
||||
for (int i=0; i<42;i++)
|
||||
foo();
|
||||
}
|
||||
}
|
7
pmd/test-data/ForLoopsNeedBraces2.java
Normal file
7
pmd/test-data/ForLoopsNeedBraces2.java
Normal file
@ -0,0 +1,7 @@
|
||||
public class ForLoopsNeedBraces2 {
|
||||
public void foo() {
|
||||
for (int i=0; i<42;i++) {
|
||||
foo();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user