IDEA-157857 (Inspection: Math.random()' cast to 'int' - improvement suggestions)

This commit is contained in:
Bas Leijdekkers
2016-06-25 15:53:21 +02:00
parent 243eeb0364
commit b5eee7a5a2
3 changed files with 23 additions and 12 deletions
@@ -1887,7 +1887,7 @@ mismatched.string.builder.query.update.display.name=Mismatched query and update
mismatched.string.builder.updated.problem.descriptor=Contents of {0} <code>#ref</code> are updated, but never queried #loc
mismatched.string.builder.queried.problem.descriptor=Contents of {0} <code>#ref</code> are queried, but never updated #loc
math.random.cast.to.int.display.name='Math.random()' cast to 'int'
math.random.cast.to.int.problem.descriptor=<code>#ref</code> cast to 'int' is always rounded down to '0' #loc
math.random.cast.to.int.problem.descriptor=<code>#ref</code> cast to ''{0}'' is always rounded down to ''0'' #loc
math.random.cast.to.int.quickfix=Add parentheses to perform multiplication before cast
boolean.variable.always.inverted.display.name=Boolean variable is always inverted
boolean.field.always.inverted.problem.descriptor=Boolean field <code>#ref</code> is always inverted #loc
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2012 Bas Leijdekkers
* Copyright 2011-2016 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -40,7 +40,8 @@ public class MathRandomCastToIntInspection extends BaseInspection {
@NotNull
@Override
protected String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message("math.random.cast.to.int.problem.descriptor");
final PsiType type = (PsiType)infos[1];
return InspectionGadgetsBundle.message("math.random.cast.to.int.problem.descriptor", type.getPresentableText());
}
@Override
@@ -57,7 +58,7 @@ public class MathRandomCastToIntInspection extends BaseInspection {
}
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)parent;
final IElementType tokenType = polyadicExpression.getOperationTokenType();
if (JavaTokenType.ASTERISK != tokenType) {
if (JavaTokenType.ASTERISK != tokenType || polyadicExpression.getType() == null) {
return null;
}
return new MathRandomCastToIntegerFix();
@@ -72,14 +73,16 @@ public class MathRandomCastToIntInspection extends BaseInspection {
@Override
@NotNull
public String getName() {
return InspectionGadgetsBundle.message(
"math.random.cast.to.int.quickfix");
return InspectionGadgetsBundle.message("math.random.cast.to.int.quickfix");
}
@Override
protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
final PsiElement element = descriptor.getPsiElement();
final PsiElement parent = element.getParent();
PsiElement parent = element.getParent();
while (parent instanceof PsiPrefixExpression) {
parent = parent.getParent();
}
if (!(parent instanceof PsiTypeCastExpression)) {
return;
}
@@ -93,8 +96,12 @@ public class MathRandomCastToIntInspection extends BaseInspection {
if (operand == null) {
return;
}
final PsiType type = polyadicExpression.getType();
if (type == null) {
return;
}
@NonNls final StringBuilder newExpression = new StringBuilder();
newExpression.append("(int)(");
newExpression.append("(").append(type.getCanonicalText()).append(")(");
final PsiExpression[] operands = polyadicExpression.getOperands();
for (final PsiExpression expression : operands) {
final PsiJavaToken token = polyadicExpression.getTokenBeforeOperand(expression);
@@ -123,7 +130,10 @@ public class MathRandomCastToIntInspection extends BaseInspection {
@Override
public void visitTypeCastExpression(PsiTypeCastExpression expression) {
super.visitTypeCastExpression(expression);
final PsiExpression operand = expression.getOperand();
PsiExpression operand = expression.getOperand();
while (operand instanceof PsiPrefixExpression) {
operand = ((PsiPrefixExpression)operand).getOperand();
}
if (!(operand instanceof PsiMethodCallExpression)) {
return;
}
@@ -132,7 +142,7 @@ public class MathRandomCastToIntInspection extends BaseInspection {
return;
}
final PsiType type = castType.getType();
if (!PsiType.INT.equals(type) && !PsiType.LONG.equals(type)) {
if (!(type instanceof PsiPrimitiveType) || PsiType.DOUBLE.equals(type) || PsiType.FLOAT.equals(type) || PsiType.BOOLEAN.equals(type)) {
return;
}
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)operand;
@@ -154,7 +164,7 @@ public class MathRandomCastToIntInspection extends BaseInspection {
if (!"java.lang.Math".equals(qualifiedName) && !"java.lang.StrictMath".equals(qualifiedName)) {
return;
}
registerError(methodCallExpression, expression);
registerError(methodCallExpression, expression, type);
}
}
}
@@ -4,6 +4,7 @@ public class MathRandomCastToInt {
void foo() {
int runs = (int) <warning descr="'Math.random()' cast to 'int' is always rounded down to '0'">Math.random()</warning> * 1000000 * 2;
long random = (long) <warning descr="'Math.random()' cast to 'int' is always rounded down to '0'">Math.random()</warning> * 10;
long random = (long) <warning descr="'Math.random()' cast to 'long' is always rounded down to '0'">Math.random()</warning> * 10;
long r = (byte) -+-+-+<warning descr="'Math.random()' cast to 'byte' is always rounded down to '0'">Math.random()</warning>*8L;
}
}