mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
ExpressionUtils#isVoidContext updated for Java 12 (also for expression lists in for update)
This commit is contained in:
+15
-4
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+19
-1
@@ -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();
|
||||
};
|
||||
}
|
||||
}"""
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user