Skip to content

Commit

Permalink
supplemental: test-cases for unnecessary parentheses on typecasting
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitsatr authored and romani committed Jan 25, 2025
1 parent ad7b6ba commit 95eeefd
Show file tree
Hide file tree
Showing 3 changed files with 310 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
UnnecessaryParentheses
tokens = (default)EXPR, IDENT, NUM_DOUBLE, NUM_FLOAT, NUM_INT, NUM_LONG, \
STRING_LITERAL, LITERAL_NULL, LITERAL_FALSE, LITERAL_TRUE, ASSIGN, \
BAND_ASSIGN, BOR_ASSIGN, BSR_ASSIGN, BXOR_ASSIGN, DIV_ASSIGN, \
MINUS_ASSIGN, MOD_ASSIGN, PLUS_ASSIGN, SL_ASSIGN, SR_ASSIGN, STAR_ASSIGN, \
LAMBDA, TEXT_BLOCK_LITERAL_BEGIN, LAND, LITERAL_INSTANCEOF, GT, LT, GE, \
LE, EQUAL, NOT_EQUAL, UNARY_MINUS, UNARY_PLUS, INC, DEC, LNOT, BNOT, \
POST_INC, POST_DEC
*/
package com.puppycrawl.tools.checkstyle.checks.coding.unnecessaryparentheses;
public class InputUnnecessaryParenthesesCasts1 {
public void valid1() {
int x = 23;
int y = 44;
float k = 12f;

//no violation below until https://github.com/checkstyle/checkstyle/issues/14872
int d = ((int) 100f) + 100 * 2 / ((int) 12.5) + (int) 90f;
int p = (int) 110f + 10 * 2 / (int) 10f + (int) 32.2;

y = (int) (22.2 * 2) / ((int) 8f + 5);

double arg2 = 23.2;
int i = (int) arg2;
i = ((int) arg2); // violation 'Unnecessary parentheses around assignment right-hand side'
p = (int) arg2;

//no violation below until https://github.com/checkstyle/checkstyle/issues/14872
x = (2 * 2 /((int) k));
x = 2 * 2 / (int) k;

int par = ((int)2f * 2) / 4;
y = ((Integer) x).hashCode();

int py = 12;
float xy = 40f;
int yp = 0;
boolean finished = true;
boolean result = false;

//no violation below until https://github.com/checkstyle/checkstyle/issues/14872
if(py >= ((int)xy) || (yp ==1 | py >=1)) {
xy--;
}
else if(yp >= (int)xy || (py ==1 | py >=1)) {
xy++;
}

if (!((int) xy > yp) && py < 20) {
py++;
}

if (35 + (int)'a' == 100) {
py++;
}

boolean checkone = true;
//no violation below until https://github.com/checkstyle/checkstyle/issues/14872
if (!((boolean) checkone)) {
checkone = false;
}
else if ((boolean) checkone) {
checkone = !checkone;
}

double limit = 3.2;
//no violation below until https://github.com/checkstyle/checkstyle/issues/14872
for (int j = 0; j >= ((int) limit); j++) {
yp +=1;
}
for (int j =0; j >= (int) limit; j++) {
py--;
break;
}

//no violation below until https://github.com/checkstyle/checkstyle/issues/14872
for(int j = 10; !finished && !((boolean) (j > 5)) ; j++){
break;
}
for(int jp = 9; !finished || !(boolean) (jp >5); jp++){
checkone = false;
break;
}

//no violation below until https://github.com/checkstyle/checkstyle/issues/14872
long p1 = ((long) ((20 >> 24 ) & 0xFF)) << 24;
long p2 = (long) ((20 >> 24 ) & 0xFF) << 24;

//until https://github.com/checkstyle/checkstyle/issues/14872
// could be operator precedence issue
float f4 = -((float) 42);
float f5 = -(float) 90;

//until https://github.com/checkstyle/checkstyle/issues/14872
long shiftedbytwo = ((long)x) << 2;
long shiftedbythree = (long)y << 3;

//until https://github.com/checkstyle/checkstyle/issues/14872
// could be operator precedence issue
short complement = (short) ~((short) 87777);
short bcomplement = (short) ~(short) 90122;

//no violation below until https://github.com/checkstyle/checkstyle/issues/14872
int numSlices1 = (int) Math.max(Math.ceil(((double) 20) / 10), 1);
int numSlices2 = (int) Math.max(Math.ceil((double) 20 / 10), 1);
}
private long getLong1(int start, int end) {
//no violation below until https://github.com/checkstyle/checkstyle/issues/14872
return (((long) start) << 32) | 0xFFFFFFFFL & end;
}
private long getLong2(int start, int end) {
return ((long) start << 32) | 0xFFFFFFFFL & end;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
UnnecessaryParentheses
tokens = (default)EXPR, IDENT, NUM_DOUBLE, NUM_FLOAT, NUM_INT, NUM_LONG, \
STRING_LITERAL, LITERAL_NULL, LITERAL_FALSE, LITERAL_TRUE, ASSIGN, \
BAND_ASSIGN, BOR_ASSIGN, BSR_ASSIGN, BXOR_ASSIGN, DIV_ASSIGN, \
MINUS_ASSIGN, MOD_ASSIGN, PLUS_ASSIGN, SL_ASSIGN, SR_ASSIGN, STAR_ASSIGN, \
LAMBDA, TEXT_BLOCK_LITERAL_BEGIN, LAND, LITERAL_INSTANCEOF, GT, LT, GE, \
LE, EQUAL, NOT_EQUAL, UNARY_MINUS, UNARY_PLUS, INC, DEC, LNOT, BNOT, \
POST_INC, POST_DEC
*/

package com.puppycrawl.tools.checkstyle.checks.coding.unnecessaryparentheses;

import static org.junit.Assert.assertEquals;
import static java.lang.Math.abs;
import java.util.Arrays;
import java.util.HashSet;
import java.util.function.ToIntFunction;

public class InputUnnecessaryParenthesesCasts2 {
public void fooConditionals(T element) {
int x = 23;
int y = 44;
float k = 12f;
boolean finished = true;
boolean result = false;

String filevalue = "FILEVALUE";
if (!finished || !((boolean) filevalue.contains("O"))) {
// no violation until https://github.com/checkstyle/checkstyle/issues/14872
filevalue += "F";
}
else if (finished || !(boolean) filevalue.contains("O")) {
filevalue += "G";
}

if (result && finished || ((int) 23.1 + 21) == 32) {
y--;
}

// no violation until https://github.com/checkstyle/checkstyle/issues/14872
if (!((boolean) filevalue.contains("G")) || finished) {
x++;
}
else if (!(boolean) filevalue.contains("P") || finished) {
filevalue += "p";
}

String[] a = {"s", "a", "1", "2", "3"};
String[] abr = {"18", "z", "w", "30", "u", "vel"};
Arrays.stream(a)
.filter(s -> !((boolean) s.isEmpty()))
// no violation below until https://github.com/checkstyle/checkstyle/issues/14872
.toArray(String[]::new);

Arrays.stream(abr).filter(s -> !(boolean) s.isEmpty())
.toArray(String[]::new);

Arrays.stream(a)//no violation until https://github.com/checkstyle/checkstyle/issues/14872
.filter(s -> ((boolean) s.isEmpty()))
.toArray(String[]::new);
Arrays.stream(abr)
.filter(s -> (boolean) s.isEmpty())
.toArray(String[]::new);

new HashSet<Integer>().stream()
.filter(f -> f > ((int) 1.1 + 200));

//no violation below until https://github.com/checkstyle/checkstyle/issues/14872
if (((double) abs(10 - 2)) / 2 <= 0.01) {
y += 10;
}
else if ((double) abs(10 - 2) / 2 >= 0.02) {
x += 2;
}

final ToIntFunction<T> comparison1 = ((Comparable<T>) element)::compareTo; // no violation

Bean bean = new Bean();
assertEquals(1, ((int[]) bean.array)[0]); // no violation

//no violation below until https://github.com/checkstyle/checkstyle/issues/14872
float rest = ((float) (50 - System.currentTimeMillis())) / 1000;
float stop = (float) (50 - System.currentTimeMillis()) / 1000;

// no violation below as parentheses detection in ternary(?) operators
// is handled by different logic and is dependent on QUESION token
float pin = false ? ((float) 21) : ((float) 31);

Object obj = "hello"; // no violation below
String result1 = (obj instanceof String) ? ((String) obj) : "Default";

//no violation below until https://github.com/checkstyle/checkstyle/issues/14872
String op1 = ((String) obj) + ((Boolean) finished).toString();
String op2 = (String) obj + ((Boolean) finished).toString();

filevalue = "'" + ((char) 32) + "'";
// violation above 'Unnecessary parentheses around expression.'
filevalue = "'" + (char) 31 + "'"; // no violation

//no violation below until https://github.com/checkstyle/checkstyle/issues/14872
ck("name", (long) k, (long) ((byte) 0x22));
ck("check", (long) k, (long) (byte) 0x24);
}

public void ck(String byt, long a, long c) {}
static class T {}
public class Bean {
public Object array;
public Bean() {
array = new int[]{1, 2, 3};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
UnnecessaryParentheses
tokens = (default)EXPR, IDENT, NUM_DOUBLE, NUM_FLOAT, NUM_INT, NUM_LONG, \
STRING_LITERAL, LITERAL_NULL, LITERAL_FALSE, LITERAL_TRUE, ASSIGN, \
BAND_ASSIGN, BOR_ASSIGN, BSR_ASSIGN, BXOR_ASSIGN, DIV_ASSIGN, \
MINUS_ASSIGN, MOD_ASSIGN, PLUS_ASSIGN, SL_ASSIGN, SR_ASSIGN, STAR_ASSIGN, \
LAMBDA, TEXT_BLOCK_LITERAL_BEGIN, LAND, LITERAL_INSTANCEOF, GT, LT, GE, \
LE, EQUAL, NOT_EQUAL, UNARY_MINUS, UNARY_PLUS, INC, DEC, LNOT, BNOT, \
POST_INC, POST_DEC
*/

package com.puppycrawl.tools.checkstyle.checks.coding.unnecessaryparentheses;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InputUnnecessaryParenthesesCasts3 {
private static final double MDP_MULT = 2.2;

public void fooUnnecessaryParentheses(String hexa) {
Object b = new Object();
int unm = 30;
boolean Eval = false;
List<Object> siZ = new ArrayList<>();
Map<Object, Object> record = new HashMap<>();
if (hexa.contains("Z")) {
throw new IllegalArgumentException(((char) b) + " is not a hexadecimal digit");
// violation above 'Unnecessary parentheses around expression.'
}
if (hexa.contains("Y")) {
throw new IllegalArgumentException((char) b + " is not a hexadecimal digit");
}

Attrs le = new Attrs();
String fileName = "log.txt";
setFileLastModified(fileName,
//no violation below until https://github.com/checkstyle/checkstyle/issues/14872
((long) le.getMTime()) * 1000);
setFileLastModified(fileName,
(long) le.getMTime() * 1000);

//no violation below until https://github.com/checkstyle/checkstyle/issues/14872
if (!Eval && unm > 0 && ((int) (12f * unm - 1)) > unm+20) {}
if (!Eval && unm > 0 && (int) (12f * unm - 1) > unm+20) {}

//no violation below until https://github.com/checkstyle/checkstyle/issues/14872
final long l8 = ((long) record.get(unm)) & 0xffL;
final long l9 = (long) record.get(unm) & 0xffL;

int maxSize = 21;
//no violation below until https://github.com/checkstyle/checkstyle/issues/14872
double used = maxSize == 0 ? 0 : 100 * (((double) unm) / maxSize);
double used1 = maxSize == 0 ? 0 : 100 * ((double) unm / maxSize);

int dX = 90;
int dY = 2;
//no violation below until https://github.com/checkstyle/checkstyle/issues/14872
int stepx = (int) (((double)dX) * MDP_MULT) / dY;
int stepy = (int) ((double)dX * MDP_MULT) / dY;

Object layerOffset;
//no violation below until https://github.com/checkstyle/checkstyle/issues/14872
unm += ((int) (Math.sqrt(unm) * 1.5));
unm += (int) (Math.sqrt(unm) * 1.5);
}

public static void setFileLastModified(String fileName, long lastModified) {}
public static class Attrs {
int getMTime() {
return 123;
}
}
}

0 comments on commit 95eeefd

Please sign in to comment.