no_nested_try_catch reports a warning on a try…catch block that's not really nested within another.
To Reproduce
Run Elvis on the following module:
-module(example).
-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 ->
{Kind, Error}
end.
You'll get the following warning:
# src/example.erl [FAIL]
- no_nested_try_catch (https://github.com/inaka/elvis_core/tree/main/doc_rules/elvis_style/no_nested_try_catch.md)
- Nested try...catch block starting at line 10.
Expected Behavior
No warnings, since the second try…catch is not really nested within the first one (i.e., it's not try … try … catch … end … catch … end, which is what the rule tries to prevent).
Additional Context
This equivalent version of the code works:
-module(example).
-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() ->
Result =
try
do:something()
catch
Kind:Error ->
{Kind, Error}
end,
case Result of
{ok, KeepGoing} ->
try
do:something_else("and", KeepGoing)
catch
_:_ -> {ignore, errors, here}
end;
{Kind, Error} ->
{Kind, Error}
end.
Bug Description
no_nested_try_catch
reports a warning on atry…catch
block that's not really nested within another.To Reproduce
Run Elvis on the following module:
You'll get the following warning:
Expected Behavior
No warnings, since the second
try…catch
is not really nested within the first one (i.e., it's nottry … try … catch … end … catch … end
, which is what the rule tries to prevent).Additional Context
This equivalent version of the code works: