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 for `trailing_closure` rule with anonymous closure call #2159

Closed
proxi opened this Issue Apr 17, 2018 · 2 comments

Comments

Projects
None yet
3 participants
@proxi
Copy link

proxi commented Apr 17, 2018

New Issue Checklist

Bug Report

It is sometimes handy to use anonymous closure as code grouping, e.g.:

menu.addItem({
    let menuItem = NSMenuItem(title: "Blah!")
    return menuItem
}())

When linted with trailing_closure enable, this incorrectly triggers a warning.

Complete output when running SwiftLint, including the stack trace and command used
Bagombo:closure proxi$ swiftlint 
Loading configuration from '.swiftlint.yml'
Linting Swift files in current working directory
Linting 'Closure.swift' (1/1)
Closure.swift:2:5: warning: Trailing Closure Violation: Trailing closure syntax should be used whenever possible. (trailing_closure)
Done linting! Found 1 violation, 0 serious in 1 file.
Bagombo:closure proxi$ 

Environment

  • SwiftLint version (run swiftlint version to be sure)? 0.25.1
  • Installation method used (Homebrew, CocoaPods, building from source, etc)? CocoaPods
  • Paste your configuration file:
opt_in_rules:
    - trailing_closure
  • Are you using nested configurations?
    If so, paste their relative paths and respective contents. No
  • Which Xcode version are you using (check xcode-select -p)? 9.3
  • Do you have a sample that shows the issue? Run echo "[string here]" | swiftlint lint --no-cache --use-stdin --enable-all-rules
    to quickly test if your example is really demonstrating the issue. If your example is more
    complex, you can use swiftlint lint --path [file here] --no-cache --enable-all-rules.
// This triggers a violation:
func foo(_ bar: Int) {
    foo({ return 42 }())
}

@marcelofabri marcelofabri added the bug label Apr 17, 2018

@Pashcal

This comment has been minimized.

Copy link

Pashcal commented Oct 10, 2018

Also looks like a similar bug:
"trailing_closure" is triggering on Dictionary(grouping: list, by: { $0.group }).sorted { $0.key < $1.key }, but not with Dictionary(grouping: list) { $0.group }.sorted { $0.key < $1.key }.
For me, first options is more readable.

@marcelofabri

This comment has been minimized.

Copy link
Collaborator

marcelofabri commented Jan 19, 2019

@Pashcal for future reference: what you're describing is not bug: the rule checks all closures that can be closure.

If you believe the first one is more readable, please open an issue to track adding a configuration to treat it as a special case. Please try to specify the enhancement as a generic case. When should the rule ignore the fact that a closure can be a trailing closure?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment