Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-8567] Diagnostics not triggered for a where clause in a switch statement #51085

DevAndArtist mannequin opened this issue Aug 17, 2018 · 3 comments

[SR-8567] Diagnostics not triggered for a where clause in a switch statement #51085

DevAndArtist mannequin opened this issue Aug 17, 2018 · 3 comments


Copy link

DevAndArtist mannequin commented Aug 17, 2018

Previous ID SR-8567
Radar None
Original Reporter @DevAndArtist
Type Bug

Apple Swift version 4.1.2 (swiftlang-902.0.54 clang-902.0.39.2)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI
Assignee None
Priority Medium

md5: 55f6062fa98c2e5d82acfbaf8f17b250

Issue Description:

enum Permission {
  case notSupported(UInt8)
  case readOnly(UInt8)
  case readWrite

let permission = Permission.notSupported(0)

switch permission {
case .notSupported(let code),
     .readOnly(let code) where code > 0: // NO WARNIGNS
  /* create the right error value */
  /* create the default error value */

Expected result:

case .notSupported(let code), .readOnly(let code) where code > 0: // 'where' only applies to the second pattern match in this case
Copy link

belkadan commented Aug 17, 2018

I think this is correct behavior. The newline tells the compiler that you understand that it's only attached to that case. If you had actually wanted that behavior, how else would you silence the warning?

Copy link
Mannequin Author

DevAndArtist mannequin commented Aug 18, 2018

Hmm I haven't thought about silencing the warning if it's written in the multi-line approach. That's actually a fair point. Then I can assume this issue is not really a bug but my misunderstanding, you can close it.

Edit: How would you silence the warning in the single-line approach? Forcing the developer to wrap the lines after each comma is not ideal for everyone or every code-style considering there are developers that aim for small code height.

I think the issue is that there is no way to express a general `where` clause that would apply to any matched associated value instead of each individual one. That's what I pitched recently here. If that existed then the current diagnostic for the single-line case should probably be removed, no?

Copy link

belkadan commented Aug 20, 2018

The warning only shows up when you have a pattern with a where clause following a pattern without a where clause in the same case. The default diagnostic does suggest using multiple lines, but you could also reorder the patterns (maybe), or split them into two cases using fallthrough. Neither of those seems as tidy as a line break in most situations, though.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

1 participant