Swiftlint: Prefer function as a parameter instead of simple trailing closure

Created on 5 Dec 2016  路  4Comments  路  Source: realm/SwiftLint

This is an exception to https://github.com/realm/SwiftLint/issues/54.

If the trailing closure is simple then prefer

result.map(stripTrailingWhitespace)

instead of

result.map { stripTrailingWhitespace($0) }

reference:
https://github.com/exercism/xswift/pull/209#discussion_r90971304

rule-request

Most helpful comment

I don't think this is directly related to #54, as it would just validate this behavior:

// triggers
result.map({ stripTrailingWhitespace($0) })

// ok
result.map { stripTrailingWhitespace($0) }

All 4 comments

I don't think this is directly related to #54, as it would just validate this behavior:

// triggers
result.map({ stripTrailingWhitespace($0) })

// ok
result.map { stripTrailingWhitespace($0) }

"Trailing closure syntax should be used whenever possible."?

I don't think the description totally matches what it'd be validated. Maybe @jpsim can confirm, but I feel that rule would just validate that if it's a closure and it's the last parameter, trailing syntax should be used. Function references would always be valid for that rule.

@marcelofabri's last comment is spot on 馃憣. I would like a rule that can recommend using result.map(stripTrailingWhitespace) over result.map { stripTrailingWhitespace($0) } but in practice, that's challenging to build because you need to detect whether or not doing so would be ambiguous (e.g. cannot resolve between function overloads by the same name).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

muzamilhassan1987 picture muzamilhassan1987  路  3Comments

larslockefeer picture larslockefeer  路  3Comments

mildm8nnered picture mildm8nnered  路  3Comments

ArthurMaroulier picture ArthurMaroulier  路  3Comments

castus picture castus  路  3Comments