Skip to content

Commit

Permalink
Closes #351 - fix false positive on no_nested_try_catch (#369)
Browse files Browse the repository at this point in the history
* fix no_nested_try_catch

* Update test/examples/pass_no_nested_try_catch.erl

Co-authored-by: Brujo Benavides <[email protected]>

---------

Co-authored-by: Brujo Benavides <[email protected]>
  • Loading branch information
bormilan and elbrujohalcon authored Nov 1, 2024
1 parent df9fe91 commit 919fa6f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2191,13 +2191,15 @@ wildcard_match(X, Y) ->
%% @private
%% @doc No nested try...catch blocks
check_nested_try_catchs(ResultFun, TryExp) ->
Predicate = fun(Node) -> ktn_code:type(Node) == 'try' end,
lists:filtermap(fun (Node) when Node /= TryExp ->
{true, ResultFun(Node)};
(_) ->
false
lists:filtermap(fun(Node) ->
case ktn_code:type(Node) == 'try' of
true ->
{true, ResultFun(Node)};
false ->
false
end
end,
elvis_code:find(Predicate, TryExp)).
ktn_code:content(TryExp)).

%% @private
%% @doc No #{...}#{...}
Expand Down
20 changes: 20 additions & 0 deletions test/examples/pass_no_nested_try_catch.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-module(pass_no_nested_try_catch).

-export([example/0]).

%% @doc If this fails in do:something/0, we handle the error.
%% But if it fails in do:something_else/2, we ignore it.
example() ->
try do:something() of
{ok, KeepGoing} ->
try
do:something_else("and", KeepGoing)
catch
_:_ -> {ignore, errors, here}
end
catch
Kind:Error ->
try this:block(is, also, not_nested) catch _:_ -> {Kind, Error} end
after
try this:one(is, "not", nested, either) catch _:_ -> ok end
end.
6 changes: 5 additions & 1 deletion test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1343,7 +1343,11 @@ verify_no_nested_try_catch(Config) ->
elvis_style,
no_nested_try_catch,
#{ignore => [Module]},
Path).
Path),

Module2 = pass_no_nested_try_catch,
Path2 = atom_to_list(Module2) ++ "." ++ Ext,
[] = elvis_core_apply_rule(Config, elvis_style, no_nested_try_catch, #{}, Path2).

-spec verify_no_successive_maps(config()) -> any().
-if(?OTP_RELEASE < 27).
Expand Down

0 comments on commit 919fa6f

Please sign in to comment.