Skip to content

Commit

Permalink
Ignore macros for case when the name provided in used macros place (#119
Browse files Browse the repository at this point in the history
)
  • Loading branch information
vkatsuba authored May 4, 2021
1 parent 742d28c commit ba648f7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ ebin/
_build
_checkouts
doc/
rebar3
7 changes: 7 additions & 0 deletions priv/test_files/unused_macros/used_macro_sample.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
-define(SIMPLE_MACRO, simple_macro).
-define(MACRO_USED_IN(MACRO), ??MACRO).
-define(MACRO_THAT_USES(OTHER_MACRO), {OTHER_MACRO, ?MACRO_USED_IN(?OTHER_MACRO)}).
-define(USED_MACROS_AS_FUNC, ?MODULE:simple_macros_func).

-define( USED_MACRO_WITH_BROKEN( CODE ) , case ?CODE of code -> ??CODE ) .

Expand All @@ -22,3 +23,9 @@ broken( ) -> ?USED_MACRO_WITH_BROKEN( SIMPLE_MACRO ) ; _ -> other end .

macro_is_an_atom() ->
{?macroIsAnAtom, ?macroIsAnAtom(?macroIsAnAtomToo())}.

simple_macros_call() ->
?USED_MACROS_AS_FUNC(<<"">>, <<"">>).

simple_macros_func(Data0, Data1) ->
io:format("~p~n", [{Data0, Data1}]).
7 changes: 6 additions & 1 deletion src/rules/unused_macros.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ do_analyze(File, AST) ->
UsedMacros = lists:map(fun macro_application_name/1, MacroUsage),
[result(File, MacroName, MacroArity, MacroLine)
|| {MacroName, MacroArity, MacroLine} <- DefinedMacros,
not lists:member({MacroName, MacroArity}, UsedMacros)].
not is_member({MacroName, MacroArity}, UsedMacros)].

macro_definition_name_and_line(Node) ->
{MacroName, MacroArity} = hank_utils:macro_definition_name(Node),
Expand Down Expand Up @@ -78,3 +78,8 @@ ignored({Name, _Arity}, Name) ->
true;
ignored(_Pattern, _IgnoreSpec) ->
false.

is_member({MacroName, none}, UsedMacros) ->
lists:keymember(MacroName, 1, UsedMacros);
is_member(Macro, UsedMacros) ->
lists:member(Macro, UsedMacros).

0 comments on commit ba648f7

Please sign in to comment.