[java] AvoidLiteralsInIfCondition: report Literal rather than IfCondition

If the IfCondition spans multiple lines, it easier to spot the
literal, if the violation is reported on the correct line where
the literal is located.
This commit is contained in:
Andreas Dangel committed 2019-12-16 11:17:21 +01:00
1 parent ed253183aa
commit c0d0fd3fbe
2 files changed
+4 -6

No files matched your search

@@ -470,18 +470,16 @@ More exceptions can be defined with the property "ignoreMagicNumbers".
<property name="xpath">
<value>
<![CDATA[
//IfStatement[Expression//*[local-name() != 'UnaryExpression' or @Operator != '-']/PrimaryExpression/PrimaryPrefix/Literal
//IfStatement/Expression//*[local-name() != 'UnaryExpression' or @Operator != '-']/PrimaryExpression/PrimaryPrefix/Literal
[not(NullLiteral)]
[not(BooleanLiteral)]
[empty(index-of(tokenize($ignoreMagicNumbers, '\s*,\s*'), @Image))]
]
|
(: consider negative literals :)
//IfStatement[Expression//UnaryExpression[@Operator = '-']/PrimaryExpression/PrimaryPrefix/Literal
//IfStatement/Expression//UnaryExpression[@Operator = '-']/PrimaryExpression/PrimaryPrefix/Literal
[not(NullLiteral)]
[not(BooleanLiteral)]
[empty(index-of(tokenize($ignoreMagicNumbers, '\s*,\s*'), concat('-', @Image)))]
]
]]>
</value>
</property>
@@ -93,8 +93,8 @@ public class Foo {
<test-code>
<description>#2140 [java] AvoidLiteralsInIfCondition: false negative for expressions</description>
<expected-problems>3</expected-problems>
<expected-linenumbers>3,4,5</expected-linenumbers>
<expected-problems>4</expected-problems>
<expected-linenumbers>3,3,4,5</expected-linenumbers>
<code><![CDATA[
public class Foo {
public void bar(int a) {