swiftlang/swift

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

Open

#54,860 opened on 2020年3月26日

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
 (平均マージ 8d 17h) (30d で 510 merged PRs)

説明

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
    }
}

コントリビューターガイド