Misleading error message: `'case' label can only appear inside a 'switch' statement`

Since case case be used in an if statement, not to mention when defining an enum, the error message 'case' label can only appear inside a 'switch' statement seems incorrect to me and I'd like to file a JIRA ticket if no one has already. I'm not sure what the error message could changed to, though.

enum Enum {
  case aCase
  case anotherCase
}

let value = Enum.aCase

if case .aCase = value {
  print("a case")
} else {
  print("another case")
}

You can clearly see from the above example that the case keyword can appear outside a switch statement.

1 Like

The message is correct as written.

A label is something that ends with a colon (:). A case label can only appear inside a switch statement.

The message says nothing about where the case keyword can appear. Obviously, the keyword can be used elsewhere without switch, such as inside the declaration of an enum.

4 Likes

I'm curious about what code produces that error message.

2 Likes
Terms of Service

Privacy Policy

Cookie Policy