Skip to content
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

Closes #351 - fix false positive on no_nested_try_catch #369

Merged
merged 3 commits into from
Nov 1, 2024
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
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