KT-1977 Wrong 'unused expression' in catch

#KT-1977 fixed
This commit is contained in:
Svetlana Isakova
2012-05-18 15:19:38 +04:00
parent ff2b190f2d
commit b49fa2ab82
2 changed files with 65 additions and 0 deletions
@@ -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);
}
@@ -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 {
<!UNUSED_EXPRESSION!>88<!>
Integer.parseInt(s)
22
}
catch (e: NumberFormatException) {
44
}
finally {
<!UNUSED_EXPRESSION!>22<!>
}
}
fun test2(s : String) : Int? {
return try {
<!UNUSED_EXPRESSION!>88<!>
Integer.parseInt(s)
22
} finally {
<!UNUSED_FUNCTION_LITERAL!>{
(x : Int) -> x
}<!>
}
}
//KT-2015 False "Expression is unused" warnings
fun foo() {
val <!UNUSED_VARIABLE!>i<!> : Int = try{
bar()
1
}
catch(e : Exception){
0
}
}
fun bar() {
}