View Javadoc
1   /*xml
2   <module name="Checker">
3     <module name="TreeWalker">
4       <module name="UnnecessaryParentheses"/>
5     </module>
6   </module>
7   */
8   package com.puppycrawl.tools.checkstyle.checks.coding.unnecessaryparentheses;
9   
10  import java.util.List;
11  
12  // xdoc section -- start
13  class Example1 {
14  
15    public int square(int a, int b) {
16      // violation below, 'Unnecessary parentheses around assignment right-hand side'
17      int square = (a * b);
18      // violation below, 'Unnecessary parentheses around identifier 'square''
19      return (square);
20    }
21  
22    int sumOfSquares = 0;
23    public void sumOfSquares() {
24      // violation below, 'Unnecessary parentheses around literal '0''
25      for (int i = (0); i < 10; i++) {
26        // violation below, 'Unnecessary parentheses around assignment right-hand side'
27        int x = (i + 1);
28        sumOfSquares += (square(x,x));  // 2 violations
29      }
30    }
31  
32    List<String> myList = List.of("a1", "b1", "c1");
33    public void filter() {
34      myList.stream()
35          // violation below, 'Unnecessary parentheses around lambda value'
36          .filter((s) -> s.startsWith("c"))
37              .forEach(System.out::println);
38    }
39  
40    int a = 10, b = 12, c = 15;
41    boolean x = true, y = false, z = true;
42    public void test() {
43      // 3 violations below
44      if ((a >= 0 && b <= 9) || (c >= 5 && b <= 5) || (c >= 3 && a <= 7)) {
45        return;
46      }
47      // violation below, 'Unnecessary parentheses around expression'
48      if ((-a) != -27 && b > 5) {
49        return;
50      }
51      if (x == (a <= 15)) {
52        return;
53      }
54      if (x == (y == z)) {
55        return;
56      }
57    }
58  
59  }
60  // xdoc section -- end