Skip to content

Commit 6fd203b

Browse files
ggcampinhojosevalim
authored andcommitted
Warn if unary operators are followed by new lines (#6482)
1 parent 0a677fe commit 6fd203b

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

lib/elixir/src/elixir_parser.yrl

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,9 @@ capture_op_eol -> capture_op : '$1'.
378378
capture_op_eol -> capture_op eol : '$1'.
379379

380380
unary_op_eol -> unary_op : '$1'.
381-
unary_op_eol -> unary_op eol : '$1'.
381+
unary_op_eol -> unary_op eol : warn_unary_operator_eol('$1'), '$1'.
382382
unary_op_eol -> dual_op : '$1'.
383-
unary_op_eol -> dual_op eol : '$1'.
383+
unary_op_eol -> dual_op eol : warn_unary_operator_eol('$1'), '$1'.
384384

385385
match_op_eol -> match_op : '$1'.
386386
match_op_eol -> match_op eol : '$1'.
@@ -857,6 +857,25 @@ warn_empty_stab_clause({stab_op, {Line, _Begin, _End}, '->'}) ->
857857
"an expression is always required on the right side of ->. "
858858
"Please provide a value after ->").
859859

860+
warn_unary_operator_eol({dual_op, {Line, _Begin, _End}, Op}) ->
861+
elixir_errors:warn(Line, ?file(),
862+
io_lib:format(
863+
"unary operator ~ts followed by new line. "
864+
"This may happen when you try to use a binary operator over multiple lines. "
865+
"Please make sure that the operator and all arguments are on the same line",
866+
[Op]
867+
)
868+
);
869+
warn_unary_operator_eol({unary_op, {Line, _Begin, _End}, Op}) ->
870+
elixir_errors:warn(Line, ?file(),
871+
io_lib:format(
872+
"unary operator ~ts followed by new line. "
873+
"Please make sure the unary operator and the expression that follows "
874+
"the operator are on the same line",
875+
[Op]
876+
)
877+
).
878+
860879
warn_pipe({arrow_op, {Line, _Begin, _End}, Op}, {_, [_ | _], [_ | _]}) ->
861880
elixir_errors:warn(Line, ?file(),
862881
io_lib:format(

lib/elixir/test/elixir/kernel/warning_test.exs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,25 @@ defmodule Kernel.WarningTest do
8484
end) =~ message
8585
end
8686

87+
test "unary operator followed by new line" do
88+
assert capture_err(fn ->
89+
Code.eval_string """
90+
1
91+
+
92+
1
93+
"""
94+
end) =~ "unary operator + followed by new line"
95+
96+
assert capture_err(fn ->
97+
Code.eval_string """
98+
!
99+
1
100+
"""
101+
end) =~ "unary operator ! followed by new line"
102+
after
103+
purge Sample
104+
end
105+
87106
test "useless attr" do
88107
message = capture_err(fn ->
89108
Code.eval_string """

0 commit comments

Comments
 (0)