mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-62403 (Implement new inspection "double literal cast to float could be float literal")
This commit is contained in:
@@ -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=<code>#ref</code> could be replaced with ''{0}'' #loc
|
||||
double.literal.may.be.float.literal.quickfix=Replace with ''{0}''
|
||||
@@ -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);
|
||||
|
||||
+150
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<body><table> <tr> <td valign="top" height="150">
|
||||
<font face="verdana" size="-1">
|
||||
This inspection reports <b><font color="#000080">double</font></b> literal expressions
|
||||
which are immediately cast to <b><font color="#000080">float</font></b>. Such
|
||||
literal expressions can be replaced with the equivalent <b><font color="#000080">float</font></b>
|
||||
literal.
|
||||
</font></td> </tr> <tr> <td height="20"> <font face="verdana" size="-2">New in 10, Powered by InspectionGadgets </font> </td> </tr> </table> </body>
|
||||
</html>
|
||||
+2
-2
@@ -1,9 +1,9 @@
|
||||
<html>
|
||||
<body><table> <tr> <td valign="top" height="150">
|
||||
<font face="verdana" size="-1">
|
||||
This inspection reports <b><font color="#000080">int</font></b> literal expression
|
||||
This inspection reports <b><font color="#000080">int</font></b> literal expressions
|
||||
which are immediately cast to <b><font color="#000080">long</font></b>. Such
|
||||
literal expression can be replaced with the equivalent <b><font color="#000080">long</font></b>
|
||||
literal expressions can be replaced with the equivalent <b><font color="#000080">long</font></b>
|
||||
literal.
|
||||
</font></td> </tr> <tr> <td height="20"> <font face="verdana" size="-2">New in 9, Powered by InspectionGadgets </font> </td> </tr> </table> </body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user