Skip to content

[SR-10118] Warn when OptionSet static constants are initialized with a zero value #52520

Closed
@beccadax

Description

@beccadax
Previous ID SR-10118
Radar rdar://problem/48689254
Original Reporter @beccadax
Type Improvement
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, DiagnosticsQoI, StarterBug
Assignee Vercantez (JIRA)
Priority Medium

md5: dd3960163b3287c2517c33c41718adda

Issue Description:

An OptionSet-conforming instance with a value of 0 behaves very strangely; for instance, contains(_:) will always return true for it. Because of this, and because [] is the preferred syntax for an empty option set, ClangImporter removes none members when it imports an NS_OPTIONS enum.

However, if you write an OptionSet-conforming type in Swift, there's nothing stopping you from adding one yourself:

struct MyOptions: OptionSet {
  var rawValue: Int
  static let none = MyOptions(rawValue: 0)
}

It would be nice if the compiler emitted a warning when you did this. The criteria would probably be something like:

  • A call to init(rawValue:)

  • Passing the literal 0

  • In an initial value expression (Initializer, in the compiler's parlance)

  • For a static let variable

  • In a type conforming to OptionSet

  • When the variable belongs to the type it's declared within

Metadata

Metadata

Assignees

No one assigned

    Labels

    OptionSetArea → standard library: The `OptionSet` protocolcompilerThe Swift compiler itselfdiagnostics QoIBug: Diagnostics Quality of Implementationgood first issueGood for newcomersimprovementmissing warningBug: Missing warningtype checkerArea → compiler: Semantic analysis

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions