From c25786fabf822186bb71eab01902c2b0283a6c9a Mon Sep 17 00:00:00 2001 From: "Roman.Ivanov" Date: Fri, 4 May 2018 15:17:04 +0700 Subject: [PATCH] ManualArrayCopyInspection: prevent changing semantics : IDEA-52481 --- .../ig/performance/ManualArrayCopyInspection.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/ManualArrayCopyInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/ManualArrayCopyInspection.java index 7912cf8117b0..9c23174a9511 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/ManualArrayCopyInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/ManualArrayCopyInspection.java @@ -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