added new IfStmtsMustUseBracesRule
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@852 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
???? 2002 - 0.91:
|
||||
Added new rule: UnusedImportsRule, EmptySwitchStmtRule, SwitchStmtsShouldHaveDefaultRule
|
||||
Added new rule: UnusedImportsRule, EmptySwitchStmtRule, SwitchStmtsShouldHaveDefaultRule, IfStmtsMustUseBracesRule
|
||||
Fixed bug 597813 - Rule properties are now parsed correctly
|
||||
Fixed bug 597905 - UseSingletonRule now resets it's state correctly
|
||||
Moved several rules into a new ruleset - braces.
|
||||
|
@ -0,0 +1,23 @@
|
||||
/*
|
||||
* User: tom
|
||||
* Date: Sep 5, 2002
|
||||
* Time: 2:37:46 PM
|
||||
*/
|
||||
package test.net.sourceforge.pmd.rules;
|
||||
|
||||
import net.sourceforge.pmd.rules.IfStmtsMustUseBracesRule;
|
||||
|
||||
public class IfStmtsMustUseBracesRuleTest extends RuleTst {
|
||||
|
||||
public IfStmtsMustUseBracesRuleTest(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public void test1() throws Throwable {
|
||||
runTest("IfStmtsMustUseBraces1.java", 1, new IfStmtsMustUseBracesRule());
|
||||
}
|
||||
|
||||
public void test2() throws Throwable {
|
||||
runTest("IfStmtsMustUseBraces2.java", 0, new IfStmtsMustUseBracesRule());
|
||||
}
|
||||
}
|
@ -60,6 +60,25 @@ public class Foo {
|
||||
}
|
||||
}
|
||||
}
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
<rule name="IfStmtsMustUseBraces"
|
||||
message="Avoid using if statements without curly braces"
|
||||
class="net.sourceforge.pmd.rules.IfStmtsMustUseBracesRule">
|
||||
<description>
|
||||
Avoid using if statements without using curly braces
|
||||
</description>
|
||||
|
||||
<example>
|
||||
<![CDATA[
|
||||
public class Foo {
|
||||
public void bar() {
|
||||
int x = 0;
|
||||
if (foo) x++;
|
||||
}
|
||||
}
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
|
@ -10,6 +10,7 @@ import net.sourceforge.pmd.ast.SimpleNode;
|
||||
import net.sourceforge.pmd.ast.ASTBlock;
|
||||
|
||||
public abstract class BracesRule extends AbstractRule {
|
||||
|
||||
protected boolean hasBlockAsFirstChild(SimpleNode node) {
|
||||
return (node.jjtGetNumChildren() != 0 && (node.jjtGetChild(0) instanceof ASTBlock));
|
||||
}
|
||||
|
@ -8,14 +8,19 @@ package net.sourceforge.pmd.rules;
|
||||
import net.sourceforge.pmd.ast.ASTIfStatement;
|
||||
import net.sourceforge.pmd.ast.SimpleNode;
|
||||
import net.sourceforge.pmd.ast.ASTBlock;
|
||||
import net.sourceforge.pmd.ast.ASTCompilationUnit;
|
||||
import net.sourceforge.pmd.*;
|
||||
|
||||
public class IfElseStmtsMustUseBracesRule extends BracesRule {
|
||||
|
||||
private int lineNumberOfLastViolation;
|
||||
|
||||
public Object visit(ASTCompilationUnit node, Object data) {
|
||||
lineNumberOfLastViolation = -1;
|
||||
return super.visit(node,data);
|
||||
}
|
||||
|
||||
public Object visit(ASTIfStatement node, Object data) {
|
||||
RuleContext ctx = (RuleContext)data;
|
||||
// filter out if stmts without an else
|
||||
if (node.jjtGetNumChildren() < 3) {
|
||||
return super.visit(node, data);
|
||||
@ -26,6 +31,7 @@ public class IfElseStmtsMustUseBracesRule extends BracesRule {
|
||||
SimpleNode secondStmt = (SimpleNode)node.jjtGetChild(2);
|
||||
|
||||
if (!hasBlockAsFirstChild(firstStmt) && !hasBlockAsFirstChild(secondStmt) && (node.getBeginLine() != this.lineNumberOfLastViolation)) {
|
||||
RuleContext ctx = (RuleContext)data;
|
||||
ctx.getReport().addRuleViolation(createRuleViolation(ctx, node.getBeginLine()));
|
||||
lineNumberOfLastViolation = node.getBeginLine();
|
||||
}
|
||||
|
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* User: tom
|
||||
* Date: Sep 5, 2002
|
||||
* Time: 2:28:16 PM
|
||||
*/
|
||||
package net.sourceforge.pmd.rules;
|
||||
|
||||
import net.sourceforge.pmd.ast.ASTIfStatement;
|
||||
import net.sourceforge.pmd.ast.SimpleNode;
|
||||
import net.sourceforge.pmd.ast.ASTCompilationUnit;
|
||||
import net.sourceforge.pmd.RuleContext;
|
||||
|
||||
public class IfStmtsMustUseBracesRule extends BracesRule {
|
||||
|
||||
private int lineNumberOfLastViolation;
|
||||
|
||||
public Object visit(ASTCompilationUnit node, Object data) {
|
||||
lineNumberOfLastViolation = -1;
|
||||
return super.visit(node,data);
|
||||
}
|
||||
|
||||
public Object visit(ASTIfStatement node, Object data) {
|
||||
// if..else stmts are covered by other rules
|
||||
if (node.jjtGetNumChildren() >= 3) {
|
||||
return super.visit(node, data);
|
||||
}
|
||||
|
||||
// the first child is a Expression, so skip that and get the first stmt
|
||||
SimpleNode child = (SimpleNode)node.jjtGetChild(1);
|
||||
|
||||
if (!hasBlockAsFirstChild(child) && (node.getBeginLine() != lineNumberOfLastViolation)) {
|
||||
RuleContext ctx = (RuleContext)data;
|
||||
ctx.getReport().addRuleViolation(createRuleViolation(ctx, node.getBeginLine()));
|
||||
lineNumberOfLastViolation = node.getBeginLine();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
6
pmd/test-data/IfStmtsMustUseBraces1.java
Normal file
6
pmd/test-data/IfStmtsMustUseBraces1.java
Normal file
@ -0,0 +1,6 @@
|
||||
public class IfStmtsMustUseBraces1 {
|
||||
public void foo() {
|
||||
int x = 0;
|
||||
if (true) x=2;
|
||||
}
|
||||
}
|
7
pmd/test-data/IfStmtsMustUseBraces2.java
Normal file
7
pmd/test-data/IfStmtsMustUseBraces2.java
Normal file
@ -0,0 +1,7 @@
|
||||
public class IfStmtsMustUseBraces2 {
|
||||
public void foo() {
|
||||
if (true) {
|
||||
int x=2;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user