mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
DEA-91189 ("Numeric cast that loses precision" should have "ignore for equals" checkbox)
This commit is contained in:
+19
-22
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2003-2009 Dave Griffith, Bas Leijdekkers
|
||||
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,18 +16,17 @@
|
||||
package com.siyeh.ig.numeric;
|
||||
|
||||
import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
|
||||
import com.intellij.psi.PsiExpression;
|
||||
import com.intellij.psi.PsiType;
|
||||
import com.intellij.psi.PsiTypeCastExpression;
|
||||
import com.intellij.psi.PsiTypeElement;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.psiutils.ClassUtils;
|
||||
import com.siyeh.ig.psiutils.ExpressionUtils;
|
||||
import com.siyeh.ig.psiutils.MethodUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -36,8 +35,7 @@ public class CastThatLosesPrecisionInspection extends BaseInspection {
|
||||
/**
|
||||
* @noinspection StaticCollection
|
||||
*/
|
||||
private static final Map<PsiType, Integer> typePrecisions =
|
||||
new HashMap<PsiType, Integer>(7);
|
||||
private static final Map<PsiType, Integer> typePrecisions = new HashMap<PsiType, Integer>(7);
|
||||
|
||||
static {
|
||||
typePrecisions.put(PsiType.BYTE, 1);
|
||||
@@ -69,15 +67,12 @@ public class CastThatLosesPrecisionInspection extends BaseInspection {
|
||||
@NotNull
|
||||
public String buildErrorString(Object... infos) {
|
||||
final PsiType operandType = (PsiType)infos[0];
|
||||
return InspectionGadgetsBundle.message(
|
||||
"cast.that.loses.precision.problem.descriptor",
|
||||
operandType.getPresentableText());
|
||||
return InspectionGadgetsBundle.message("cast.that.loses.precision.problem.descriptor", operandType.getPresentableText());
|
||||
}
|
||||
|
||||
@Override
|
||||
public JComponent createOptionsPanel() {
|
||||
return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle.message(
|
||||
"cast.that.loses.precision.option"),
|
||||
return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle.message("cast.that.loses.precision.option"),
|
||||
this, "ignoreIntegerCharCasts");
|
||||
}
|
||||
|
||||
@@ -90,8 +85,7 @@ public class CastThatLosesPrecisionInspection extends BaseInspection {
|
||||
extends BaseInspectionVisitor {
|
||||
|
||||
@Override
|
||||
public void visitTypeCastExpression(
|
||||
@NotNull PsiTypeCastExpression expression) {
|
||||
public void visitTypeCastExpression(@NotNull PsiTypeCastExpression expression) {
|
||||
final PsiType castType = expression.getType();
|
||||
if (!ClassUtils.isPrimitiveNumericType(castType)) {
|
||||
return;
|
||||
@@ -112,8 +106,13 @@ public class CastThatLosesPrecisionInspection extends BaseInspection {
|
||||
return;
|
||||
}
|
||||
}
|
||||
Object result =
|
||||
ExpressionUtils.computeConstantExpression(operand);
|
||||
if (PsiType.LONG.equals(operandType) && PsiType.INT.equals(castType)) {
|
||||
final PsiMethod method = PsiTreeUtil.getParentOfType(expression, PsiMethod.class, true, PsiClass.class);
|
||||
if (MethodUtils.isHashCode(method)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
Object result = ExpressionUtils.computeConstantExpression(operand);
|
||||
if (result instanceof Character) {
|
||||
result = Integer.valueOf(((Character)result).charValue());
|
||||
}
|
||||
@@ -130,15 +129,13 @@ public class CastThatLosesPrecisionInspection extends BaseInspection {
|
||||
registerError(castTypeElement, operandType);
|
||||
}
|
||||
|
||||
private boolean hasLowerPrecision(PsiType operandType,
|
||||
PsiType castType) {
|
||||
private boolean hasLowerPrecision(PsiType operandType, PsiType castType) {
|
||||
final Integer operandPrecision = typePrecisions.get(operandType);
|
||||
final Integer castPrecision = typePrecisions.get(castType);
|
||||
return operandPrecision <= castPrecision;
|
||||
return operandPrecision.intValue() <= castPrecision.intValue();
|
||||
}
|
||||
|
||||
private boolean valueIsContainableInType(Number value,
|
||||
PsiType type) {
|
||||
private boolean valueIsContainableInType(Number value, PsiType type) {
|
||||
final long longValue = value.longValue();
|
||||
final double doubleValue = value.doubleValue();
|
||||
if (PsiType.BYTE.equals(type)) {
|
||||
|
||||
+14
-3
@@ -1,8 +1,8 @@
|
||||
package com.siyeh.igtest.confusing;
|
||||
package com.siyeh.igtest.numeric.cast_that_loses_precision;
|
||||
|
||||
public class CastThatLosesPrecisionInspection
|
||||
public class CastThatLosesPrecision
|
||||
{
|
||||
public CastThatLosesPrecisionInspection()
|
||||
public CastThatLosesPrecision()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -67,4 +67,15 @@ public class CastThatLosesPrecisionInspection
|
||||
System.out.println("f = " + f);
|
||||
}
|
||||
|
||||
|
||||
private long aLong = 2L;
|
||||
private double d = 1.0;
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = (int) (aLong ^ (aLong >>> 32));
|
||||
long temp = d != +0.0d ? (int) d : 0L;
|
||||
result = 31 * result + (int) (temp ^ temp >>> 32);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
<problem>
|
||||
<file>CastThatLosesPrecision.java</file>
|
||||
<line>18</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Numeric cast that loses precision</problem_class>
|
||||
<description>Cast to <code>int</code> from 'float' may result in loss of precision #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>CastThatLosesPrecision.java</file>
|
||||
<line>20</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Numeric cast that loses precision</problem_class>
|
||||
<description>Cast to <code>char</code> from 'double' may result in loss of precision #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>CastThatLosesPrecision.java</file>
|
||||
<line>22</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Numeric cast that loses precision</problem_class>
|
||||
<description>Cast to <code>int</code> from 'double' may result in loss of precision #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>CastThatLosesPrecision.java</file>
|
||||
<line>24</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Numeric cast that loses precision</problem_class>
|
||||
<description>Cast to <code>int</code> from 'long' may result in loss of precision #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>CastThatLosesPrecision.java</file>
|
||||
<line>26</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Numeric cast that loses precision</problem_class>
|
||||
<description>Cast to <code>byte</code> from 'long' may result in loss of precision #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>CastThatLosesPrecision.java</file>
|
||||
<line>29</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Numeric cast that loses precision</problem_class>
|
||||
<description>Cast to <code>long</code> from 'double' may result in loss of precision #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>CastThatLosesPrecision.java</file>
|
||||
<line>31</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Numeric cast that loses precision</problem_class>
|
||||
<description>Cast to <code>long</code> from 'float' may result in loss of precision #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>CastThatLosesPrecision.java</file>
|
||||
<line>37</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Numeric cast that loses precision</problem_class>
|
||||
<description>Cast to <code>float</code> from 'double' may result in loss of precision #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>CastThatLosesPrecision.java</file>
|
||||
<line>55</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Numeric cast that loses precision</problem_class>
|
||||
<description>Cast to <code>byte</code> from 'long' may result in loss of precision #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>CastThatLosesPrecision.java</file>
|
||||
<line>77</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Numeric cast that loses precision</problem_class>
|
||||
<description>Cast to <code>int</code> from 'double' may result in loss of precision #loc</description>
|
||||
</problem>
|
||||
</problems>
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.siyeh.ig.numeric;
|
||||
|
||||
import com.siyeh.ig.IGInspectionTestCase;
|
||||
|
||||
public class CastThatLosesPrecisionInspectionTest extends IGInspectionTestCase {
|
||||
|
||||
public void test() throws Exception {
|
||||
doTest("com/siyeh/igtest/numeric/cast_that_loses_precision", new CastThatLosesPrecisionInspection());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user