Skip to content

Rule Request: Warn on assigning nil to IUO. #1954

@mortenbekditlevsen

Description

@mortenbekditlevsen

New Issue Checklist

Rule Request

  1. Implicitly Unwrapped Optionals are common to use for late initialization. For instance this is done by the IBOutlet properties generated by Interface Builder.
    When just using IUOs for doing late initialization, it is unusual to re-nil these properties.
    This means that doing so could often be a programmer error.
    For instance it is common to assign nil-values to UILabel text in 'prepareForReuse' in a UITableViewCell. It is a rather easy typo to nil the label instead of the text property of the label.
    This gives an error that will trigger once the cell is reused, since the IUO is now nilled.
    If it is possible to detect nil-assignments to IUOs, it seems like this sort of error could be prevented in many cases.
    As we have not been able to think of a valid reason to re-assign nil to an IUO, it sounds like it could be a reasonable default to have such assignments trigger a warning.

var a: Int!
var b: Int?
a = 1 // would not trigger the violation
a = nil // would trigger the violation
b = nil // would not trigger the violation

  1. No configuration required.
  2. We believe that it should be enabled by default. Unless someone can provide a reasonable programming pattern where assigning nil to an IUO after late initialization makes sense.

Metadata

Metadata

Assignees

No one assigned

    Labels

    rule-requestRequests for a new rules.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions