forked from phoedos/pmd
Merge branch 'issue-232' into pmd/5.4.x
This commit is contained in:
@@ -805,24 +805,24 @@ public class Count {
|
||||
externalInfoUrl="${pmd.website.baseurl}/rules/java/basic.html#SimplifiedTernary">
|
||||
<description>
|
||||
<![CDATA[
|
||||
Look for ternary operators with the form condition ? literalBoolean : foo
|
||||
or condition ? foo : literalBoolean.
|
||||
Look for ternary operators with the form `condition ? literalBoolean : foo`
|
||||
or `condition ? foo : literalBoolean`.
|
||||
|
||||
These expressions can be simplified respectively to
|
||||
condition || foo when the literalBoolean is true
|
||||
!condition && foo when the literalBoolean is false
|
||||
`condition || foo` when the literalBoolean is true
|
||||
`!condition && foo` when the literalBoolean is false
|
||||
or
|
||||
!condition || foo when the literalBoolean is true
|
||||
condition && foo when the literalBoolean is false
|
||||
`!condition || foo` when the literalBoolean is true
|
||||
`condition && foo` when the literalBoolean is false
|
||||
]]>
|
||||
</description>
|
||||
<priority>3</priority>
|
||||
<properties>
|
||||
<property name="xpath">
|
||||
<value><![CDATA[
|
||||
//ConditionalExpression[@Ternary='true'][not(PrimaryExpression/*/Literal/BooleanLiteral) and (Expression/PrimaryExpression/*/Literal/BooleanLiteral)]
|
||||
//ConditionalExpression[@Ternary='true'][not(PrimaryExpression/*/Literal) and (Expression/PrimaryExpression/*/Literal/BooleanLiteral)]
|
||||
|
|
||||
//ConditionalExpression[@Ternary='true'][not(Expression/PrimaryExpression/*/Literal/BooleanLiteral) and (PrimaryExpression/*/Literal/BooleanLiteral)]
|
||||
//ConditionalExpression[@Ternary='true'][not(Expression/PrimaryExpression/*/Literal) and (PrimaryExpression/*/Literal/BooleanLiteral)]
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
|
@@ -94,6 +94,18 @@ public class SimplifiedTernary {
|
||||
private SomeValue doStuff(SomeValue someValue) {
|
||||
return THE_VALUE.equals(rangeValue.to) ? new SomeValue(someValue.from, null, true) : someValue;
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>#232 False positive with ternary operator using nullables</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class SimplifiedTernary {
|
||||
public Boolean getHasCustomName() {
|
||||
return mHasCustomName ? true : null;
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
@@ -22,6 +22,8 @@ This is a bug fixing release, no major changes were introduced.
|
||||
|
||||
* general
|
||||
* [#234](https://github.com/pmd/pmd/issues/234): \[core] Zip file stream closes spuriously when loading rulesets
|
||||
* java-basic
|
||||
* [#232](https://github.com/pmd/pmd/issues/232): \[java] SimplifiedTernary: Incorrect ternary operation can be simplified.
|
||||
* java-design
|
||||
* [#216](https://github.com/pmd/pmd/issues/216): \[java] \[doc] NonThreadSafeSingleton: Be more explicit as to why double checked locking is not recommended
|
||||
* [#219](https://github.com/pmd/pmd/issues/219): \[java] UnnecessaryLocalBeforeReturn: ClassCastException in switch case with local variable returned
|
||||
|
Reference in New Issue
Block a user