Skip to content

prefer_self_in_static_references error with static property initializers in extensions #3775

Closed

Description

@mflider

New Issue Checklist

(Might be related to this one: #3768 )

Describe the bug

Using prefer_self_in_static_references auto-fixes code into a non-compilable state.

Complete output when running SwiftLint, including the stack trace and command used
extension DateFormatter {
    /// Represents weekday as full name (e.g. Sunday, Monday, Tuesday, etc.)
    static let weekdayName: DateFormatter = {
        let result = DateFormatter()
        result.dateFormat = DateFormatter.dateFormat(fromTemplate: "EEEE", options: 0, locale: nil)
        return result
    }()
}

gets turned into:

extension DateFormatter {
    /// Represents weekday as full name (e.g. Sunday, Monday, Tuesday, etc.)
    static let weekdayName: DateFormatter = {
        let result = DateFormatter()
        result.dateFormat = Self.dateFormat(fromTemplate: "EEEE", options: 0, locale: nil)
        return result
    }()
}

…which causes the compilation error:
Covariant 'Self' type cannot be referenced from a stored property initializer

Environment

  • SwiftLint version (run swiftlint version to be sure)? 0.45.1
  • Installation method used (Homebrew, CocoaPods, building from source, etc)? Homebrew
  • Paste your configuration file:
opt_in_rules:
  - prefer_self_in_static_references                                                                                                            
  • Are you using nested configurations? No
  • Which Xcode version are you using (check xcodebuild -version)? GM
  • Do you have a sample that shows the issue? Run echo "[string here]" | swiftlint lint --no-cache --use-stdin --enable-all-rules
    to quickly test if your example is really demonstrating the issue. If your example is more
    complex, you can use swiftlint lint --path [file here] --no-cache --enable-all-rules.

(see above)

Activity

  1. added
    bug
    on Jan 21, 2022
  2. jpsim commented on Jan 21, 2022

    @jpsim
    Collaborator

    Thanks for reporting this issue. Can you please try to see if #3771 fixes this for you?

  3. jpsim commented on Jan 21, 2022

    @jpsim
    Collaborator

    Never mind, it was easy for me to check thanks to the complete sample you shared, #3771 does not resolve your case.

  4. jpsim commented on Jan 21, 2022

    @jpsim
    Collaborator

    FYI @SimplyDanny if you want to solve another class of false positives.

  5. SimplyDanny commented on Jan 21, 2022

    @SimplyDanny
    Collaborator

    Sure. I'll take a look. Sorry for all the issues with this new rule.

  6. jpsim commented on Jan 21, 2022

    @jpsim
    Collaborator

    No worries, I've learned a lot about the complexity of the Swift language over the years working on SwiftLint.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions