Skip to content
New issue

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

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

Already on GitHub? # to your account

False positive on unused_closure_parameter rule with self #2437

Closed
Jeehut opened this issue Oct 10, 2018 · 2 comments
Closed

False positive on unused_closure_parameter rule with self #2437

Jeehut opened this issue Oct 10, 2018 · 2 comments
Assignees

Comments

@Jeehut
Copy link
Collaborator

Jeehut commented Oct 10, 2018

Using SwiftLint 0.27.0 I get the following false positive:

// setup view model bindings
viewModel?.profileImage.didSet(weak: self) { (self, profileImage) in // << `self` in a closure should be replaced with _
    if let profileImage = profileImage {
        self.profileImageView.image = profileImage
    }
    else {
        self.profileImageView.image = UIImage(named: "avatar_100pt")
    }
}

The underlying implementation looks like this:

import Foundation

/// A binding wrapper to track changes on constant properties.
class ObservableProperty<ValueType> {
    // MARK: - Stored Instance Properties
    private var didSetClosures: [(ValueType) -> Void] = []

    private(set) var value: ValueType {
        didSet {
            didSetClosures.forEach { $0(value) }
        }
    }

    // MARK: - Initializers
    init(_ value: ValueType) {
        self.value = value
    }

    // MARK: - Instance Methods
    /// Will be called after the wrapped value is changed.
    /// `$0` is the passed parameter (usually `self`), `$1` is the new value.
    func didSet<WeakObject: AnyObject>(weak object: WeakObject, _ closure: @escaping (WeakObject, ValueType) -> Void) {
        let weakClosure: (ValueType) -> Void = { [weak object] value in
            guard let object = object else { return }
            closure(object, value)
        }

        didSetClosures.append(weakClosure)
        weakClosure(value)
    }

    /// Use to change the value wrapped as a variable.
    func set(_ newValue: ValueType) {
        value = newValue
    }
}

This is an implementation of the idea also implemented in https://github.com/dreymonde/Delegated and should therefore be warning-free. My best guess is that there's some kind of exception logic going on in the rule logic for self references.

@realm-probot realm-probot bot added the O:User label Oct 10, 2018
@marcelofabri
Copy link
Collaborator

See #2006

@Jeehut
Copy link
Collaborator Author

Jeehut commented Oct 18, 2018

AFAIK the above usage of self isn't a compiler bug anymore, instead it's a feature implemented after this proposal was accepted by the Swift Community and was shipped with Swift 4.2.

The compiler-bug was even mentioned in the proposal here. Note that I'm not using any backticks.

@Jeehut Jeehut changed the title False positive on unused_closure_paramter rule with self False positive on unused_closure_parameter rule with self Feb 12, 2019
@Jeehut Jeehut self-assigned this Feb 12, 2019
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants