-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Rule: unused_closure_parameter
does not respect parameters with backticks
#4588
Comments
I think the rule might be working correctly here and that you're strongly capturing To only weakly capture func weakify<Owner: AnyObject>(_ owner: Owner, closure: @escaping (Owner) -> Void) -> (() -> Void) {
{ [weak owner] in
if let owner {
closure(owner)
}
}
}
class Foo {
struct Sut {
var arg: (() -> Void)?
}
var sut = Sut()
func bar() {
var sut = Sut()
sut.arg = weakify(self) { `self` in
_ = `self`
}
}
} Or to use a different closure argument name to avoid the confusion: func weakify<Owner: AnyObject>(_ owner: Owner, closure: @escaping (Owner) -> Void) -> (() -> Void) {
{ [weak owner] in
if let owner {
closure(owner)
}
}
}
class Foo {
struct Sut {
var arg: (() -> Void)?
}
var sut = Sut()
func bar() {
var sut = Sut()
sut.arg = weakify(self) { this in
_ = this
}
}
} |
Yeah, you're right in the case of `self`, I gave a bad example, sorry. class Foo {
struct Sut {
var arg: (() -> Void)?
}
var sut = Sut()
func bar() {
var sut = Sut()
sut.arg = weakify(self) { `baz` in
_ = baz
}
}
} warning: Unused Closure Parameter Violation: Unused parameter "`baz`" in a closure should be replaced with _. (unused_closure_parameter) ps.: It is a really dumb example to use backticks for a non-reserved keyword, so I accept if you close the post because of this. 🙄 |
Because SwiftLint rules operate before typechecking and semantic analysis, there’s not enough information available to know if I’ll do some more research to better understand the behavior of Swift’s identifier shadowing with backticks to see if there’s something we can do to improve this rule without requiring a separate, more semantically-aware analyzer rule. |
ok, so some lightweight research tells me that we should be able to ignore backticks when checking usage: let foo = 0
let closure: (Int) -> Void = { `foo` in
print(foo)
}
closure(1) // prints '1' |
Fix in #4590 |
Thanks for the quick fix, have a nice day! |
New Issue Checklist
Describe the bug
In the newly released 0.50.0 version, the rule
unused_closure_parameter
detects parameter names between backticks as unused parameters, even if they are in use.This might be intended behavior but it was not an issue in (at least) 0.49.1.
Complete output when running SwiftLint, including the stack trace and command used
Environment
NO
xcodebuild -version
)?Xcode 14.1
Build version: 14B47b
The text was updated successfully, but these errors were encountered: