Skip to content

Commit 709383f

Browse files
committed
Warn on trailing commas on calls, closes #11399
1 parent e9be638 commit 709383f

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

lib/elixir/src/elixir_parser.yrl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@ kw_base -> kw_eol container_expr : [{'$1', '$2'}].
552552
kw_base -> kw_base ',' kw_eol container_expr : [{'$3', '$4'} | '$1'].
553553

554554
kw_call -> kw_base : reverse('$1').
555+
kw_call -> kw_base ',' : warn_trailing_comma('$2'), reverse('$1').
555556
kw_call -> kw_base ',' matched_expr : maybe_bad_keyword_call_follow_up('$2', '$1', '$3').
556557

557558
kw_data -> kw_base : reverse('$1').
@@ -1166,6 +1167,12 @@ error_invalid_kw_identifier({_, Location, do}) ->
11661167
error_invalid_kw_identifier({_, Location, KW}) ->
11671168
return_error(Location, "syntax error before: ", "'" ++ atom_to_list(KW) ++ ":'").
11681169

1170+
%% TODO: Make this an error on v2.0
1171+
warn_trailing_comma({',', {Line, Column, _}}) ->
1172+
elixir_errors:erl_warn({Line, Column}, ?file(),
1173+
"trailing commas are not allowed inside function/macro call arguments"
1174+
).
1175+
11691176
%% TODO: Make this an error on v2.0
11701177
warn_empty_paren({_, {Line, Column, _}}) ->
11711178
elixir_errors:erl_warn({Line, Column}, ?file(),

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1901,6 +1901,11 @@ defmodule Kernel.WarningTest do
19011901
end
19021902
end
19031903

1904+
test "warnings on trailing comma on call" do
1905+
assert capture_err(fn -> Code.eval_string("Keyword.merge([], foo: 1,)") end) =~
1906+
"trailing commas are not allowed inside function/macro call arguments"
1907+
end
1908+
19041909
test "defstruct warns with duplicate keys" do
19051910
assert capture_err(fn ->
19061911
Code.eval_string("""

0 commit comments

Comments
 (0)