Skip to content

redundant_optional_initialization warns on wrapped nilable property leading to compile errors #3998

Closed

Description

@SimplyDanny

New Issue Checklist

Describe the bug

In the example

@propertyWrapper
struct Wrapper<T> {
    let name: String
    var wrappedValue: T

    init(wrappedValue value: T, _ name: String) {
        self.name = name
        self.wrappedValue = value
    }
}

struct Wrapped {
    @Wrapper("name")
    var flag: Bool? = nil // redundant_optional_initialization warns here
}

SwiftLint warns on the wrapped property assigned to nil. The property wrapper requires this explicit initialization, however. Removing the explicit assignment leads to a compile error.

Complete output when running SwiftLint, including the stack trace and command used
$ swiftlint lint main.swift
Linting Swift files at paths main.swift
Linting 'main.swift' (1/1)
main.swift:14:20: warning: Redundant Optional Initialization Violation: Initializing an optional variable with nil is redundant. (redundant_optional_initialization)
Done linting! Found 1 violation, 0 serious in 1 file.

Environment

  • SwiftLint 0.47.1
  • Homebrew
  • Default configuration
  • Xcode 13.4.1

Activity

  1. added
    bug
    on Jun 18, 2022
  2. added
    good first issue
    on Feb 11, 2024
  3. theamodhshetty commented on Feb 27, 2026

    @theamodhshetty
    Contributor

    I opened a PR for this: #6513

    Implemented a targeted fix via configuration instead of globally skipping annotated properties:

    • Added ignore_attributes to implicit_optional_initialization
    • When style: always, declarations with ignored attributes are skipped
    • Added generated rule examples/tests and changelog entry

    This keeps current defaults unchanged while allowing wrapper-specific opt-outs (e.g. wrappers that require explicit = nil).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions