diff --git a/java/java-psi-api/src/com/intellij/psi/util/RedundantCastUtil.java b/java/java-psi-api/src/com/intellij/psi/util/RedundantCastUtil.java index f10b89fcb67f..ebff707ebacd 100644 --- a/java/java-psi-api/src/com/intellij/psi/util/RedundantCastUtil.java +++ b/java/java-psi-api/src/com/intellij/psi/util/RedundantCastUtil.java @@ -541,7 +541,10 @@ public class RedundantCastUtil { if (opposite == null || conditionalType instanceof PsiPrimitiveType && !Comparing.equal(conditionalType, opposite.getType())) return; } - } else if (parent instanceof PsiSynchronizedStatement && expr != null && expr.getType() instanceof PsiPrimitiveType) { + } + else if (parent instanceof PsiSynchronizedStatement && + expr != null && + (expr.getType() instanceof PsiPrimitiveType || expr instanceof PsiFunctionalExpression)) { return; } else if (expr instanceof PsiLambdaExpression || expr instanceof PsiMethodReferenceExpression) { if (parent instanceof PsiParenthesizedExpression && parent.getParent() instanceof PsiReferenceExpression) { diff --git a/java/java-tests/testData/inspection/redundantCast/lambda/MethodRefContext.java b/java/java-tests/testData/inspection/redundantCast/lambda/MethodRefContext.java index 3d7a4c5e98fd..e9b30032afb6 100644 --- a/java/java-tests/testData/inspection/redundantCast/lambda/MethodRefContext.java +++ b/java/java-tests/testData/inspection/redundantCast/lambda/MethodRefContext.java @@ -7,6 +7,12 @@ class Test { return s.length(); } + void test() { + synchronized ((Bar)Test::length) { + System.out.println("Synchronizing on a method reference is a nice thing to try!"); + } + } + interface Bar { Integer m(String s); }