[java-inspections] IDEA-351562 False negative for integer division in floating point context when using var

GitOrigin-RevId: 064ddb6d50110ba83060fe8785935ca2b4107234
This commit is contained in:
Tagir Valeev
2024-04-15 14:15:12 +02:00
committed by intellij-monorepo-bot
parent f0add46c89
commit c41f8b8269
2 changed files with 10 additions and 3 deletions

View File

@@ -30,7 +30,6 @@ import com.siyeh.ig.psiutils.ExpectedTypeUtils;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Set;
@@ -58,7 +57,7 @@ public final class IntegerDivisionInFloatingPointContextInspection extends BaseI
}
@Override
protected @Nullable LocalQuickFix buildFix(Object... infos) {
protected @NotNull LocalQuickFix buildFix(Object... infos) {
String castTo = (String)infos[0];
return new IntegerDivisionInFloatingPointContextFix(castTo);
}
@@ -83,7 +82,10 @@ public final class IntegerDivisionInFloatingPointContextInspection extends BaseI
return;
}
final PsiExpression context = getContainingExpression(expression);
final PsiType contextType = ExpectedTypeUtils.findExpectedType(context, true);
PsiType contextType = ExpectedTypeUtils.findExpectedType(context, true);
if (contextType == null) {
contextType = context.getType();
}
String castTo;
if (PsiTypes.floatType().equals(contextType) || PsiTypes.doubleType().equals(contextType)) {
castTo = contextType.getCanonicalText();

View File

@@ -14,4 +14,9 @@ public class IntegerDivisionInFloatingPointContext {
if(<warning descr="'intValue/anotherIntValue': integer division in floating-point context">intValue/anotherIntValue</warning> > 0.1);
float f = <warning descr="'intValue / anotherIntValue': integer division in floating-point context">intValue / anotherIntValue</warning>;
}
void varUse() {
double a = 1.0 + <warning descr="'5 / 2': integer division in floating-point context">5 / 2</warning>;
var b = 1.0 + <warning descr="'5 / 2': integer division in floating-point context">5 / 2</warning>;
}
}