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
commit c9fa05d2c7
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
5 changed files with 88 additions and 73 deletions

View File

@ -6621,6 +6621,15 @@
"contributions": [
"code"
]
},
{
"login": "VoidxHoshi",
"name": "LaLucid",
"avatar_url": "https://avatars.githubusercontent.com/u/55886143?v=4",
"profile": "https://github.com/VoidxHoshi",
"contributions": [
"code"
]
}
],
"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
* java
* [#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
* [#3910](https://github.com/pmd/pmd/pull/3910): \[java] UnusedPrivateField - Allow the ignored fieldnames to be configurable
* 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)
* [#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)
* [#3938](https://github.com/pmd/pmd/pull/3938): \[java] Modify SimplifiedTernary to meet the missing case #3603 - [@VoidxHoshi](https://github.com/VoidxHoshi)
{% endtocmaker %}

View File

@ -1291,12 +1291,11 @@ public void foo() throws Exception {
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
or
`!condition || foo` when the literalBoolean is true
`condition && foo` when the literalBoolean is false
These expressions can be simplified as follows:
* `condition ? true : expr` simplifies to `condition || expr`
* `condition ? false : expr` simplifies to `!condition && expr`
* `condition ? expr : true` simplifies to `!condition || expr`
* `condition ? expr : false` simplifies to `condition && expr`
]]>
</description>
<priority>3</priority>
@ -1305,9 +1304,9 @@ or
<property name="xpath">
<value>
<![CDATA[
//ConditionalExpression[not(PrimaryExpression/*/Literal) and (Expression/PrimaryExpression/*/Literal/BooleanLiteral)]
|
//ConditionalExpression[not(Expression/PrimaryExpression/*/Literal) and (PrimaryExpression/*/Literal/BooleanLiteral)]
//ConditionalExpression
[(Expression|.)/PrimaryExpression/*/Literal/BooleanLiteral]
[not((Expression|.)/PrimaryExpression/*/Literal/NullLiteral)]
]]>
</value>
</property>
@ -1330,6 +1329,10 @@ public class Foo {
public void test4() {
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>

View File

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