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

False positive on unused_closure_parameter rule with self #2437

Closed
Dschee opened this Issue Oct 10, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@Dschee
Copy link
Collaborator

Dschee 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

This comment has been minimized.

Copy link
Collaborator

marcelofabri commented Oct 17, 2018

See #2006

@Dschee

This comment has been minimized.

Copy link
Collaborator Author

Dschee 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.

@Dschee Dschee changed the title False positive on unused_closure_paramter rule with self False positive on unused_closure_parameter rule with self Feb 12, 2019

@Dschee Dschee self-assigned this Feb 12, 2019

@Dschee Dschee closed this in #2639 Feb 14, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.