ExpressionUtils#isVoidContext updated for Java 12 (also for expression lists in for update)

This commit is contained in:
Tagir Valeev
2018-12-06 17:57:34 +07:00
parent 12a86f56a1
commit e2f7b5986b
2 changed files with 34 additions and 5 deletions
@@ -1341,13 +1341,24 @@ public class ExpressionUtils {
/**
* Returns true if expression is evaluated in void context (i.e. its return value is not used)
* @param expression expression to check
* @return true if expression is evaluated in void context. More precisely if its parent is expression statement or lambda with void SAM
* @return true if expression is evaluated in void context.
*/
public static boolean isVoidContext(PsiExpression expression) {
PsiElement element = PsiUtil.skipParenthesizedExprUp(expression.getParent());
return element instanceof PsiExpressionStatement ||
(element instanceof PsiLambdaExpression &&
PsiType.VOID.equals(LambdaUtil.getFunctionalInterfaceReturnType((PsiLambdaExpression)element)));
if (element instanceof PsiExpressionStatement) {
if (element.getParent() instanceof PsiSwitchLabeledRuleStatement) {
PsiSwitchBlock block = ((PsiSwitchLabeledRuleStatement)element.getParent()).getEnclosingSwitchBlock();
return !(block instanceof PsiSwitchExpression);
}
return true;
}
if (element instanceof PsiExpressionList && element.getParent() instanceof PsiExpressionListStatement) {
return true;
}
if (element instanceof PsiLambdaExpression) {
if (PsiType.VOID.equals(LambdaUtil.getFunctionalInterfaceReturnType((PsiLambdaExpression)element))) return true;
}
return false;
}
/**
@@ -32,7 +32,7 @@ class IgnoreResultOfCallInspectionTest extends LightInspectionTestCase {
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return JAVA_8
return JAVA_12
}
@Override
@@ -344,6 +344,24 @@ public static int atLeast(int min, int actual, String varName) {
return new byte[length];
}
}"""
}
void testInForExpressionList() {
doTest """class X {
void test(String s) {
for(int i=0; i<10; i++, s./*Result of 'String.trim()' is ignored*/trim/**/()) {}
}
}"""
}
void testInSwitchExpression() {
doTest """class X {
String test(String s) {
return switch(s) {
default -> s.trim();
};
}
}"""
}
}