Merge pull request #3938 from VoidxHoshi:master

[java] Modify SimplifiedTernary to meet the missing case #3603 #3938
This commit is contained in:
Andreas Dangel
2022-04-28 15:03:28 +02:00
5 changed files with 88 additions and 73 deletions

View File

@ -6621,6 +6621,15 @@
"contributions": [ "contributions": [
"code" "code"
] ]
},
{
"login": "VoidxHoshi",
"name": "LaLucid",
"avatar_url": "https://avatars.githubusercontent.com/u/55886143?v=4",
"profile": "https://github.com/VoidxHoshi",
"contributions": [
"code"
]
} }
], ],
"contributorsPerLine": 7, "contributorsPerLine": 7,

File diff suppressed because it is too large Load Diff

View File

@ -32,6 +32,7 @@ This is a {{ site.pmd.release_type }} release.
* [#2505](https://github.com/pmd/pmd/issues/2505): \[doc] Improve side bar to show release date * [#2505](https://github.com/pmd/pmd/issues/2505): \[doc] Improve side bar to show release date
* java * java
* [#3068](https://github.com/pmd/pmd/issues/3068): \[java] Some tests should not depend on real rules * [#3068](https://github.com/pmd/pmd/issues/3068): \[java] Some tests should not depend on real rules
* [#3603](https://github.com/pmd/pmd/issues/3603): \[java] SimplifiedTernary: no violation for 'condition ? true : false' case
* [#3889](https://github.com/pmd/pmd/pull/3889): \[java] Catch LinkageError in UselessOverridingMethodRule * [#3889](https://github.com/pmd/pmd/pull/3889): \[java] Catch LinkageError in UselessOverridingMethodRule
* [#3910](https://github.com/pmd/pmd/pull/3910): \[java] UnusedPrivateField - Allow the ignored fieldnames to be configurable * [#3910](https://github.com/pmd/pmd/pull/3910): \[java] UnusedPrivateField - Allow the ignored fieldnames to be configurable
* java-bestpractices * java-bestpractices
@ -60,6 +61,7 @@ This is a {{ site.pmd.release_type }} release.
* [#3910](https://github.com/pmd/pmd/pull/3910): \[java] UnusedPrivateField - Allow the ignored fieldnames to be configurable - [@laoseth](https://github.com/laoseth) * [#3910](https://github.com/pmd/pmd/pull/3910): \[java] UnusedPrivateField - Allow the ignored fieldnames to be configurable - [@laoseth](https://github.com/laoseth)
* [#3928](https://github.com/pmd/pmd/pull/3928): \[plsql] Fix plsql parsing error in parenthesis groups - [@LiGaOg](https://github.com/LiGaOg) * [#3928](https://github.com/pmd/pmd/pull/3928): \[plsql] Fix plsql parsing error in parenthesis groups - [@LiGaOg](https://github.com/LiGaOg)
* [#3935](https://github.com/pmd/pmd/pull/3935): \[plsql] Fix parser exception in EXECUTE IMMEDIATE BULK COLLECT #3687 - [@Scrsloota](https://github.com/Scrsloota) * [#3935](https://github.com/pmd/pmd/pull/3935): \[plsql] Fix parser exception in EXECUTE IMMEDIATE BULK COLLECT #3687 - [@Scrsloota](https://github.com/Scrsloota)
* [#3938](https://github.com/pmd/pmd/pull/3938): \[java] Modify SimplifiedTernary to meet the missing case #3603 - [@VoidxHoshi](https://github.com/VoidxHoshi)
{% endtocmaker %} {% endtocmaker %}

View File

@ -1291,12 +1291,11 @@ public void foo() throws Exception {
Look for ternary operators with the form `condition ? literalBoolean : foo` Look for ternary operators with the form `condition ? literalBoolean : foo`
or `condition ? foo : literalBoolean`. or `condition ? foo : literalBoolean`.
These expressions can be simplified respectively to These expressions can be simplified as follows:
`condition || foo` when the literalBoolean is true * `condition ? true : expr` simplifies to `condition || expr`
`!condition && foo` when the literalBoolean is false * `condition ? false : expr` simplifies to `!condition && expr`
or * `condition ? expr : true` simplifies to `!condition || expr`
`!condition || foo` when the literalBoolean is true * `condition ? expr : false` simplifies to `condition && expr`
`condition && foo` when the literalBoolean is false
]]> ]]>
</description> </description>
<priority>3</priority> <priority>3</priority>
@ -1305,9 +1304,9 @@ or
<property name="xpath"> <property name="xpath">
<value> <value>
<![CDATA[ <![CDATA[
//ConditionalExpression[not(PrimaryExpression/*/Literal) and (Expression/PrimaryExpression/*/Literal/BooleanLiteral)] //ConditionalExpression
| [(Expression|.)/PrimaryExpression/*/Literal/BooleanLiteral]
//ConditionalExpression[not(Expression/PrimaryExpression/*/Literal) and (PrimaryExpression/*/Literal/BooleanLiteral)] [not((Expression|.)/PrimaryExpression/*/Literal/NullLiteral)]
]]> ]]>
</value> </value>
</property> </property>
@ -1330,6 +1329,10 @@ public class Foo {
public void test4() { public void test4() {
final boolean otherValue = condition ? something() : false; // can be as simple as condition && something(); final boolean otherValue = condition ? something() : false; // can be as simple as condition && something();
} }
public boolean test5() {
return condition ? true : false; // can be as simple as return condition;
}
} }
]]> ]]>
</example> </example>

View File

@ -53,12 +53,12 @@ public class Foo {
</test-code> </test-code>
<test-code> <test-code>
<description>condition ? true : false</description> <description>#3603 condition ? true : false</description>
<expected-problems>0</expected-problems> <expected-problems>1</expected-problems>
<code><![CDATA[ <code><![CDATA[
public class Foo { public class Foo {
public boolean test() { public boolean test() {
return condition ? true : false; // Existing rule return condition ? true : false;
} }
} }
]]></code> ]]></code>