swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.61k stars 10.37k forks source link

Remove useless default in switch for ints expression when all cases are already satisfies #75614

Open MichaelAdalbert opened 3 months ago

MichaelAdalbert commented 3 months ago

Motivation

In Swift, when using ranges within a switch statement, an error is raised even if the ranges cover all possible cases. Consider the following example:

func foo(_ n: UInt32) -> String {
    switch n {
        case ...22: return "Lower"
        case 22...: return "Higher"
    }
}

To resolve this issue, a default case must be added:

func foo(_ n: UInt32) -> String {
    switch n {
        case ...22: return "Lower"
        case 22...: return "Higher"
        default: return ""
    }
}

However, this addition is redundant since all possible cases are already covered by the first two branches.

Proposed solution

The solution is as follows:

Alternatives considered

No response

Additional information

No response

GunjanRawat26 commented 2 months ago

Hey @AnthonyLatsis

I'm a Swift Mentorship Mentee looking for a good first bug to work on. Would it be okay if I start working on this issue? If you have any recommendations or could tag me in any other suitable issues as you're triaging new ones, that would be greatly appreciated! Also, I had a discussion with amritpan. Could you please guide me on whether this issue would require minor changes or if it would need any pitches?

AnthonyLatsis commented 2 months ago

@GunjanRawat26 Hi! I am not even sure we want to do this one. Definitely subject to discussion and not a minor change at that. For alternatives, I think that many of the good first issues labeled with https://github.com/swiftlang/swift/labels/diagnostics%20QoI are a great starting point. These are often more about collaborating, exercising your understanding of the language, and reflecting on your experience as a Swift user than coding.