mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 06:05:01 +07:00
Stream API migration: add narrowing cast if necessary when replacing += with sum()
This commit is contained in:
@@ -18,6 +18,8 @@ package com.intellij.codeInspection.streamMigration;
|
||||
import com.intellij.codeInspection.streamMigration.StreamApiMigrationInspection.MapOp;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.TypeConversionUtil;
|
||||
import com.siyeh.ig.psiutils.ParenthesesUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
@@ -48,6 +50,11 @@ class ReplaceWithSumFix extends MigrateToStreamFix {
|
||||
if (!type.equals(PsiType.DOUBLE) && !type.equals(PsiType.LONG)) {
|
||||
type = PsiType.INT;
|
||||
}
|
||||
PsiType addendType = addend.getType();
|
||||
if(addendType != null && !TypeConversionUtil.isAssignable(type, addendType)) {
|
||||
addend = JavaPsiFacade.getElementFactory(project).createExpressionFromText(
|
||||
"(" + type.getCanonicalText() + ")" + ParenthesesUtils.getText(addend, ParenthesesUtils.MULTIPLICATIVE_PRECEDENCE), addend);
|
||||
}
|
||||
StringBuilder builder = generateStream(new MapOp(tb.getLastOperation(), addend, tb.getVariable(), type));
|
||||
builder.append(".sum()");
|
||||
return replaceWithNumericAddition(project, loopStatement, var, builder, type);
|
||||
|
||||
Reference in New Issue
Block a user