-
Notifications
You must be signed in to change notification settings - Fork 58
#461: New rule: simplify_anonymous_functions #511
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
67b899e
#461: New rule: simplify_anonymous_functions
elbrujohalcon 3ff02b6
Remove unneeded clarification
elbrujohalcon 575144e
Simplify code even further
elbrujohalcon c107c36
Update doc_rules/elvis_style/simplify_anonymous_functions.md
elbrujohalcon a8b7ac0
Update src/elvis_style.erl
elbrujohalcon 3c2df38
Fix md
elbrujohalcon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # Simplify Anonymous Functions [](https://github.com/inaka/elvis_core/releases/tag/4.2.0)  | ||
|
|
||
| Anonymous functions that simply call a named function (with the same arguments in the same order) | ||
| should be avoided; they can be more concisely expressed using the function reference syntax. | ||
|
|
||
| ## Avoid | ||
|
|
||
| ```erlang | ||
| fun(Pattern) -> is_match_node(Pattern) end | ||
| ``` | ||
|
|
||
| ## Prefer | ||
|
|
||
| ```erlang | ||
| fun is_match_node/1 | ||
| ``` | ||
|
|
||
| ## Rationale | ||
|
|
||
| The `fun F/A` syntax is clearer and more concise when the anonymous function does nothing more than | ||
| call another function. It reduces noise and makes the code easier to read and maintain by removing | ||
| unnecessary bindings and boilerplate. | ||
|
|
||
| ## Options | ||
|
|
||
| - None. | ||
|
|
||
| ## Example configuration | ||
|
|
||
| ```erlang | ||
| {elvis_style, simplify_anonymous_functions, #{}} | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| -module(fail_simplify_anonymous_functions). | ||
|
|
||
| -export([functions/0]). | ||
|
|
||
| functions() -> | ||
| #{ | ||
| no_args => fun() -> rand:uniform() end, | ||
| one_arg => fun(X) -> erlang:display(X) end, | ||
| two_args => fun(A, B) -> io:format(A, B) end, | ||
| local => fun() -> local() end, | ||
| auto_import => fun(A) -> atom_to_list(A) end | ||
| }. | ||
|
|
||
| local() -> local. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| -module(pass_simplify_anonymous_functions). | ||
|
|
||
| -export([functions/0]). | ||
|
|
||
| functions() -> | ||
| #{ | ||
| no_args => fun rand:uniform/0, | ||
| one_arg => fun erlang:display/1, | ||
| two_args => fun io:format/2, | ||
| local => fun local/0, | ||
| auto_import => fun atom_to_list/1, | ||
| flip_param_order => fun(A, B) -> io:format(B, A) end, | ||
| more_than_a_call => fun() -> second:call(first:call()) end, | ||
| op => fun(X) -> not X end, | ||
| bi_op => fun(A, B) -> A + B end, | ||
| id => fun(X) -> X end, | ||
| with_guards => fun(X) when is_binary(X) -> binary_to_list(X) end, | ||
| with_matching => fun(<<X/binary>>) -> erlang:binary_to_atom(X) end, | ||
| multiple_clauses => fun (x) -> atom_to_list(x); (X) -> binary_to_list(X) end, | ||
| ignored_arg => fun(X, _) -> erlang:display(X) end | ||
| }. | ||
|
|
||
| local() -> local. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.