Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change(export_used_types): don't warn on behaviour callbacks #371

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) -> apply(B, behaviour_info, [callbacks]) end, BehaviourNames)).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A try…catch is needed here, just in case someone introduces a typo (-behaviour(gen_srvr).) or uses a behaviour that's defined outside of OTP (-behaviour(my_behaviour).).


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