[java-highlighting] IDEA-324327 IJ-CR-110223 Constant associated with switch block must be used with primitives or String

GitOrigin-RevId: fab5d57dc37c1694607686e268072473ec60a4de
This commit is contained in:
Mikhail Pyltsin
2023-07-10 12:52:26 +02:00
committed by intellij-monorepo-bot
parent fef71943cd
commit 21d97bbbb0
3 changed files with 7 additions and 7 deletions

View File

@@ -161,7 +161,7 @@ public class SwitchBlockHighlightingModel {
if (kind == null || requiredLevel != null && !myLevel.isAtLeast(requiredLevel)) {
boolean is7 = myLevel.isAtLeast(LanguageLevel.JDK_1_7);
String expected = JavaErrorBundle.message(is7 ? "valid.switch.17.selector.types" : "valid.switch.selector.types");
String expected = JavaErrorBundle.message(is7 ? "valid.switch.1_7.selector.types" : "valid.switch.selector.types");
HighlightInfo.Builder info =
createError(mySelector, JavaErrorBundle.message("incompatible.types", expected, JavaHighlightUtil.formatType(mySelectorType)));
registerFixesOnInvalidSelector(info);
@@ -706,9 +706,9 @@ public class SwitchBlockHighlightingModel {
if (kind == SelectorKind.INT || kind == SelectorKind.STRING) {
return;
}
String expected = JavaErrorBundle.message("valid.switch.21.constant.types");
HighlightInfo.Builder infoIncompatibleTypes =
createError(expr, JavaErrorBundle.message("unexpected.type", expected, JavaHighlightUtil.formatType(expr.getType())));
createError(expr, JavaErrorBundle.message("incompatible.label.types", JavaHighlightUtil.formatType(expr.getType()),
JavaHighlightUtil.formatType(mySelectorType)));
holder.add(infoIncompatibleTypes.create());
return;
}

View File

@@ -230,13 +230,13 @@ exception.already.caught.warn=Unreachable section: {1, choice, 0#exception|2#exc
not.a.statement=Not a statement
invalid.statement=Invalid statement
incompatible.types=Incompatible types. Found: ''{1}'', required: ''{0}''
incompatible.label.types=Incompatible types. Type of constant label: ''{0}'', switch selector type: ''{1}''
unexpected.type=Unexpected type. Found: ''{1}'', required: ''{0}''
incompatible.types.reason.ambiguous.method.reference=<br/>reason: method reference is ambiguous: both ''{0}'' and ''{1}'' match
incompatible.switch.null.type=''{0}'' cannot be converted to ''{1}''
inaccessible.type=''{0}'' is inaccessible here
valid.switch.selector.types=byte, char, short or int
valid.switch.17.selector.types=char, byte, short, int, Character, Byte, Short, Integer, String, or an enum
valid.switch.21.constant.types=char, byte, short, int, Character, Byte, Short, Integer, String
valid.switch.1_7.selector.types=char, byte, short, int, Character, Byte, Short, Integer, String, or an enum
switch.illegal.fall.through.from=Illegal fall-through from a pattern
switch.illegal.fall.through.to=Illegal fall-through to a pattern
invalid.case.label.combination.constants.and.patterns=Invalid case label combination: A case label must consist of either a list of case constants or a single case pattern

View File

@@ -1,14 +1,14 @@
public class ConstantAssignment {
void dd(Double i) {
switch (i) {
case <error descr="Unexpected type. Found: 'double', required: 'char, byte, short, int, Character, Byte, Short, Integer, String'">1.0</error> -> System.out.println(1);
case <error descr="Incompatible types. Type of constant label: 'double', switch selector type: 'java.lang.Double'">1.0</error> -> System.out.println(1);
default -> throw new IllegalStateException("Unexpected value: " + i);
}
}
void dd2(Long i) {
switch (i) {
case <error descr="Unexpected type. Found: 'long', required: 'char, byte, short, int, Character, Byte, Short, Integer, String'">1L</error>:
case <error descr="Incompatible types. Type of constant label: 'long', switch selector type: 'java.lang.Long'">1L</error>:
System.out.println(1);
default:
throw new IllegalStateException("Unexpected value: " + i);