Skip to content

Commit

Permalink
Fix #130: Avoid emitting a warning if an attribute is used only where…
Browse files Browse the repository at this point in the history
… it's defined (#131)
  • Loading branch information
Brujo Benavides authored May 21, 2021
1 parent 7402ee5 commit 40159f5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/rules/single_use_hrl_attrs.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ analyze(FilesAndASTs, _Context) ->
[build_macro_result(HrlFile, MacroKey, AttributesUsed)
|| {HrlFile, #{define := Defines}} <- HrlDefs,
MacroKey <- Defines,
is_used_only_once(MacroKey, AttributesUsed)]
is_used_only_once(HrlFile, MacroKey, AttributesUsed)]
++ [build_record_result(HrlFile, RecordKey, AttributesUsed)
|| {HrlFile, #{record := Records}} <- HrlDefs,
RecordKey <- Records,
is_used_only_once(RecordKey, AttributesUsed)].
is_used_only_once(HrlFile, RecordKey, AttributesUsed)].

build_macro_result(HrlFile, {Macro, Line}, AttributesUsed) ->
[File] = maps:get(Macro, AttributesUsed),
Expand All @@ -56,8 +56,15 @@ build_record_result(HrlFile, {Record, Line}, AttributesUsed) ->
text => hank_utils:format_text("#~tp is used only at ~ts", [Record, File]),
pattern => Record}.

is_used_only_once({Key, _Line}, AttributesUsed) ->
length(maps:get(Key, AttributesUsed, [])) == 1.
is_used_only_once(HrlFile, {Key, _Line}, AttributesUsed) ->
case maps:get(Key, AttributesUsed, []) of
[SingleFile] ->
%% There is nothing wrong with using an attribute only in the same
%% file where it's defined.
SingleFile /= HrlFile;
_ ->
false
end.

file_using({File, FileAST}, CurrentFiles) ->
AddFun = fun(Files) -> lists:usort([File | Files]) end,
Expand Down
3 changes: 2 additions & 1 deletion test/files/single_use_hrl_attrs/lib/app/include/header1.hrl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-define(APP_HEADER_1, "this is header from app that will be used in just one module").
-define(SOME_MACRO_1(A), A).
-define(SOME_DEFINE, ok).
-define(THIS_MACRO, {is_used, "and", it, shouldnt, generate, a, warning}).
-define(SOME_DEFINE, ?THIS_MACRO).

0 comments on commit 40159f5

Please sign in to comment.