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

unused_closure_parameter false positive on lazy var #1161

Closed
ignazioc opened this issue Jan 11, 2017 · 11 comments
Closed

unused_closure_parameter false positive on lazy var #1161

ignazioc opened this issue Jan 11, 2017 · 11 comments
Labels
bug Unexpected and reproducible misbehavior.

Comments

@ignazioc
Copy link

ignazioc commented Jan 11, 2017

When a closure is used in this way, swiftlint raise a warning about the variable lbl not being used.

    lazy var label: UILabel = { (lbl: UILabel) -> UILabel in
        lbl.backgroundColor = UIColor.red
        return lbl
    }(UILabel())
@jpsim jpsim added the bug Unexpected and reproducible misbehavior. label Jan 11, 2017
@jpsim
Copy link
Collaborator

jpsim commented Jan 11, 2017

Thanks for reporting this.

@ignazioc
Copy link
Author

ignazioc commented Jan 11, 2017 via email

@jpsim
Copy link
Collaborator

jpsim commented Jan 11, 2017

Some tips to debug this in SwiftLint yourself:

  1. Clone and update submodules.
  2. Build and run the swiftlint scheme from SwiftLint.xcworkspace, setting the scheme's "Working Directory" and arguments.
  3. Set a breakpoint in the rule you're trying to debug so you can step through an inspect the state as you step through to identify why the code you shared is triggering a violation.

image

image

image

@marcelofabri
Copy link
Collaborator

You can also add your example to the rule examples. Another useful thing might be updating the rule to conform to OptInRule so your breakpoints only will be triggered with the examples (and not all files from the integration tests)

@marcelofabri
Copy link
Collaborator

Another tip is using SourceKitten to get the information about your example so you can understand the problem better.

@ignazioc
Copy link
Author

Thanks for the suggestions. I was able to debug the issue and investigate a little bit using SourceKitten. Here what I've found.

This is the test input:

3.times { number in
	 number
}


{ ( lbl: UILabel) -> UILabel in
	lbl
}(obj)

SwiftLint raises a warning on the second closure

example.swift:7:5: warning: Unused Closure Parameter Violation: Unused parameter "lbl" in a closure should be replaced with _. (unused_closure_parameter)

Here the relevant output from SourceKitten:

"key.length" : 30,
"key.name" : "3.times",
"key.bodyoffset" : 10,
"key.kind" : "source.lang.swift.expr.call",
"key.offset" : 1,
"key.namelength" : 7

"key.length" : 43,
"key.name" : "{ ( lbl: UILabel) -> UILabel in\n\tlbl\n}",
"key.bodyoffset" : 73,
"key.kind" : "source.lang.swift.expr.call",
"key.offset" : 34,
"key.namelength" : 38

On the first example the closure's body can be found between key.bodyoffset and key.lenghth and SwiftLint code scans that part of the file to find any usage of the parameters.

Unfortunately on the second example the same indexes point outside of the definition of the closure, to the call ((obj)) part. Therefor SwifLint doesn't find any usage of the parameter and raise the (wrong) warning.

The first solution coming to my mind is to check if the key.name is actually a closure and in that case search there any usage of the parameter.
It doesn't look really clean so maybe some of you expert can give me an hint?

Thanks!

@marcelofabri
Copy link
Collaborator

I think it's hacky to try to use key.name because we don't have any AST information about its contents. What if the parameter is used in a string or comment?

IMHO, we should just detect this case (i.e. detect that key.name is a closure) and skip any validations for now.

@marcelofabri
Copy link
Collaborator

After all, this rule doesn't work with closure declarations as well (see #1082).

@ghost
Copy link

ghost commented Jan 16, 2017

Not to hijack this thread, but a number of the suggestions given should probably be added to the CONTRIBUTING.md file to help future contributors.

@ignazioc
Copy link
Author

ignazioc commented Jan 16, 2017 via email

@marcelofabri
Copy link
Collaborator

@ignazioc I've opened #1252 to ignore this case for now and we can deal with it later in #1082.

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

No branches or pull requests

3 participants