Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
14 changes: 14 additions & 0 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1743,8 +1743,22 @@ same_except_location_attr(LeftNode, RightNode) ->
ktn_code:type(LeftNode) =:= ktn_code:type(RightNode) andalso
maps:remove(location, maps:get(attrs, LeftNode)) =:=
maps:remove(location, maps:get(attrs, RightNode)) andalso
same_node_attrs_except_location(LeftNode, RightNode) andalso
same_except_location_attr(ktn_code:content(LeftNode), ktn_code:content(RightNode)).

same_node_attrs_except_location(#{node_attrs := LeftAttrs}, #{node_attrs := RightAttrs}) ->
maps:keys(LeftAttrs) =:= maps:keys(RightAttrs) andalso
lists:all(
fun(AttrKey) ->
same_except_location_attr(
maps:get(AttrKey, LeftAttrs), maps:get(AttrKey, RightAttrs)
)
end,
maps:keys(LeftAttrs)
);
same_node_attrs_except_location(LeftNode, RightNode) ->
not maps:is_key(node_attrs, LeftNode) andalso not maps:is_key(node_attrs, RightNode).

-spec has_include_ms_transform(ktn_code:tree_node()) -> boolean().
has_include_ms_transform(Root) ->
Fun = fun(Node) ->
Expand Down
8 changes: 7 additions & 1 deletion test/examples/fail_no_operation_on_same_value.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
-module(fail_no_operation_on_same_value).

-export([boolean_ops/1, meaningless_ops/1, without_variables/0]).
-export([boolean_ops/1, meaningless_ops/1, without_variables/0, operation_on_records/0]).

-record(a_record, {a_field}).

boolean_ops(A) ->
#{
Expand Down Expand Up @@ -31,3 +33,7 @@ without_variables() ->
{things, are} /= {things, are},
[literals] == [literals]
].

operation_on_records() ->
One = #a_record{a_field = 1},
One#a_record.a_field == One#a_record.a_field.
9 changes: 8 additions & 1 deletion test/examples/pass_no_operation_on_same_value.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
-module(pass_no_operation_on_same_value).

-export([boolean_ops/2, meaningless_ops/1, without_variables/0, other_operators/0]).
-export([boolean_ops/2, meaningless_ops/1, without_variables/0, other_operators/0, operation_on_records/0]).

-record(a_record, {a_field}).

boolean_ops(A, B) ->
#{
Expand Down Expand Up @@ -47,3 +49,8 @@ other_operators() ->
A * A,
[A] ++ [A]
].

operation_on_records() ->
One = #a_record{a_field = 1},
Two = #a_record{a_field = 2},
One#a_record.a_field < Two#a_record.a_field.
15 changes: 8 additions & 7 deletions test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1972,8 +1972,6 @@ verify_no_operation_on_same_value(Config) ->

FailPath = "fail_no_operation_on_same_value." ++ Ext,
[
#{line_num := 7},
#{line_num := 8},
#{line_num := 9},
#{line_num := 10},
#{line_num := 11},
Expand All @@ -1985,15 +1983,18 @@ verify_no_operation_on_same_value(Config) ->
#{line_num := 17},
#{line_num := 18},
#{line_num := 19},
#{line_num := 25},
#{line_num := 31},
#{line_num := 32}
#{line_num := 20},
#{line_num := 21},
#{line_num := 27},
#{line_num := 33},
#{line_num := 34},
#{line_num := 39}
] =
elvis_core_apply_rule(Config, elvis_style, no_operation_on_same_value, #{}, FailPath),

[
#{line_num := 25},
#{line_num := 26}
#{line_num := 27},
#{line_num := 28}
] =
elvis_core_apply_rule(
Config, elvis_style, no_operation_on_same_value, #{operations => ['--', '++']}, FailPath
Expand Down
Loading