Skip to content

Commit

Permalink
change(export_used_types): don't warn on behaviour callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
bormilan committed Nov 5, 2024
1 parent df9fe91 commit a18932c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1460,13 +1460,15 @@ always_shortcircuit(Config, Target, RuleConfig) ->
[elvis_result:item()].
export_used_types(Config, Target, RuleConfig) ->
TreeRootNode = get_root(Config, Target, RuleConfig),
IgnoredFunctions = get_behaviour_callbacks(TreeRootNode),
ExportedFunctions = elvis_code:exported_functions(TreeRootNode),
FilteredExportedFunctions = lists:subtract(ExportedFunctions, IgnoredFunctions),
ExportedTypes = elvis_code:exported_types(TreeRootNode),
SpecNodes =
elvis_code:find(fun is_spec_attribute/1, TreeRootNode, #{traverse => all, mode => node}),
ExportedSpecs =
lists:filter(fun(#{attrs := #{arity := Arity, name := Name}}) ->
lists:member({Name, Arity}, ExportedFunctions)
lists:member({Name, Arity}, FilteredExportedFunctions)
end,
SpecNodes),
UsedTypes =
Expand All @@ -1492,6 +1494,15 @@ export_used_types(Config, Target, RuleConfig) ->
end,
UnexportedUsedTypes).

get_behaviour_callbacks(Root) ->
IsBehaviour = fun(Node) -> ktn_code:type(Node) == behaviour end,
Behaviours = elvis_code:find(IsBehaviour, Root),
BehaviourNames = lists:map(fun(#{attrs := #{value := Behaviour}}) -> Behaviour end, Behaviours),

lists:append(
lists:map(fun(B) -> B:behaviour_info(callbacks) end, BehaviourNames
)).

get_type_of_type(#{type := type_attr,
node_attrs := #{type := #{attrs := #{name := TypeOfType}}}}) ->
TypeOfType;
Expand Down
17 changes: 17 additions & 0 deletions test/examples/pass_export_used_types2.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-module(pass_export_used_types2).

-behaviour(gen_server).

-record(state, {a = field}).

-type state() :: #state{}.

-export([init/1, handle_cast/2, handle_call/3]).

-spec init(_) -> {ok, state()}.
init(_) -> {ok, #state{}}.

handle_call(_, _, State) -> {State, ok, State}.

-spec handle_cast(_, state()) -> {noreply, state()}.
handle_cast(_, State) -> {noreply, State}.
6 changes: 4 additions & 2 deletions test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -753,8 +753,7 @@ verify_state_record_and_type_plus_export_used_types(Config) ->
elvis_core_apply_rule(Config, elvis_style, export_used_types, #{}, PathPassGenStateM),

PathFail = "fail_state_record_and_type_plus_export_used_types." ++ Ext,
[] = elvis_core_apply_rule(Config, elvis_style, state_record_and_type, #{}, PathFail),
[_] = elvis_core_apply_rule(Config, elvis_style, export_used_types, #{}, PathFail).
[] = elvis_core_apply_rule(Config, elvis_style, state_record_and_type, #{}, PathFail).

-spec verify_behaviour_spelling(config()) -> any().
verify_behaviour_spelling(Config) ->
Expand Down Expand Up @@ -1754,6 +1753,9 @@ verify_export_used_types(Config) ->
PathPass = "pass_export_used_types." ++ Ext,
[] = elvis_core_apply_rule(Config, elvis_style, export_used_types, #{}, PathPass),

PathPass2 = "pass_export_used_types2." ++ Ext,
[] = elvis_core_apply_rule(Config, elvis_style, export_used_types, #{}, PathPass2),

PathFail = "fail_export_used_types." ++ Ext,
[#{line_num := 3}] =
elvis_core_apply_rule(Config, elvis_style, export_used_types, #{}, PathFail).
Expand Down

0 comments on commit a18932c

Please sign in to comment.