added WhileStmtsMustUseBracesRule

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@455 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2002-07-20 03:23:30 +00:00
parent 774d126911
commit 57e232a615
8 changed files with 97 additions and 40 deletions

View File

@ -46,15 +46,12 @@
</target>
<target name="test" depends="compile">
<junit printsummary="yes" haltonfailure="yes">
<junit printsummary="no" haltonfailure="yes">
<classpath>
<pathelement location="${build}" />
<pathelement location="${test-data}" />
<pathelement location="${basedir}" />
</classpath>
<formatter type="plain" />
<batchtest fork="no" todir="${build}">
<fileset dir="${regress}">
<include name="test/**/*Test.java" />

View File

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

View File

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

View File

@ -20,6 +20,24 @@ public class Something {
</example>
</rule>
<rule name="WhileLoopsMustUseBracesRule"
message="Avoid using 'while' statements without curly braces"
class="net.sourceforge.pmd.rules.WhileLoopsMustUseBracesRule">
<description>
Avoid using 'while' statements without using curly braces
</description>
<example>
<![CDATA[
public void doSomething() {
while (true)
x++;
}
]]>
</example>
</rule>
</ruleset>

View File

@ -10,39 +10,10 @@ import net.sourceforge.pmd.ast.SimpleNode;
import net.sourceforge.pmd.ast.ASTBlock;
import net.sourceforge.pmd.*;
public class IfElseStmtsMustUseBracesRule extends AbstractRule implements Rule {
public class IfElseStmtsMustUseBracesRule extends AbstractRule {
private int lineNumberOfLastViolation;
/**
* If..else stmt structure seems to be like this:
* IfStmt
* Expression
* Stmt
* Block
* Stmt
* Block
* if (foo == null) {
* return bar;
* } else {
* return buz;
* }
*
* Sometimes people get lazy and leave out the Blocks, like this:
* if (foo== null)
* return bar;
* else
* return buz;
*
* The following usage is OK though:
* IfStmt
* Expression
* Stmt
* i.e.:
* if (foo == null)
* return bar;
*
*/
public Object visit(ASTIfStatement node, Object data) {
RuleContext ctx = (RuleContext)data;
// filter out if stmts without an else
@ -54,11 +25,9 @@ public class IfElseStmtsMustUseBracesRule extends AbstractRule implements Rule {
SimpleNode firstStmt = (SimpleNode)node.jjtGetChild(1);
SimpleNode secondStmt = (SimpleNode)node.jjtGetChild(2);
if (!hasBlockAsFirstChild(firstStmt) && !hasBlockAsFirstChild(secondStmt)) {
if (node.getBeginLine() != this.lineNumberOfLastViolation) {
ctx.getReport().addRuleViolation(createRuleViolation(ctx, node.getBeginLine()));
lineNumberOfLastViolation = node.getBeginLine();
}
if (!hasBlockAsFirstChild(firstStmt) && !hasBlockAsFirstChild(secondStmt) && (node.getBeginLine() != this.lineNumberOfLastViolation)) {
ctx.getReport().addRuleViolation(createRuleViolation(ctx, node.getBeginLine()));
lineNumberOfLastViolation = node.getBeginLine();
}
return super.visit(node,data);
}

View File

@ -0,0 +1,33 @@
/*
* User: tom
* Date: Jul 19, 2002
* Time: 11:13:17 PM
*/
package net.sourceforge.pmd.rules;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.ast.ASTIfStatement;
import net.sourceforge.pmd.ast.SimpleNode;
import net.sourceforge.pmd.ast.ASTBlock;
import net.sourceforge.pmd.ast.ASTWhileStatement;
public class WhileLoopsMustUseBracesRule extends AbstractRule {
private int lineNumberOfLastViolation;
public Object visit(ASTWhileStatement node, Object data) {
RuleContext ctx = (RuleContext)data;
SimpleNode firstStmt = (SimpleNode)node.jjtGetChild(1);
if (!hasBlockAsFirstChild(firstStmt) && (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,7 @@
public class WhileLoopsNeedBraces1 {
public void foo() {
int x =0;
while (true)
x++;
}
}

View File

@ -0,0 +1,8 @@
public class WhileLoopsNeedBraces2 {
public void foo() {
int x =0;
while (true) {
x++;
}
}
}