diff --git a/src/elvis_style.erl b/src/elvis_style.erl index e9ad5ca..36f274e 100644 --- a/src/elvis_style.erl +++ b/src/elvis_style.erl @@ -2014,13 +2014,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; @@ -2029,10 +2029,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. diff --git a/test/examples/used_ignored_variable_in_macro.erl b/test/examples/used_ignored_variable_in_macro.erl new file mode 100644 index 0000000..5fb98a7 --- /dev/null +++ b/test/examples/used_ignored_variable_in_macro.erl @@ -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, _}). diff --git a/test/style_SUITE.erl b/test/style_SUITE.erl index c54551b..6a9c3e4 100644 --- a/test/style_SUITE.erl +++ b/test/style_SUITE.erl @@ -632,14 +632,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,