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

Rule Request: Favor Key Path Expressions as Functions When Using Map & Filter #3082

Open
72A12F4E opened this issue Feb 6, 2020 · 1 comment

Comments

@72A12F4E
Copy link

72A12F4E commented Feb 6, 2020

As of Swift 5.2 you can now use a key path expression as a function when calling map/filter/etc.

I am proposing a rule that would identify & flag classic function calls that could be expressed using the new key path style.

Good

users.map(\.email)
users.filter(\.isAdmin)

Bad

users.map { $0.email }
users.filter { $0.isAdmin }
users.filter { user in
    return user.isAdmin 
}

I am not sure if this should be opt-in or not, but I would suggest that this be enabled by default.

@72A12F4E 72A12F4E changed the title Favor Key Path Expressions as Functions Favor Key Path Expressions as Functions When Using Map & Filter Feb 6, 2020
@72A12F4E 72A12F4E changed the title Favor Key Path Expressions as Functions When Using Map & Filter Rule Request: Favor Key Path Expressions as Functions When Using Map & Filter Feb 6, 2020
marcelofabri added a commit that referenced this issue Nov 8, 2020
marcelofabri added a commit that referenced this issue Nov 10, 2020
Fixes #3082

# Conflicts:
#	SwiftLint.xcodeproj/project.pbxproj
marcelofabri added a commit that referenced this issue Nov 11, 2020
Fixes #3082

# Conflicts:
#	SwiftLint.xcodeproj/project.pbxproj
@ZevEisenberg
Copy link
Contributor

FYI, key paths can be quite slow currently, because they're not optimized. That might be good to add as a caveat in this rule's description. I just changed a bunch of { $0.foo } to (\.foo) in my app, only to revert the change when it choked while running tests. More info: https://bugs.swift.org/browse/SR-9323

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

Successfully merging a pull request may close this issue.

3 participants