RedundantCastUtil: do not report false-positive in code like Object x = (long)intValue;

GitOrigin-RevId: c290213db44e67f5c8949da0b9dce58a7cb9323a
This commit is contained in:
Tagir Valeev
2019-04-23 13:19:17 +03:00
committed by intellij-monorepo-bot
parent e5a2358183
commit 3840db2736
2 changed files with 8 additions and 3 deletions
@@ -1,4 +1,4 @@
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.psi.util;
import com.intellij.codeInsight.AnnotationUtil;
@@ -16,6 +16,7 @@ import com.intellij.psi.tree.IElementType;
import com.intellij.util.ArrayUtil;
import com.intellij.util.Function;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ig.psiutils.ExpectedTypeUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -767,8 +768,9 @@ public class RedundantCastUtil {
if (castType instanceof PsiPrimitiveType) {
if (opType instanceof PsiPrimitiveType) {
PsiElement parent = PsiUtil.skipParenthesizedExprUp(typeCast.getParent());
if (parent instanceof PsiReturnStatement || parent instanceof PsiExpressionList || parent instanceof PsiVariable ||
parent instanceof PsiAssignmentExpression) {
if ((parent instanceof PsiReturnStatement || parent instanceof PsiExpressionList || parent instanceof PsiVariable ||
parent instanceof PsiAssignmentExpression) &&
castType.equals(ExpectedTypeUtils.findExpectedType(typeCast, false))) {
return !TypeConversionUtil.isSafeConversion(castType, opType); // let's suppose that casts losing precision are important
} else {
return !castType.equals(opType); // cast might be necessary (e.g. ((double)1)/5)
@@ -15,4 +15,7 @@ class A {
widen((int)c);
}
Object test(int x) {
return (long) x;
}
}