Skip to content

[SR-4468] "Switch must be exhaustive" should suggest at least 1 incompletely handled case. #47045

@swift-ci

Description

@swift-ci
Previous ID SR-4468
Radar None
Original Reporter mattgallagher (JIRA User)
Type Improvement
Status Resolved
Resolution Done

Attachment: Download

Environment

Xcode 8.3, Swift 3.1

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, DiagnosticsQoI
Assignee @CodaFi
Priority Medium

md5: 952fe45970b7ad1ce8b237d8be29d7ba

Issue Description:

The error message "Switch must exhaustive, consider adding a default clause" should be improved by stating at least one case that is incompletely handled so the programmer can immediately do something to start fixing the problem.

Consider the following code in Swift 3.1, which will give the error "Switch must exhaustive, consider adding a default clause":

enum Something {
case a, b, c, d, r, e, f, g
}

let s: Something = someFunctionReturningSomething()
switch s {
case .a: print("a")
case .b: print("b")
case .c: print("c")
case .d: print("d")
case .e: print("e")
case .f: print("f")
case .g: print("g")
}

Upon seeing the error message, you can't immediately fix the problem. Instead, you need to slowly and painstakingly analyze the code, comparing cases in the enum to cases in the switch to find which case is accidentally omitted. This can be difficult in the presence of where clauses and can be very time consuming for enums with a large number of cases.

This is work that the compiler should be able to do (theoretically, it has already done the work to recognize the need for the error in the first place).

A more helpful message would be: "Switch must be exhaustive; cases, including 'r', are incompletely handled. Consider adding a default clause."

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions