I have read check documentation: https://checkstyle.sourceforge.io/config_coding.html#UnnecessaryParentheses
I have downloaded the latest checkstyle from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words
/var/tmp $ javac UnnecessaryParenthesesExample.java
/var/tmp $ cat config.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="TreeWalker">
<module name="UnnecessaryParentheses"/>
</module>
</module>
/var/tmp $ cat UnnecessaryParenthesesExample.java
public class UnnecessaryParenthesesExample {
public static boolean foo(int x, int y) {
return (x == 0 && y > 0) || (x > 0 && y == 0);
}
public static void main(String... args) {
System.out.printf("x = 0, y = 0: %b%n", foo(0, 0));
System.out.printf("x = 0, y = 1: %b%n", foo(0, 1));
System.out.printf("x = 1, y = 0: %b%n", foo(1, 0));
System.out.printf("x = 1, y = 1: %b%n", foo(1, 1));
}
}
/var/tmp $ RUN_LOCALE="-Duser.language=en -Duser.country=US"
/var/tmp $ java $RUN_LOCALE -jar checkstyle-9.1-all.jar -c config.xml UnnecessaryParenthesesExample.java
Starting audit...
[ERROR] I:\_tmp\_checkstyle\UnnecessaryParenthesesExample.java:4:16: Unnecessary parentheses around expression. [UnnecessaryParentheses]
[ERROR] I:\_tmp\_checkstyle\UnnecessaryParenthesesExample.java:4:37: Unnecessary parentheses around expression. [UnnecessaryParentheses]
Audit done.
Checkstyle ends with 2 errors.
While technically these parentheses are indeed unnecessary (LAND has a higher precedence than LOR), it makes the intent more clear. I've only been able to disable this warning by removing LAND from the tokens, but then Checkstyle won't be strict enough.
For now I've been adding helper variables and methods to work around the issue, but it would be nice to at least allow some extra parentheses when combining LAND and LOR.
I have read check documentation: https://checkstyle.sourceforge.io/config_coding.html#UnnecessaryParentheses
I have downloaded the latest checkstyle from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words
While technically these parentheses are indeed unnecessary (
LANDhas a higher precedence thanLOR), it makes the intent more clear. I've only been able to disable this warning by removingLANDfrom the tokens, but then Checkstyle won't be strict enough.For now I've been adding helper variables and methods to work around the issue, but it would be nice to at least allow some extra parentheses when combining
LANDandLOR.