From b5eee7a5a2e76208bc8feaad36070f4a8b7eb195 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Sat, 25 Jun 2016 15:52:40 +0200 Subject: [PATCH] IDEA-157857 (Inspection: Math.random()' cast to 'int' - improvement suggestions) --- .../siyeh/InspectionGadgetsBundle.properties | 2 +- .../bugs/MathRandomCastToIntInspection.java | 30 ++++++++++++------- .../MathRandomCastToInt.java | 3 +- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties index 8ad1340f7bb1..35b243cea79e 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties @@ -1887,7 +1887,7 @@ mismatched.string.builder.query.update.display.name=Mismatched query and update mismatched.string.builder.updated.problem.descriptor=Contents of {0} #ref are updated, but never queried #loc mismatched.string.builder.queried.problem.descriptor=Contents of {0} #ref 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=#ref cast to 'int' is always rounded down to '0' #loc +math.random.cast.to.int.problem.descriptor=#ref 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 #ref is always inverted #loc diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/MathRandomCastToIntInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/MathRandomCastToIntInspection.java index 63d2f52e55f0..a7608b23abb7 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/MathRandomCastToIntInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/MathRandomCastToIntInspection.java @@ -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); } } } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/math_random_cast_to_int/MathRandomCastToInt.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/math_random_cast_to_int/MathRandomCastToInt.java index a470067de409..ad50b6e3ec18 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/math_random_cast_to_int/MathRandomCastToInt.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/math_random_cast_to_int/MathRandomCastToInt.java @@ -4,6 +4,7 @@ public class MathRandomCastToInt { void foo() { int runs = (int) Math.random() * 1000000 * 2; - long random = (long) Math.random() * 10; + long random = (long) Math.random() * 10; + long r = (byte) -+-+-+Math.random()*8L; } }