Skip to content

Commit

Permalink
add ignore macros (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
bormilan authored Nov 29, 2024
1 parent a7a8267 commit e81a9d5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.

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 @@ -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,
Expand Down

0 comments on commit e81a9d5

Please sign in to comment.