swiftlang/swift

[SR-12421] Incorrect/unhelpful error when incorrectly calling return in failable initializer

Open

#54,860 opened on Mar 26, 2020

View on GitHub
 (7 comments) (0 reactions) (0 assignees)Swift (10,719 forks)batch import
buggood first issue

Repository metrics

Stars
 (69,989 stars)
PR merge metrics
 (Avg merge 8d 17h) (510 merged PRs in 30d)

Description

Previous ID SR-12421
Radar None
Original Reporter pilky (JIRA User)
Type Bug
Status In Progress
Resolution

Xcode 11.4 (11E146)

Votes 0
Component/s
Labels Bug, StarterBug
Assignee hassaneldesouky (JIRA)
Priority Medium

md5: 50d6e39b9624d565f57eea856086a4de

Issue Description:

If you create a failable initialiser and accidentally add a simple return rather than return nil when you want to fail, the Compiler gives an error at the start of the init about a property not being initialised.

Ideally this should detect that you have done a simple return rather than a return nil and show an error at that line, ideally with a fixit to add the nil back in

class TestClass {
    let label: String
    init?(label: String?) { //Gives "Constant 'self.label' used before being initialized"
        guard let actualLabel = label else {
            return //Actual error is a missing nil here
        }
        self.label = actualLabel
    }
}

Contributor guide