diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ExpressionUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ExpressionUtils.java index 24e85d6d103d..18b6b48a475e 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ExpressionUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ExpressionUtils.java @@ -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; } /** diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/IgnoreResultOfCallInspectionTest.groovy b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/IgnoreResultOfCallInspectionTest.groovy index 934bfc4fc16e..7f26ce81f48e 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/IgnoreResultOfCallInspectionTest.groovy +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/IgnoreResultOfCallInspectionTest.groovy @@ -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(); + }; + } }""" } } \ No newline at end of file