From 567369f83cfe6a773eb5a5b1cdc0a7c332b5582e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mil=C3=A1n=20B=C3=B3r?= Date: Fri, 29 Nov 2024 13:23:01 +0100 Subject: [PATCH] add ignore macros --- src/elvis_style.erl | 8 +++++--- test/examples/used_ignored_variable_in_macro.erl | 13 +++++++++++++ test/style_SUITE.erl | 5 ++++- 3 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 test/examples/used_ignored_variable_in_macro.erl diff --git a/src/elvis_style.erl b/src/elvis_style.erl index e52ba9f4..604912e7 100644 --- a/src/elvis_style.erl +++ b/src/elvis_style.erl @@ -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; @@ -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. 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 00000000..5fb98a7e --- /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 08be829d..a9fb9ea3 100644 --- a/test/style_SUITE.erl +++ b/test/style_SUITE.erl @@ -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,