diff --git a/src/elvis_style.erl b/src/elvis_style.erl index efa6e197..c9a57181 100644 --- a/src/elvis_style.erl +++ b/src/elvis_style.erl @@ -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 #{...}#{...} diff --git a/test/examples/pass_no_nested_try_catch.erl b/test/examples/pass_no_nested_try_catch.erl new file mode 100644 index 00000000..d7804a03 --- /dev/null +++ b/test/examples/pass_no_nested_try_catch.erl @@ -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. diff --git a/test/style_SUITE.erl b/test/style_SUITE.erl index d6aa6126..45082004 100644 --- a/test/style_SUITE.erl +++ b/test/style_SUITE.erl @@ -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).