ManualArrayCopyInspection: prevent changing semantics : IDEA-52481

This commit is contained in:
Roman.Ivanov
2018-05-04 15:32:05 +07:00
parent 5269cb606d
commit c25786fabf
@@ -16,6 +16,7 @@
package com.siyeh.ig.performance;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.dataFlow.DfaUtil;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
@@ -84,7 +85,13 @@ public class ManualArrayCopyInspection extends BaseInspection {
if (newExpression == null) {
return;
}
PsiReplacementUtil.replaceStatement(forStatement, newExpression, commentTracker);
PsiIfStatement ifStatement = (PsiIfStatement)commentTracker.replaceAndRestoreComments(forStatement, newExpression);
if (Boolean.TRUE.equals(DfaUtil.evaluateCondition(ifStatement.getCondition()))) {
PsiStatement nakedSubListClear = ControlFlowUtils.stripBraces(ifStatement.getThenBranch());
assert nakedSubListClear != null;
CommentTracker ct = new CommentTracker();
ct.replaceAndRestoreComments(ifStatement, ct.markUnchanged(nakedSubListClear));
}
}
@Nullable
@@ -161,7 +168,7 @@ public class ManualArrayCopyInspection extends BaseInspection {
final String toOffsetText = buildOffsetText(strippedLhsIndexExpression, variable,
limitExpression, decrement && (JavaTokenType.LT.equals(tokenType) || JavaTokenType.GT.equals(tokenType)),
commentTracker);
return "System.arraycopy(" + fromArrayText + ", " + fromOffsetText + ", " + toArrayText + ", " + toOffsetText + ", " + lengthText + ");";
return "if(" + lengthText + ">=0)" + "System.arraycopy(" + fromArrayText + ", " + fromOffsetText + ", " + toArrayText + ", " + toOffsetText + ", " + lengthText + ");";
}
@Nullable