Predict boolean type for 'when' expression of switch pattern

IDEA-274106

GitOrigin-RevId: 3f41806bc5eceea7efa2c10bc149f231b4d42262
This commit is contained in:
Andrey Cherkasov
2022-11-07 05:17:27 +04:00
committed by intellij-monorepo-bot
parent 2f7a5590b8
commit 4bbd7cf012
13 changed files with 137 additions and 1 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// 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.codeInsight;
import com.intellij.codeInsight.completion.CompletionMemory;
@@ -500,6 +500,24 @@ public final class ExpectedTypesProvider {
}
}
@Override
public void visitPatternGuard(@NotNull PsiPatternGuard guard) {
processGuard(guard);
}
@Override
public void visitGuardedPattern(@NotNull PsiGuardedPattern pattern) {
processGuard(pattern);
}
private void processGuard(@NotNull PsiCaseLabelElement guard) {
final PsiSwitchBlock switchBlock = PsiTreeUtil.getParentOfType(guard, PsiSwitchBlock.class);
if (switchBlock != null) {
final TailType caseTail = TailTypes.forSwitchLabel(switchBlock);
myResult.add(createInfoImpl(PsiType.BOOLEAN, ExpectedTypeInfo.TYPE_STRICTLY, PsiType.BOOLEAN, caseTail));
}
}
@Override
public void visitForeachStatement(@NotNull PsiForeachStatement statement) {
if (myExpr.equals(statement.getIteratedValue())) {

View File

@@ -0,0 +1,10 @@
// "Create local variable 'flag'" "true-preview"
class Foo {
void test(Object obj) {
boolean flag;
switch (obj) {
case String s && flag -> System.out.println(1);
default -> System.out.println(2);
}
}
}

View File

@@ -0,0 +1,10 @@
// "Create local variable 'flag'" "true-preview"
class Foo {
void test(Object obj) {
boolean flag;
switch (obj) {
case String s when flag -> System.out.println(1);
default -> System.out.println(2);
}
}
}

View File

@@ -0,0 +1,9 @@
// "Create local variable 'flag'" "true-preview"
class Foo {
void test(Object obj) {
switch (obj) {
case String s && flag<caret> -> System.out.println(1);
default -> System.out.println(2);
}
}
}

View File

@@ -0,0 +1,9 @@
// "Create local variable 'flag'" "true-preview"
class Foo {
void test(Object obj) {
switch (obj) {
case String s when flag<caret> -> System.out.println(1);
default -> System.out.println(2);
}
}
}

View File

@@ -0,0 +1,13 @@
// "Create method 'isEmpty' in 'Main'" "true-preview"
class Main {
void foo(Object obj) {
switch (obj) {
case String s && isEmpty(s) -> {}
default -> {}
}
}
private boolean isEmpty(String s) {
return false;
}
}

View File

@@ -0,0 +1,13 @@
// "Create method 'isEmpty' in 'Main'" "true-preview"
class Main {
void foo(Object obj) {
switch (obj) {
case String s when isEmpty(s) -> {}
default -> {}
}
}
private boolean isEmpty(String s) {
return false;
}
}

View File

@@ -0,0 +1,9 @@
// "Create method 'isEmpty' in 'Main'" "true-preview"
class Main {
void foo(Object obj) {
switch (obj) {
case String s && isEmpt<caret>y(s) -> {}
default -> {}
}
}
}

View File

@@ -0,0 +1,9 @@
// "Create method 'isEmpty' in 'Main'" "true-preview"
class Main {
void foo(Object obj) {
switch (obj) {
case String s when isEmpt<caret>y(s) -> {}
default -> {}
}
}
}

View File

@@ -0,0 +1,9 @@
// "Create parameter 'flag'" "true"
class Foo {
void test(Object obj, boolean flag) {
switch (obj) {
case String s && flag -> System.out.println(1);
default -> System.out.println(2);
}
}
}

View File

@@ -0,0 +1,9 @@
// "Create parameter 'flag'" "true"
class Foo {
void test(Object obj, boolean flag) {
switch (obj) {
case String s when flag -> System.out.println(1);
default -> System.out.println(2);
}
}
}

View File

@@ -0,0 +1,9 @@
// "Create parameter 'flag'" "true"
class Foo {
void test(Object obj) {
switch (obj) {
case String s && flag<caret> -> System.out.println(1);
default -> System.out.println(2);
}
}
}

View File

@@ -0,0 +1,9 @@
// "Create parameter 'flag'" "true"
class Foo {
void test(Object obj) {
switch (obj) {
case String s when flag<caret> -> System.out.println(1);
default -> System.out.println(2);
}
}
}