AvoidUsingOctalValues: too many false + for non octal values -> rewritten in Java with an exact regexp

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4806 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Xavier Le Vourch
2006-11-17 01:46:21 +00:00
parent 110797a22e
commit 35a1eeccf2
3 changed files with 29 additions and 15 deletions

View File

@ -20,6 +20,7 @@ public class AvoidUsingOctalValuesTest extends SimpleAggregatorTst {
new TestDescriptor(TEST2, "OK, hex value", 0, rule), new TestDescriptor(TEST2, "OK, hex value", 0, rule),
new TestDescriptor(TEST3, "OK, long value", 0, rule), new TestDescriptor(TEST3, "OK, long value", 0, rule),
new TestDescriptor(TEST4, "OK, double value", 0, rule), new TestDescriptor(TEST4, "OK, double value", 0, rule),
new TestDescriptor(TEST5, "OK, double value", 0, rule),
}); });
} }
@ -43,4 +44,9 @@ public class AvoidUsingOctalValuesTest extends SimpleAggregatorTst {
" double d = 0.1;" + PMD.EOL + " double d = 0.1;" + PMD.EOL +
"}"; "}";
private static final String TEST5 =
"public class Foo {" + PMD.EOL +
" float f = 0f;" + PMD.EOL +
"}";
} }

View File

@ -1087,26 +1087,12 @@ public class Test {
<rule name="AvoidUsingOctalValues" <rule name="AvoidUsingOctalValues"
message="Do not start a literal by 0 unless it's an octal value" message="Do not start a literal by 0 unless it's an octal value"
class="net.sourceforge.pmd.rules.XPathRule" class="net.sourceforge.pmd.rules.basic.AvoidUsingOctalValues"
externalInfoUrl="http://pmd.sourceforge.net/rules/basic.html#AvoidUsingOctalValues"> externalInfoUrl="http://pmd.sourceforge.net/rules/basic.html#AvoidUsingOctalValues">
<description> <description>
Integer literals should not start with zero. Integer literals should not start with zero.
Zero means that the rest of literal will be interpreted as an octal value. Zero means that the rest of literal will be interpreted as an octal value.
</description> </description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//Literal
[(starts-with(@Image, 0))]
[string-length(@Image)>1]
[not(contains(@Image, '.'))]
[not(substring(@Image, 2, 1) = 'x')]
[not(substring(@Image, 2, 1) = 'L')]
]]>
</value>
</property>
</properties>
<priority>3</priority> <priority>3</priority>
<example> <example>
<![CDATA[ <![CDATA[

View File

@ -0,0 +1,22 @@
package net.sourceforge.pmd.rules.basic;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.ast.ASTLiteral;
public class AvoidUsingOctalValues extends AbstractRule {
public static final Pattern OCTAL_PATTERN = Pattern.compile("0[0-7]+");
public Object visit(ASTLiteral node, Object data) {
Matcher m = OCTAL_PATTERN.matcher(node.getImage());
if (m.matches()) {
addViolation(data, node);
}
return data;
}
}