Uploaded image for project: 'Swift'
  1. Swift
  2. SR-12421

Incorrect/unhelpful error when incorrectly calling return in failable initializer

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: In Progress
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: None
    • Labels:
    • Environment:

      Xcode 11.4 (11E146)

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

        Attachments

          Activity

            People

            Assignee:
            hassaneldesouky Hassan ElDesouky
            Reporter:
            pilky Martin Pilkington
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated: