diff --git a/src/elvis_style.erl b/src/elvis_style.erl index 94d2758..acbd339 100644 --- a/src/elvis_style.erl +++ b/src/elvis_style.erl @@ -1124,56 +1124,48 @@ atom_naming_convention(Config, Target, RuleConfig) -> no_init_lists(Config, Target, RuleConfig) -> Root = get_root(Config, Target, RuleConfig), - InitFuns = + ListInitClauses = case is_relevant_behaviour(Root, RuleConfig) of true -> - IsFunction = fun(Node) -> ktn_code:type(Node) == function end, - FunctionNodes = elvis_code:find(IsFunction, Root), - ProcessFun = - fun(FunctionNode) -> - Location = ktn_code:attr(location, FunctionNode), - Content = ktn_code:content(FunctionNode), - lists:map(fun(Elem) -> - Attributes = ktn_code:node_attr(pattern, Elem), - {Location, Attributes} - end, - Content) + IsInit1Function = + fun(Node) -> + ktn_code:type(Node) == function + andalso ktn_code:attr(name, Node) == init + andalso ktn_code:attr(arity, Node) == 1 + end, + [Init1Fun] = elvis_code:find(IsInit1Function, Root), + + FilterClauses = + fun(Clause) -> + [Attribute] = ktn_code:node_attr(pattern, Clause), + case is_list_node(Attribute) of + true -> + {true, ktn_code:attr(location, Clause)}; + false -> + false + end end, - lists:append( - lists:filtermap(fun(FunctionNode) -> - Name = ktn_code:attr(name, FunctionNode), - case Name of - init -> - {true, ProcessFun(FunctionNode)}; - _ -> - false - end - end, - FunctionNodes)); + Content = ktn_code:content(Init1Fun), + ListAttrClauses = lists:filtermap(FilterClauses, Content), + case length(ListAttrClauses) =:= length(Content) of + true -> + ListAttrClauses; + false -> + [] + end; false -> [] end, - IsBreakRule = - lists:all(fun({_, Attributes}) -> - length(lists:filter(fun(Elem) -> is_list_node(Elem) end, Attributes)) =:= 1 - end, - InitFuns), - ResultFun = - fun({Location, _}) -> + fun(Location) -> Info = [Location], Msg = ?NO_INIT_LISTS_MSG, elvis_result:new(item, Msg, Info, Location) end, - case IsBreakRule of - true -> - lists:map(ResultFun, InitFuns); - false -> - [] - end. + lists:map(ResultFun, ListInitClauses). is_relevant_behaviour(Root, RuleConfig) -> ConfigBehaviors = option(behaviours, RuleConfig, no_init_lists), @@ -1188,6 +1180,8 @@ is_relevant_behaviour(Root, RuleConfig) -> is_list_node(#{type := cons}) -> true; +is_list_node(#{type := nil}) -> + true; is_list_node(#{type := match, content := Content}) -> lists:any(fun(Elem) -> is_list_node(Elem) end, Content); is_list_node(_) -> diff --git a/test/examples/no_init_lists_examples/pass_no_init_lists2.erl b/test/examples/no_init_lists_examples/pass_no_init_lists2.erl index c3c7b12..8ab684b 100644 --- a/test/examples/no_init_lists_examples/pass_no_init_lists2.erl +++ b/test/examples/no_init_lists_examples/pass_no_init_lists2.erl @@ -10,7 +10,7 @@ start_link() -> init(#{}) -> ok. -init([_], udnefined) -> +init([_], undefined) -> ok. handle_cast(_, _) -> ok. diff --git a/test/style_SUITE.erl b/test/style_SUITE.erl index a3856b3..bb2a1ad 100644 --- a/test/style_SUITE.erl +++ b/test/style_SUITE.erl @@ -1529,16 +1529,19 @@ verify_no_init_lists(Config) -> FailPath = ExamplesDir ++ "fail_no_init_lists." ++ Ext, FailPath2 = ExamplesDir ++ "fail_no_init_lists2." ++ Ext, FailPath3 = ExamplesDir ++ "fail_no_init_lists3." ++ Ext, - % FailPath4 = ExamplesDir ++ "fail_no_init_lists4." ++ Ext, + FailPath4 = ExamplesDir ++ "fail_no_init_lists4." ++ Ext, + [_] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, FailPath), [_] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, FailPath2), [_, _, _] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, FailPath3), - % [_] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, FailPath4), + [_] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, FailPath4), + PassPath = ExamplesDir ++ "pass_no_init_lists." ++ Ext, PassPath2 = ExamplesDir ++ "pass_no_init_lists2." ++ Ext, PassPath3 = ExamplesDir ++ "pass_no_init_lists3." ++ Ext, PassPath4 = ExamplesDir ++ "pass_no_init_lists4." ++ Ext, PassPath5 = ExamplesDir ++ "pass_no_init_lists5." ++ Ext, + [] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, PassPath), [] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, PassPath2), [] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, PassPath3),