diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUtil.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUtil.java index f08411886d9..bf8d3b47c2a 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUtil.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUtil.java @@ -241,6 +241,17 @@ public class JetPsiUtil { return parent; } } + if (parent instanceof JetTryExpression) { + JetTryExpression tryExpression = (JetTryExpression) parent; + if (tryExpression.getTryBlock() == block) { + return parent; + } + for (JetCatchClause clause : tryExpression.getCatchClauses()) { + if (clause.getCatchBody() == block) { + return parent; + } + } + } return null; } @@ -257,6 +268,9 @@ public class JetPsiUtil { if (expression == null) { expression = getDirectParentOfTypeForBlock(block, JetFunctionLiteral.class); } + if (expression == null) { + expression = getDirectParentOfTypeForBlock(block, JetTryExpression.class); + } if (expression != null) { return isImplicitlyUsed(expression); } diff --git a/compiler/testData/diagnostics/tests/controlFlowAnalysis/kt1977.jet b/compiler/testData/diagnostics/tests/controlFlowAnalysis/kt1977.jet new file mode 100644 index 00000000000..843bd4cb5f2 --- /dev/null +++ b/compiler/testData/diagnostics/tests/controlFlowAnalysis/kt1977.jet @@ -0,0 +1,51 @@ +package kt1977 + +//KT-1977 Wrong 'unused expression' in catch +fun strToInt(s : String) : Int? = +try { + Integer.parseInt(s) +} catch(e : NumberFormatException) { + null +} + +//more tests +fun test1(s : String) : Int? { + return try { + 88 + Integer.parseInt(s) + 22 + } + catch (e: NumberFormatException) { + 44 + } + finally { + 22 + } +} + +fun test2(s : String) : Int? { + return try { + 88 + Integer.parseInt(s) + 22 + } finally { + { + (x : Int) -> x + } + } +} + + +//KT-2015 False "Expression is unused" warnings +fun foo() { + val i : Int = try{ + bar() + 1 + } + catch(e : Exception){ + 0 + } +} + +fun bar() { +} \ No newline at end of file