Extract Function: Do not report "Moving out of scope" warning on nested declarations
This commit is contained in:
+8
-3
@@ -515,14 +515,19 @@ private fun ExtractionData.inferParametersInfo(
|
||||
return null
|
||||
}
|
||||
|
||||
private fun ExtractionData.checkDeclarationsMovingOutOfScope(controlFlow: ControlFlow): ErrorMessage? {
|
||||
private fun ExtractionData.checkDeclarationsMovingOutOfScope(
|
||||
enclosingDeclaration: JetDeclaration,
|
||||
controlFlow: ControlFlow
|
||||
): ErrorMessage? {
|
||||
val declarationsOutOfScope = HashSet<JetNamedDeclaration>()
|
||||
if (controlFlow is JumpBasedControlFlow) {
|
||||
controlFlow.elementToInsertAfterCall.accept(
|
||||
object: JetTreeVisitorVoid() {
|
||||
override fun visitSimpleNameExpression(expression: JetSimpleNameExpression) {
|
||||
val target = expression.getReference()?.resolve()
|
||||
if (target is JetNamedDeclaration && target.isInsideOf(originalElements)) {
|
||||
if (target is JetNamedDeclaration
|
||||
&& target.isInsideOf(originalElements)
|
||||
&& target.getParentByType(javaClass<JetDeclaration>(), true) == enclosingDeclaration) {
|
||||
declarationsOutOfScope.add(target)
|
||||
}
|
||||
}
|
||||
@@ -608,7 +613,7 @@ fun ExtractionData.performAnalysis(): AnalysisResult {
|
||||
)
|
||||
}
|
||||
|
||||
checkDeclarationsMovingOutOfScope(controlFlow)?.let { messages.add(it) }
|
||||
checkDeclarationsMovingOutOfScope(enclosingDeclaration!!, controlFlow)?.let { messages.add(it) }
|
||||
|
||||
val functionNameValidator =
|
||||
JetNameValidatorImpl(
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
class T(val n: Int)
|
||||
|
||||
// SIBLING:
|
||||
fun foo() {
|
||||
<selection>bar { t ->
|
||||
val k = 1
|
||||
t.n + k + 1
|
||||
}</selection>
|
||||
}
|
||||
|
||||
fun bar(f: (T) -> Int) {
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
class T(val n: Int)
|
||||
|
||||
// SIBLING:
|
||||
fun foo() {
|
||||
unit()
|
||||
}
|
||||
|
||||
fun unit() {
|
||||
bar { t ->
|
||||
val k = 1
|
||||
t.n + k + 1
|
||||
}
|
||||
}
|
||||
|
||||
fun bar(f: (T) -> Int) {
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
class T(val n: Int)
|
||||
|
||||
// SIBLING:
|
||||
fun foo() {
|
||||
<selection>if (true) {
|
||||
val k = 1
|
||||
T().n + k + 1
|
||||
}</selection>
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
class T(val n: Int)
|
||||
|
||||
// SIBLING:
|
||||
fun foo() {
|
||||
unit()
|
||||
}
|
||||
|
||||
fun unit() {
|
||||
if (true) {
|
||||
val k = 1
|
||||
T().n + k + 1
|
||||
}
|
||||
}
|
||||
+10
@@ -590,6 +590,16 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
|
||||
doExtractFunctionTest("idea/testData/refactoring/extractFunction/controlFlow/outputValues/singleOutputValueWithWhenElse.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("valuesUsedInLambdaOnly.kt")
|
||||
public void testValuesUsedInLambdaOnly() throws Exception {
|
||||
doExtractFunctionTest("idea/testData/refactoring/extractFunction/controlFlow/outputValues/valuesUsedInLambdaOnly.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("valuesUsedInNestedBlock.kt")
|
||||
public void testValuesUsedInNestedBlock() throws Exception {
|
||||
doExtractFunctionTest("idea/testData/refactoring/extractFunction/controlFlow/outputValues/valuesUsedInNestedBlock.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/refactoring/extractFunction/controlFlow/throws")
|
||||
|
||||
Reference in New Issue
Block a user