Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnnecessaryParentheses: no violation if casting is present before expression #14872

Open
romani opened this issue May 12, 2024 · 1 comment
Open
Labels

Comments

@romani
Copy link
Member

romani commented May 12, 2024

detected at https://github.com/checkstyle/checkstyle/pull/14856/files#r1597531069

I have read check documentation: https://checkstyle.org/checks/coding/unnecessaryparentheses.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

$ cat config.xml 
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
          "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
  <module name="TreeWalker">
    <module name="UnnecessaryParentheses">
    </module>
  </module>
</module>

$ cat Test.java 
import java.util.Arrays;

public class Test
{
  public static void main(String[] args) {
    String[] a = { "s", "", "1", "", "" };
    Arrays.stream(a).filter(s -> !((boolean)s.isEmpty())).toArray(String[]::new); // should be violation
    Arrays.stream(a).filter(s -> !(boolean)s.isEmpty()).toArray(String[]::new);
  }
}

$ java -jar checkstyle-10.16.0-all.jar -c config.xml Test.java
Starting audit...
Audit done.

Expected: violation on line 7


https://introcs.cs.princeton.edu/java/11precedence/

Most programmers do not memorize them all, and, even those that do, use parentheses for clarity.

higher precedence executes first.

Level Operator Description Associativity
16 () [] new . :: parentheses, array access, object creation, member access, method reference left-to-right
15 =++ -- unary post-increment, unary post-decrement left-to-right
14 =+ - ! ~ ++ -- unary plus, unary minus, unary logical NOT, unary bitwise NOT, unary pre-increment, unary pre-decrement right-to-left
13 () cast right-to-left
12 * / % multiplicative left-to-right
11 =+ - + additive, string concatenation left-to-right
10 << >> >>> shift left-to-right
9 < <= > >= instanceof relational left-to-right
8 == != equality left-to-right
7 & bitwise AND left-to-right
6 ^ bitwise XOR left-to-right
5 | bitwise OR left-to-right
4 && logical AND left-to-right
3 || logical OR left-to-right
2 ?: ternary right-to-left
1 = += -= *= /= %= &= ^= |= <<= >>= >>>= assignment right-to-left
0 -> -> lambda expression, switch expression right-to-left
@mohitsatr
Copy link
Contributor

mohitsatr commented Nov 18, 2024

here are more examples

import java.util.HashSet;
public class Parenthesis {

  public static void main(String[] args) {

    boolean k = true;
    boolean j = !((boolean) k); // should be violation

    Integer num = 1; 
    boolean isTrue = !((boolean) (num != 0));


    new HashSet<Integer>()
    .stream()
    .filter(f -> f > ((Integer) 0)); //should be violation 

    new HashSet<Integer>()
    .stream()
    .filter(f -> ((Integer)f) > ((Integer) 0)); //should be 2 violations 

    new HashSet<Integer>()
    .stream()
    .filter(f -> (Integer)f > (Integer) 0);
  } 

}

im on it.

mohitsatr added a commit to mohitsatr/checkstyle that referenced this issue Nov 26, 2024
mohitsatr added a commit to mohitsatr/checkstyle that referenced this issue Nov 26, 2024
mohitsatr added a commit to mohitsatr/checkstyle that referenced this issue Nov 26, 2024
mohitsatr added a commit to mohitsatr/checkstyle that referenced this issue Nov 26, 2024
mohitsatr added a commit to mohitsatr/checkstyle that referenced this issue Dec 2, 2024
mohitsatr added a commit to mohitsatr/checkstyle that referenced this issue Dec 2, 2024
mohitsatr added a commit to mohitsatr/checkstyle that referenced this issue Dec 4, 2024
mohitsatr added a commit to mohitsatr/checkstyle that referenced this issue Dec 13, 2024
rnveach pushed a commit to mohitsatr/checkstyle that referenced this issue Dec 13, 2024
mohitsatr added a commit to mohitsatr/checkstyle that referenced this issue Dec 13, 2024
mohitsatr added a commit to mohitsatr/checkstyle that referenced this issue Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants