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

Fixes #352 - Avoid reporting ignored variables as used when they are used inside a macro #376

Merged
merged 1 commit into from
Nov 29, 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
8 changes: 5 additions & 3 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1958,13 +1958,13 @@ is_ignored_var(Zipper) ->
var ->
Name = ktn_code:attr(name, Node),
[FirstChar | _] = atom_to_list(Name),
(FirstChar == $_) and (Name =/= '_') and not check_parent_match(Zipper);
(FirstChar == $_) and (Name =/= '_') and not check_parent_match_or_macro(Zipper);
_OtherType ->
false
end.

%% @private
check_parent_match(Zipper) ->
check_parent_match_or_macro(Zipper) ->
case zipper:up(Zipper) of
undefined ->
false;
Expand All @@ -1973,10 +1973,12 @@ check_parent_match(Zipper) ->
case ktn_code:type(Parent) of
match ->
zipper:down(ParentZipper) == Zipper;
macro ->
zipper:down(ParentZipper) == Zipper;
maybe_match ->
zipper:down(ParentZipper) == Zipper;
_ ->
check_parent_match(ParentZipper)
check_parent_match_or_macro(ParentZipper)
end
end.

Expand Down
13 changes: 13 additions & 0 deletions test/examples/used_ignored_variable_in_macro.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-module(used_ignored_variable_in_macro).

-include_lib("stdlib/include/assert.hrl").

-define(MYMACRO(X), fun(X) -> ok end).

-export([do/0]).

do() ->
List = [{a, b}, {c, d}],
{_Key, b} = lists:keyfind(a, 1, List),
?assertMatch({_K, _}, lists:keyfind(a, 1, List)),
?MYMACRO({_P1, _}).
5 changes: 4 additions & 1 deletion test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -631,14 +631,17 @@ verify_used_ignored_variable(Config) ->
Ext = proplists:get_value(test_file_ext, Config, "erl"),

Path = "fail_used_ignored_variable." ++ Ext,
Path2 = "used_ignored_variable_in_macro." ++ Ext,
_ = case Group of
beam_files ->
[#{line_num := _}, #{line_num := _}, #{line_num := _}, #{line_num := _}] =
elvis_core_apply_rule(Config, elvis_style, used_ignored_variable, #{}, Path);
erl_files ->
[#{line_num := 31}, #{line_num := 34}, #{line_num := 38}, #{line_num := 38}] =
elvis_core_apply_rule(Config, elvis_style, used_ignored_variable, #{}, Path)
elvis_core_apply_rule(Config, elvis_style, used_ignored_variable, #{}, Path),
[] = elvis_core_apply_rule(Config, elvis_style, used_ignored_variable, #{}, Path2)
end,

[] =
elvis_core_apply_rule(Config,
elvis_style,
Expand Down