diff --git a/java/java-analysis-impl/src/com/intellij/psi/util/RedundantCastUtil.java b/java/java-analysis-impl/src/com/intellij/psi/util/RedundantCastUtil.java index 77bb8a4cd74b..1e23019e2b20 100644 --- a/java/java-analysis-impl/src/com/intellij/psi/util/RedundantCastUtil.java +++ b/java/java-analysis-impl/src/com/intellij/psi/util/RedundantCastUtil.java @@ -1,4 +1,4 @@ -// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.psi.util; import com.intellij.codeInsight.AnnotationUtil; @@ -736,8 +736,10 @@ public final class RedundantCastUtil { visitSwitchBlockSelector(expression); PsiType expectedTypeByParent = PsiTypesUtil.getExpectedTypeByParent(expression); - for (PsiExpression resultExpression : PsiUtil.getSwitchResultExpressions(expression)) { - addIfNarrowing(resultExpression, expectedTypeByParent); + if (expectedTypeByParent != null) { + for (PsiExpression resultExpression : PsiUtil.getSwitchResultExpressions(expression)) { + addIfNarrowing(resultExpression, expectedTypeByParent); + } } super.visitSwitchExpression(expression); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/switchExpressions/RedundantCastInSwitchBranch.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/switchExpressions/RedundantCastInSwitchBranch.java index 00661f137128..ee0b834d33d5 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/switchExpressions/RedundantCastInSwitchBranch.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/switchExpressions/RedundantCastInSwitchBranch.java @@ -31,4 +31,12 @@ class RedundantCast { default -> new ArrayList<>(); }; } + + void castForFunctionalExpression(String s) { + (switch (s) { + case "a" -> (Runnable)() -> System.out.println("a"); + case "b" -> (Runnable)() -> System.out.println("b"); + default -> throw new IllegalArgumentException(); + }).run(); + } } \ No newline at end of file