diff --git a/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties index 3ecae1993797..106bef988a7a 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties @@ -1815,3 +1815,6 @@ for.can.be.foreach.option=Report indexed java.util.List loops cast.conflicts.with.instanceof.quickfix1=Replace cast to ''{0}'' with ''{1}'' cast.conflicts.with.instanceof.quickfix2=Replace instanceof ''{0}'' with ''{1}'' double.checked.locking.quickfix=Make ''{0}'' volatile +double.literal.may.be.float.literal.display.name=double literal cast to long could be float literal +double.literal.may.be.float.literal.problem.descriptor=#ref could be replaced with ''{0}'' #loc +double.literal.may.be.float.literal.quickfix=Replace with ''{0}'' \ No newline at end of file diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/InspectionGadgetsPlugin.java b/plugins/InspectionGadgets/src/com/siyeh/ig/InspectionGadgetsPlugin.java index 1b8148bcec62..1cdf74d1f5bb 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/InspectionGadgetsPlugin.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/InspectionGadgetsPlugin.java @@ -1028,6 +1028,7 @@ public class InspectionGadgetsPlugin implements ApplicationComponent, m_inspectionClasses.add(ConfusingFloatingPointLiteralInspection.class); m_inspectionClasses.add(ConstantMathCallInspection.class); m_inspectionClasses.add(DivideByZeroInspection.class); + m_inspectionClasses.add(DoubleLiteralMayBeFloatLiteralInspection.class); m_inspectionClasses.add(FloatingPointEqualityInspection.class); m_inspectionClasses.add(ImplicitNumericConversionInspection.class); m_inspectionClasses.add(IntegerDivisionInFloatingPointContextInspection.class); diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/numeric/DoubleLiteralMayBeFloatLiteralInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/numeric/DoubleLiteralMayBeFloatLiteralInspection.java new file mode 100644 index 000000000000..5000f8224a35 --- /dev/null +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/numeric/DoubleLiteralMayBeFloatLiteralInspection.java @@ -0,0 +1,150 @@ +/* + * Copyright 2010 Bas Leijdekkers + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.siyeh.ig.numeric; + +import com.intellij.codeInspection.ProblemDescriptor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.*; +import com.intellij.util.IncorrectOperationException; +import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.BaseInspection; +import com.siyeh.ig.BaseInspectionVisitor; +import com.siyeh.ig.InspectionGadgetsFix; +import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.NotNull; + +public class DoubleLiteralMayBeFloatLiteralInspection extends BaseInspection { + + @Override + @Nls + @NotNull + public String getDisplayName() { + return InspectionGadgetsBundle.message( + "double.literal.may.be.float.literal.display.name"); + } + + @Override + @NotNull + protected String buildErrorString(Object... infos) { + final PsiTypeCastExpression typeCastExpression = + (PsiTypeCastExpression) infos[0]; + final StringBuilder replacementText = + buildReplacementText(typeCastExpression, new StringBuilder()); + return InspectionGadgetsBundle.message( + "double.literal.may.be.float.literal.problem.descriptor", + replacementText); + } + + @Override + protected InspectionGadgetsFix buildFix(Object... infos) { + final PsiTypeCastExpression typeCastExpression = + (PsiTypeCastExpression) infos[0]; + final StringBuilder replacementText = + buildReplacementText(typeCastExpression, new StringBuilder()); + return new IntLiteralMayBeLongLiteralFix(replacementText.toString()); + } + + private static StringBuilder buildReplacementText( + PsiExpression expression, StringBuilder out) { + if (expression instanceof PsiLiteralExpression) { + out.append(expression.getText()); + out.append('f'); + } else if (expression instanceof PsiPrefixExpression) { + final PsiPrefixExpression prefixExpression = + (PsiPrefixExpression) expression; + final PsiJavaToken sign = prefixExpression.getOperationSign(); + out.append(sign.getText()); + return buildReplacementText(prefixExpression.getOperand(), out); + } else if (expression instanceof PsiParenthesizedExpression) { + final PsiParenthesizedExpression parenthesizedExpression = + (PsiParenthesizedExpression) expression; + out.append('('); + buildReplacementText(parenthesizedExpression.getExpression(), + out); + out.append(')'); + } else if (expression instanceof PsiTypeCastExpression) { + final PsiTypeCastExpression typeCastExpression = + (PsiTypeCastExpression) expression; + final PsiExpression operand = typeCastExpression.getOperand(); + buildReplacementText(operand, out); + } else { + assert false; + } + return out; + } + + private static class IntLiteralMayBeLongLiteralFix + extends InspectionGadgetsFix { + + private final String replacementString; + + public IntLiteralMayBeLongLiteralFix(String replacementString) { + this.replacementString = replacementString; + } + + @NotNull + public String getName() { + return InspectionGadgetsBundle.message( + "double.literal.may.be.float.literal.quickfix", + replacementString); + } + + @Override + protected void doFix(Project project, ProblemDescriptor descriptor) + throws IncorrectOperationException { + final PsiElement element = descriptor.getPsiElement(); + if (!(element instanceof PsiTypeCastExpression)) { + return; + } + final PsiTypeCastExpression typeCastExpression = + (PsiTypeCastExpression) element; + replaceExpression(typeCastExpression, replacementString); + } + } + + @Override + public BaseInspectionVisitor buildVisitor() { + return new IntLiteralMayBeLongLiteralVisitor(); + } + + private static class IntLiteralMayBeLongLiteralVisitor + extends BaseInspectionVisitor { + + @Override + public void visitLiteralExpression(PsiLiteralExpression expression) { + super.visitLiteralExpression(expression); + final PsiType type = expression.getType(); + if (PsiType.DOUBLE != type) { + return; + } + PsiElement parent = expression.getParent(); + while (parent instanceof PsiPrefixExpression || + parent instanceof PsiParenthesizedExpression) { + parent = parent.getParent(); + } + if (!(parent instanceof PsiTypeCastExpression)) { + return; + } + final PsiTypeCastExpression typeCastExpression = + (PsiTypeCastExpression) parent; + final PsiType castType = typeCastExpression.getType(); + if (PsiType.FLOAT != castType) { + return; + } + registerError(typeCastExpression, typeCastExpression); + } + } +} diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/DoubleLiteralMayBeFloatLiteral.html b/plugins/InspectionGadgets/src/inspectionDescriptions/DoubleLiteralMayBeFloatLiteral.html new file mode 100644 index 000000000000..6bfa21cd5086 --- /dev/null +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/DoubleLiteralMayBeFloatLiteral.html @@ -0,0 +1,9 @@ + +
+ +This inspection reports double literal expressions +which are immediately cast to float. Such +literal expressions can be replaced with the equivalent float +literal. +
New in 10, Powered by InspectionGadgets
+ diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/IntLiteralMayBeLongLiteral.html b/plugins/InspectionGadgets/src/inspectionDescriptions/IntLiteralMayBeLongLiteral.html index 559f7f664889..c27294fd7c1b 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/IntLiteralMayBeLongLiteral.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/IntLiteralMayBeLongLiteral.html @@ -1,9 +1,9 @@
-This inspection reports int literal expression +This inspection reports int literal expressions which are immediately cast to long. Such -literal expression can be replaced with the equivalent long +literal expressions can be replaced with the equivalent long literal.
New in 9, Powered by InspectionGadgets
\ No newline at end of file