From 5718198740e804feae5ad3fa54b20810767cef31 Mon Sep 17 00:00:00 2001 From: Tom Copeland Date: Thu, 5 Sep 2002 18:44:12 +0000 Subject: [PATCH] added new IfStmtsMustUseBracesRule git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@852 51baf565-9d33-0410-a72c-fc3788e3496d --- pmd/etc/changelog.txt | 2 +- .../rules/IfStmtsMustUseBracesRuleTest.java | 23 +++++++++++ pmd/rulesets/newrules.xml | 19 +++++++++ .../net/sourceforge/pmd/rules/BracesRule.java | 1 + .../rules/IfElseStmtsMustUseBracesRule.java | 8 +++- .../pmd/rules/IfStmtsMustUseBracesRule.java | 39 +++++++++++++++++++ pmd/test-data/IfStmtsMustUseBraces1.java | 6 +++ pmd/test-data/IfStmtsMustUseBraces2.java | 7 ++++ 8 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 pmd/regress/test/net/sourceforge/pmd/rules/IfStmtsMustUseBracesRuleTest.java create mode 100644 pmd/src/net/sourceforge/pmd/rules/IfStmtsMustUseBracesRule.java create mode 100644 pmd/test-data/IfStmtsMustUseBraces1.java create mode 100644 pmd/test-data/IfStmtsMustUseBraces2.java diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 2439597208..dded1deba5 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -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. diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/IfStmtsMustUseBracesRuleTest.java b/pmd/regress/test/net/sourceforge/pmd/rules/IfStmtsMustUseBracesRuleTest.java new file mode 100644 index 0000000000..74e1b9db5b --- /dev/null +++ b/pmd/regress/test/net/sourceforge/pmd/rules/IfStmtsMustUseBracesRuleTest.java @@ -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()); + } +} diff --git a/pmd/rulesets/newrules.xml b/pmd/rulesets/newrules.xml index 7087dd800b..35ad6bf1ce 100644 --- a/pmd/rulesets/newrules.xml +++ b/pmd/rulesets/newrules.xml @@ -60,6 +60,25 @@ public class Foo { } } } +]]> + + + + + + Avoid using if statements without using curly braces + + + + diff --git a/pmd/src/net/sourceforge/pmd/rules/BracesRule.java b/pmd/src/net/sourceforge/pmd/rules/BracesRule.java index 91ca16f776..67b7156aa6 100644 --- a/pmd/src/net/sourceforge/pmd/rules/BracesRule.java +++ b/pmd/src/net/sourceforge/pmd/rules/BracesRule.java @@ -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)); } diff --git a/pmd/src/net/sourceforge/pmd/rules/IfElseStmtsMustUseBracesRule.java b/pmd/src/net/sourceforge/pmd/rules/IfElseStmtsMustUseBracesRule.java index e51b99d5da..5014fa16b1 100644 --- a/pmd/src/net/sourceforge/pmd/rules/IfElseStmtsMustUseBracesRule.java +++ b/pmd/src/net/sourceforge/pmd/rules/IfElseStmtsMustUseBracesRule.java @@ -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(); } diff --git a/pmd/src/net/sourceforge/pmd/rules/IfStmtsMustUseBracesRule.java b/pmd/src/net/sourceforge/pmd/rules/IfStmtsMustUseBracesRule.java new file mode 100644 index 0000000000..78e19a6452 --- /dev/null +++ b/pmd/src/net/sourceforge/pmd/rules/IfStmtsMustUseBracesRule.java @@ -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; + } +} diff --git a/pmd/test-data/IfStmtsMustUseBraces1.java b/pmd/test-data/IfStmtsMustUseBraces1.java new file mode 100644 index 0000000000..74a80e93ac --- /dev/null +++ b/pmd/test-data/IfStmtsMustUseBraces1.java @@ -0,0 +1,6 @@ +public class IfStmtsMustUseBraces1 { + public void foo() { + int x = 0; + if (true) x=2; + } +} \ No newline at end of file diff --git a/pmd/test-data/IfStmtsMustUseBraces2.java b/pmd/test-data/IfStmtsMustUseBraces2.java new file mode 100644 index 0000000000..2da6cd0a24 --- /dev/null +++ b/pmd/test-data/IfStmtsMustUseBraces2.java @@ -0,0 +1,7 @@ +public class IfStmtsMustUseBraces2 { + public void foo() { + if (true) { + int x=2; + } + } +} \ No newline at end of file