Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc_rules/elvis_style/no_catch_expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ risky_call() ->
end.
```

## Exceptions
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


`catch` expressions where the result is explicitly discarded are allowed.
When you want to evaluate an expression and discard all possible results of it, even the errors,
you can signal that intention by preceding the expression with `_ =`, like in the following example:

```erlang
ignore_results_and_errors() ->
_ = catch do_something(),
done.
```

## Rationale

The `catch` expression in Erlang catches all kinds of exceptions - including runtime errors,
Expand Down
21 changes: 17 additions & 4 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1793,19 +1793,32 @@ no_import(Rule, ElvisConfig) ->
].

no_catch_expressions(Rule, ElvisConfig) ->
{nodes, CatchExprNodes} = elvis_code:find(#{
{zippers, CatchExprZippers} = elvis_code:find(#{
of_types => ['catch'],
inside => elvis_code:root(Rule, ElvisConfig)
inside => elvis_code:root(Rule, ElvisConfig),
filtered_from => zipper,
filtered_by => fun(Zipper) -> not result_discarded(Zipper) end
}),

[
elvis_result:new_item(
"an unexpected 'catch' expression was found; prefer a 'try' expression",
#{node => CatchExprNode}
#{node => zipper:node(CatchExprZipper)}
)
|| CatchExprNode <- CatchExprNodes
|| CatchExprZipper <- lists:uniq(CatchExprZippers)
].

%% @doc is this node in a _ = ... expression, where the result is explicitly discarded?
result_discarded(Zipper) ->
Parent = zipper:node(zipper:up(Zipper)),
case ktn_code:type(Parent) of
match ->
[LeftSide | _] = ktn_code:content(Parent),
ktn_code:type(LeftSide) == var andalso ktn_code:attr(name, LeftSide) == '_';
_ ->
false
end.

no_single_clause_case(Rule, ElvisConfig) ->
{nodes, CaseExprs} = elvis_code:find(#{
of_types => ['case'],
Expand Down
7 changes: 5 additions & 2 deletions test/examples/fail_no_catch_expressions.erl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
-module(fail_no_catch_expressions).

-export([catchf/0, try_catch/0, mixem/0]).
-export([catchf/0, try_catch/0, mixem/0, discarded/0]).

-dialyzer({nowarn_function, [catchf/0, try_catch/0, mixem/0]}).
-dialyzer({nowarn_function, [catchf/0, try_catch/0, mixem/0, discarded/0]}).

catchf() ->
F = fun (a) -> "catch" end,
Expand All @@ -25,3 +25,6 @@ mixem() ->
catch _ ->
catch F(a)
end.

discarded() ->
_ = catch this:should(be, fine).
2 changes: 1 addition & 1 deletion test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2424,7 +2424,7 @@ verify_no_catch_expressions(Config) ->
_ =
case Group of
beam_files ->
[#{line_num := 10}, #{line_num := 21}, #{line_num := 21}] = lists:sort(R);
[_, _, _] = R;
erl_files ->
[#{line_num := 9}, #{line_num := 24}, #{line_num := 26}] = lists:sort(R)
end.
Expand Down
Loading