View Javadoc
1   /*
2   UnnecessaryParentheses
3   tokens = (default)EXPR, IDENT, NUM_DOUBLE, NUM_FLOAT, NUM_INT, NUM_LONG, \
4            STRING_LITERAL, LITERAL_NULL, LITERAL_FALSE, LITERAL_TRUE, ASSIGN, \
5            BAND_ASSIGN, BOR_ASSIGN, BSR_ASSIGN, BXOR_ASSIGN, DIV_ASSIGN, \
6            MINUS_ASSIGN, MOD_ASSIGN, PLUS_ASSIGN, SL_ASSIGN, SR_ASSIGN, STAR_ASSIGN, \
7            LAMBDA, TEXT_BLOCK_LITERAL_BEGIN, LAND, LITERAL_INSTANCEOF, GT, LT, GE, \
8            LE, EQUAL, NOT_EQUAL, UNARY_MINUS, UNARY_PLUS, INC, DEC, LNOT, BNOT, \
9            POST_INC, POST_DEC
10  
11  
12  */
13  
14  package com.puppycrawl.tools.checkstyle.checks.coding.unnecessaryparentheses;
15  public class InputUnnecessaryParenthesesOperatorsAndCasts {
16      int f1() {
17          int x = 0;
18          for (int i = (0+1); ((i) < (6+6)); i += (1+0)) { // 4 violations
19              x += (i + 100); // violation 'Unnecessary parentheses around assignment right-hand side'
20              (x) += (i + 100/**comment test*/); // 2 violations
21              x = (x + i + 100); // violation 'Unnecessary parentheses around assignment right.*side'
22              (x) = (x + i + 100); // 2 violations
23          }
24  
25          for (int i = (0+1); (i) < ((6+6)); i += (1+0)) { // 3 violations
26              System.identityHashCode("hi");
27          }
28  
29          return (0); // violation 'Unnecessary parentheses around literal '0''
30      }
31  
32      private int f2(int arg1, double arg2) {
33          int x, a, b, c, d;
34          String e, f;
35  
36          x = 0;
37          a = 0;
38          b = 0;
39          c = (a + b); // violation 'Unnecessary parentheses around assignment right-hand side'
40          d = c - 1;
41  
42          int i = (int) arg2;
43          i = ((int) arg2); // violation 'Unnecessary parentheses around assignment right-hand side'
44  
45          x += (i + 100 + arg1); // violation 'Unnecessary parentheses around assignment right.*side'
46          a = (a + b) * (c + d);
47          b = ((((a + b) * (c + d)))); // violation 'parentheses around assignment right.*side'
48          c = (((a) <= b)) ? 0 : 1; // violation 'Unnecessary parentheses around identifier 'a''
49          d = (a) + (b) * (600) / (int) (12.5f) + (int) (arg2); // 5 violations
50          e = ("this") + ("that") + ("is" + "other"); // 2 violations
51          f = ("this is a really, really long string that should be truncated."); // 2 violations
52  
53          return (x + a + b + d); // violation 'Unnecessary parentheses around return value'
54      }
55  
56      private boolean f3() {
57          int x = f2((1), (13.5)); // 2 violations
58          boolean b = (true); // violation 'Unnecessary parentheses around literal 'true''
59          return (b); // violation 'Unnecessary parentheses around identifier 'b''
60      }
61  
62      public static int f4(int z, int a) {
63          int r = (z * a); // violation 'Unnecessary parentheses around assignment right-hand side'
64          r = (a > z) ? a : z;
65          r = ((a > z) ? a : z); // violation 'Unnecessary parentheses around assignment right.*side'
66          r = (a > z) ? a : (z + z);
67          return (r * r - 1); // violation 'Unnecessary parentheses around return value'
68      }
69  
70      public void f5() {
71          int x, y;
72          x = 0;
73          y = 0;
74          if (x == y) {
75              print(x);
76          }
77          if ((x > y)) { // violation 'Unnecessary parentheses around expression'
78              print(y);
79          }
80  
81          while ((x < 10)) { // violation 'Unnecessary parentheses around expression'
82              print(x++);
83          }
84  
85          do {
86              print((y+=100)); // violation 'Unnecessary parentheses around expression'
87          } while (y < (4000)); // violation 'Unnecessary parentheses around literal '4000''
88      }
89  
90      private void f6(TypeA a) {
91          TypeB b = (TypeB) a;
92          TypeC c = ((TypeC) a); // violation 'Unnecessary parentheses around assignment right.*side'
93          int r = 12345;
94          r <<= (3); // 2 violations
95          GenT<String> d = ((GenT<String>) a); // violation 'paren.* around assignment right.*side'
96      }
97  
98      private void print(int arg)
99      {
100         System.identityHashCode("arg = " + arg);
101     }
102 
103     private int f7() {
104         String f;
105 
106         f = ("12345678901234567890123"); // 2 violations
107 
108         return 0;
109     }
110 
111     static class GenT<T> {}
112     static class TypeA extends GenT<String> {}
113     static class TypeB extends TypeA {}
114     static class TypeC extends TypeA {}
115 }