From 6212f4138a6a34a1ebc724f6a483544aa685de94 Mon Sep 17 00:00:00 2001 From: Pavel Dolgov Date: Wed, 7 Sep 2016 19:12:53 +0300 Subject: [PATCH] Java inspection: Don't look for assignment chains in nested code blocks in "Move return to computation" inspection (IDEA-121153) --- ...urnSeparatedFromComputationInspection.java | 8 +------ .../afterAssignmentChainUnderIf.java | 15 +++++++++++++ .../beforeAssignmentChainUnderIf.java | 22 +++++++++---------- 3 files changed, 27 insertions(+), 18 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation/afterAssignmentChainUnderIf.java diff --git a/java/java-impl/src/com/intellij/codeInspection/intermediaryVariable/ReturnSeparatedFromComputationInspection.java b/java/java-impl/src/com/intellij/codeInspection/intermediaryVariable/ReturnSeparatedFromComputationInspection.java index af3d772815e4..57bf1758de6d 100644 --- a/java/java-impl/src/com/intellij/codeInspection/intermediaryVariable/ReturnSeparatedFromComputationInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/intermediaryVariable/ReturnSeparatedFromComputationInspection.java @@ -383,8 +383,7 @@ public class ReturnSeparatedFromComputationInspection extends BaseJavaBatchLocal PsiJavaToken rBrace = codeBlock.getRBrace(); if (rBrace != null) { PsiStatement lastNonEmptyStatement = getPrevNonEmptyStatement(rBrace, removeCompletely); - if (lastNonEmptyStatement == null || - isIfBranch(codeBlock) && hasChainedAssignmentsInScope(flow, resultVariable, lastNonEmptyStatement)) { + if (lastNonEmptyStatement == null) { return false; } if (moveTo(lastNonEmptyStatement, returnAtTheEnd)) { @@ -508,11 +507,6 @@ public class ReturnSeparatedFromComputationInspection extends BaseJavaBatchLocal return ExpressionUtils.computeConstantExpression(condition) == Boolean.TRUE; } - private static boolean isIfBranch(@NotNull PsiCodeBlock codeBlock) { - final PsiElement parent = codeBlock.getParent(); - return parent instanceof PsiBlockStatement && parent.getParent() instanceof PsiIfStatement; - } - private Set getBreaks(@NotNull PsiStatement targetStatement) { if (breakStatements == null) { breakStatements = new THashMap<>(); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation/afterAssignmentChainUnderIf.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation/afterAssignmentChainUnderIf.java new file mode 100644 index 000000000000..2166acd42d90 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation/afterAssignmentChainUnderIf.java @@ -0,0 +1,15 @@ +// "Move 'return' closer to computation of the value of 'n'" "true" +class T { + int x; + int y; + + int f(int a) { + int n = -1; + if (a != 0) { + n = a; + n = 31 * x + n; + return 31 * y + n; + } + return n; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation/beforeAssignmentChainUnderIf.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation/beforeAssignmentChainUnderIf.java index c0ccfdbfa1c0..8ed6b4e96bc8 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation/beforeAssignmentChainUnderIf.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation/beforeAssignmentChainUnderIf.java @@ -1,15 +1,15 @@ -// "Move 'return' closer to computation of the value of 'n'" "false" +// "Move 'return' closer to computation of the value of 'n'" "true" class T { - int x; - int y; + int x; + int y; - int f(int a) { - int n = -1; - if (a != 0) { - n = a; - n = 31 * x + n; - n = 31 * y + n; + int f(int a) { + int n = -1; + if (a != 0) { + n = a; + n = 31 * x + n; + n = 31 * y + n; + } + return n; } - return n; - } } \ No newline at end of file