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  package com.puppycrawl.tools.checkstyle.checks.coding.unnecessaryparentheses;
14  public class InputUnnecessaryParenthesesCasts1 {
15      public void valid1() {
16          int x = 23;
17          int y = 44;
18          float k = 12f;
19  
20          //no violation below until https://github.com/checkstyle/checkstyle/issues/14872
21          int d = ((int) 100f) + 100 * 2 / ((int) 12.5) + (int) 90f;
22          int p = (int) 110f + 10 * 2 / (int) 10f + (int) 32.2;
23  
24          y = (int) (22.2 * 2) / ((int) 8f + 5);
25  
26          double arg2 = 23.2;
27          int i = (int) arg2;
28          i = ((int) arg2); // violation 'Unnecessary parentheses around assignment right-hand side'
29          p = (int) arg2;
30  
31          //no violation below until https://github.com/checkstyle/checkstyle/issues/14872
32          x = (2 * 2 /((int) k));
33          x = 2 * 2 / (int) k;
34  
35          int par = ((int)2f * 2) / 4;
36          y = ((Integer) x).hashCode();
37  
38          int py = 12;
39          float xy = 40f;
40          int yp = 0;
41          boolean finished = true;
42          boolean result = false;
43  
44          //no violation below until https://github.com/checkstyle/checkstyle/issues/14872
45          if(py >= ((int)xy) || (yp ==1 | py >=1)) {
46              xy--;
47          }
48          else if(yp >= (int)xy || (py ==1 | py >=1)) {
49              xy++;
50          }
51  
52          if (!((int) xy > yp) && py < 20) {
53              py++;
54          }
55  
56          if (35 + (int)'a' == 100) {
57              py++;
58          }
59  
60          boolean checkone = true;
61          //no violation below until https://github.com/checkstyle/checkstyle/issues/14872
62          if (!((boolean) checkone)) {
63              checkone = false;
64          }
65          else if ((boolean) checkone) {
66              checkone = !checkone;
67          }
68  
69          double limit = 3.2;
70          //no violation below until https://github.com/checkstyle/checkstyle/issues/14872
71          for (int j = 0; j >= ((int) limit); j++) {
72              yp +=1;
73          }
74          for (int j =0; j >= (int) limit; j++) {
75              py--;
76              break;
77          }
78  
79          //no violation below until https://github.com/checkstyle/checkstyle/issues/14872
80          for(int j = 10; !finished && !((boolean) (j > 5)) ; j++){
81              break;
82          }
83          for(int jp = 9; !finished || !(boolean) (jp >5); jp++){
84              checkone = false;
85              break;
86          }
87  
88          //no violation below until https://github.com/checkstyle/checkstyle/issues/14872
89          long p1 = ((long) ((20 >> 24 ) & 0xFF)) << 24;
90          long p2 = (long) ((20 >> 24 ) & 0xFF) << 24;
91  
92          //until https://github.com/checkstyle/checkstyle/issues/14872
93          // could be operator precedence issue
94          float f4 = -((float) 42);
95          float f5 = -(float) 90;
96  
97          //until https://github.com/checkstyle/checkstyle/issues/14872
98          long shiftedbytwo = ((long)x) << 2;
99          long shiftedbythree = (long)y << 3;
100 
101         //until https://github.com/checkstyle/checkstyle/issues/14872
102         // could be operator precedence issue
103         short complement = (short) ~((short) 87777);
104         short bcomplement = (short) ~(short) 90122;
105 
106         //no violation below until https://github.com/checkstyle/checkstyle/issues/14872
107         int numSlices1 = (int) Math.max(Math.ceil(((double) 20) / 10), 1);
108         int numSlices2 = (int) Math.max(Math.ceil((double) 20 / 10), 1);
109     }
110     private long getLong1(int start, int end) {
111         //no violation below until https://github.com/checkstyle/checkstyle/issues/14872
112         return (((long) start) << 32) | 0xFFFFFFFFL & end;
113     }
114     private long getLong2(int start, int end) {
115         return ((long) start << 32) | 0xFFFFFFFFL & end;
116     }
117 }