Skip to content

Commit df2d20e

Browse files
committed
[java] Fix #4763 - wrong message for SimplifyBooleanReturns (#5373)
Merge pull request #5373 from oowekyala:issue4763-simplifyBooleanReturns-message
2 parents 3e34b11 + ea32fbb commit df2d20e

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

docs/pages/release_notes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ This is a {{ site.pmd.release_type }} release.
4444
* [#5214](https://github.com/pmd/pmd/issues/5214): \[java] Wrong message for LambdaCanBeMethodReference with method of enclosing class
4545
* [#5263](https://github.com/pmd/pmd/issues/5263): \[java] UnnecessaryFullyQualifiedName: false-positive in an enum that uses its own static variables
4646
* [#5315](https://github.com/pmd/pmd/issues/5315): \[java] UnnecessaryImport false positive for on-demand imports
47+
* java-design
48+
* [#4763](https://github.com/pmd/pmd/issues/4763): \[java] SimplifyBooleanReturns - wrong suggested solution
4749
* java-performance
4850
* [#5287](https://github.com/pmd/pmd/issues/5287): \[java] TooFewBranchesForSwitch false-positive with switch using list of case constants
4951
* [#5314](https://github.com/pmd/pmd/issues/5314): \[java] InsufficientStringBufferDeclarationRule: Lack of handling for char type parameters
@@ -125,6 +127,7 @@ This is a {{ site.pmd.release_type }} release.
125127
* [#5370](https://github.com/pmd/pmd/pull/5370): \[java] Fix #5214 - LambdaCanBeMethodReference issue with method of enclosing class - [Clément Fournier](https://github.com/oowekyala) (@oowekyala)
126128
* [#5371](https://github.com/pmd/pmd/pull/5371): \[doc] Improve docs on adding Antlr languages - [Juan Martín Sotuyo Dodero](https://github.com/jsotuyod) (@jsotuyod)
127129
* [#5372](https://github.com/pmd/pmd/pull/5372): \[java] Fix #5315 - UnusedImport FP with import on demand - [Clément Fournier](https://github.com/oowekyala) (@oowekyala)
130+
* [#5373](https://github.com/pmd/pmd/pull/5373): \[java] Fix #4763 - wrong message for SimplifyBooleanReturns - [Clément Fournier](https://github.com/oowekyala) (@oowekyala)
128131

129132
### 📦 Dependency updates
130133
<!-- content will be automatically generated, see /do-release.sh -->

pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/SimplifyBooleanReturnsRule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ && doesNotNeedNewParensUnderInfix(branch, op)) {
139139
if (thenTrue) {
140140
return "return {condition} || {elseBranch};";
141141
} else if (thenFalse) {
142-
return "return !{condition} || {elseBranch};";
142+
return "return !{condition} && {elseBranch};";
143143
} else if (elseTrue) {
144-
return "return !{condition} && {thenBranch};";
144+
return "return !{condition} || {thenBranch};";
145145
} else {
146146
return "return {condition} && {thenBranch};";
147147
}

pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/SimplifyBooleanReturns.xml

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ public class SimplifyBooleanReturns {
185185
<expected-problems>2</expected-problems>
186186
<expected-linenumbers>3,8</expected-linenumbers>
187187
<expected-messages>
188-
<message>This if statement can be replaced by `return !{condition} &amp;&amp; {thenBranch};`</message>
189-
<message>This if statement can be replaced by `return !{condition} &amp;&amp; {thenBranch};`</message>
188+
<message>This if statement can be replaced by `return !{condition} || {thenBranch};`</message>
189+
<message>This if statement can be replaced by `return !{condition} || {thenBranch};`</message>
190190
</expected-messages>
191191
<code><![CDATA[
192192
public class Foo {
@@ -248,7 +248,7 @@ public class SimplifyBooleanReturns {
248248
<expected-problems>1</expected-problems>
249249
<expected-linenumbers>6</expected-linenumbers>
250250
<expected-messages>
251-
<message>This if statement can be replaced by `return !{condition} &amp;&amp; {thenBranch};`</message>
251+
<message>This if statement can be replaced by `return !{condition} || {thenBranch};`</message>
252252
</expected-messages>
253253
<code><![CDATA[
254254
public class Foo {
@@ -265,7 +265,7 @@ public class SimplifyBooleanReturns {
265265
}
266266
]]></code>
267267
</test-code>
268-
268+
269269
<test-code>
270270
<description>[java] SimplifyBooleanReturns should consider literal expression #3852</description>
271271
<expected-problems>1</expected-problems>
@@ -282,4 +282,31 @@ class Tester {
282282
}
283283
]]></code>
284284
</test-code>
285+
286+
<test-code>
287+
<description> [java] SimplifyBooleanReturns - wrong suggested solution #4763 </description>
288+
<expected-problems>2</expected-problems>
289+
<expected-messages>
290+
<message>This if statement can be replaced by `return !{condition} &amp;&amp; {elseBranch};`</message>
291+
<message>This if statement can be replaced by `return !{condition} &amp;&amp; {elseBranch};`</message>
292+
</expected-messages>
293+
<code><![CDATA[
294+
public class Foo {
295+
296+
public boolean foo(Object a) {
297+
if (a == null) {
298+
return false;
299+
}
300+
return "FOO".equals(a.toString());
301+
}
302+
303+
public boolean bar(String stringA, String stringB) {
304+
if (StringUtils.isAnyBlank(stringA, stringB)) {
305+
return false;
306+
}
307+
return Objects.equals(stringA, stringB);
308+
}
309+
}
310+
]]></code>
311+
</test-code>
285312
</test-data>

0 commit comments

Comments
 (0)