mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
ExplicitArrayFillingInspection: suggest to use Arrays.setAll in case when new expression is present in rhs (IDEA-218143)
GitOrigin-RevId: fbe431b55c1e23ae215aea7b54d4cc5e638c8d86
This commit is contained in:
committed by
intellij-monorepo-bot
parent
5eeab1ac48
commit
56ee814078
@@ -13,6 +13,7 @@ import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiTypesUtil;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import com.siyeh.ig.psiutils.*;
|
||||
@@ -54,8 +55,7 @@ public class ExplicitArrayFillingInspection extends AbstractBaseJavaLocalInspect
|
||||
if (!ExpressionUtils.isReferenceTo(index, loop.getCounter())) return;
|
||||
PsiExpression rValue = assignment.getRExpression();
|
||||
if (rValue == null) return;
|
||||
if (!VariableAccessUtils.collectUsedVariables(rValue).contains(loop.getCounter()) &&
|
||||
!SideEffectChecker.mayHaveSideEffects(rValue)) {
|
||||
if (!isChangedInLoop(loop, rValue)) {
|
||||
Object constValue = ExpressionUtils.computeConstantExpression(rValue);
|
||||
if (constValue != null && constValue.equals(PsiTypesUtil.getDefaultValue(assignment.getType()))) {
|
||||
holder.registerProblem(statement, getRange(statement, ProblemHighlightType.WARNING),
|
||||
@@ -72,6 +72,12 @@ public class ExplicitArrayFillingInspection extends AbstractBaseJavaLocalInspect
|
||||
registerProblem(statement, true);
|
||||
}
|
||||
|
||||
private boolean isChangedInLoop(@NotNull CountingLoop loop, @NotNull PsiExpression rValue) {
|
||||
return VariableAccessUtils.collectUsedVariables(rValue).contains(loop.getCounter()) ||
|
||||
SideEffectChecker.mayHaveSideEffects(rValue) ||
|
||||
PsiTreeUtil.findChildOfType(rValue, PsiNewExpression.class, false) != null;
|
||||
}
|
||||
|
||||
private void registerProblem(@NotNull PsiForStatement statement, boolean isSetAll) {
|
||||
String message = InspectionsBundle.message("inspection.explicit.array.filling.description", isSetAll ? "setAll" : "fill");
|
||||
ReplaceWithArraysCallFix fix = new ReplaceWithArraysCallFix(!isSetAll);
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Replace loop with 'Arrays.setAll()' method call" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
class Test {
|
||||
|
||||
void fill2DArray() {
|
||||
final double[][] arr = new double[2][];
|
||||
Arrays.setAll(arr, i -> new double[1]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Replace loop with 'Arrays.setAll()' method call" "true"
|
||||
|
||||
class Test {
|
||||
|
||||
void fill2DArray() {
|
||||
final double[][] arr = new double[2][];
|
||||
for (<caret>int i = 0; i < arr.length; i++) {
|
||||
arr[i] = new double[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user