Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Self-referencing KeyPath falsely triggers self_in_property_initialization #5835

Closed
2 tasks done
Deanosaurus opened this issue Oct 24, 2024 · 0 comments · Fixed by #5836
Closed
2 tasks done

Self-referencing KeyPath falsely triggers self_in_property_initialization #5835

Deanosaurus opened this issue Oct 24, 2024 · 0 comments · Fixed by #5836
Assignees
Labels
bug Unexpected and reproducible misbehavior.

Comments

@Deanosaurus
Copy link

New Issue Checklist

Bug Description

When using a key path within property initialization (either as a parameter in a function/initializer for another type or as the property itself) I believe SwiftLint is falsely identifying the key path as referencing a function on NSObject.

final class NotActuallyReferencingSelf {

    // This doesn't actually reference `self` but it shows an error "`self` refers to the unapplied `NSObject.self()`"
    let keyPath: Any = \String.self

    // This does *not* show an error.
    let someType: Any = String.self
}

The error mentions NSObject, but String is a struct and you can verify this with any type \Foo.self.

Command/Output

$ swiftlint --strict

/Path/to/the/file/NotActuallyReferencingSelf.swift:10:5: error: Self in Property Initialization Violation: self refers to the unapplied NSObject.self() method, which is likely not expected; make the variable lazy to be able to refer to the current instance or use ClassName.self (self_in_property_initialization)

In reference to this case I believe we are using "ClassName.self".

Environment

  • SwiftLint version: 0.57.0
  • Xcode version: 16.0.0
  • Installation method used: Homebrew
  • Configuration file: default

Are you using nested configurations? If so, paste their
relative paths and respective contents.

  • No nested config
@SimplyDanny SimplyDanny added the bug Unexpected and reproducible misbehavior. label Oct 24, 2024
@SimplyDanny SimplyDanny self-assigned this Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unexpected and reproducible misbehavior.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants