Skip to content

[SR-2022] Better fixit for type checking in switch statements #44631

@karwa

Description

@karwa
Previous ID SR-2022
Radar None
Original Reporter @karwa
Type Improvement
Status In Progress
Resolution
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, DiagnosticsQoI, Parser, StarterBug
Assignee None
Priority Medium

md5: 9b53adc1e41e34c46ad044ffe4bfbac9

Issue Description:

switch myVariable {
    case let intValue as? Int: print("Hello, I'm an int: \(intValue)")
    case let stringValue as? String: print("Hello, I'm a string: \(stringValue)")
    default: print("Some other value...")
}

Gives you an error on the casting lines saying "Invalid pattern". It would be better if it told you to make the casts non-conditional, I.e.:

switch myVariable {
    case let intValue *as Int*: print("Hello, I'm an int: \(intValue)")
    case let stringValue *as String*: print("Hello, I'm a string: \(stringValue)")
    default: print("Some other value...")
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions