Skip to content

Commit 92eee10

Browse files
committed
Optional arguments must be counted from the end, closes #10095
1 parent 9b7b7d6 commit 92eee10

File tree

2 files changed

+9
-20
lines changed

2 files changed

+9
-20
lines changed

lib/elixir/src/elixir_locals.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ format_error({function_conflict, {Receiver, {Name, Arity}}}) ->
118118
format_error({unused_args, {Name, Arity}}) ->
119119
io_lib:format("default values for the optional arguments in ~ts/~B are never used", [Name, Arity]);
120120

121-
format_error({unused_args, {Name, Arity}, 1}) ->
122-
io_lib:format("the default value for the first optional argument in ~ts/~B is never used", [Name, Arity]);
121+
format_error({unused_args, {Name, Arity}, Count}) when Arity - Count == 1 ->
122+
io_lib:format("the default value for the last optional argument in ~ts/~B is never used", [Name, Arity]);
123123

124124
format_error({unused_args, {Name, Arity}, Count}) ->
125-
io_lib:format("the default values for the first ~B optional arguments in ~ts/~B are never used", [Count, Name, Arity]);
125+
io_lib:format("the default values for the last ~B optional arguments in ~ts/~B are never used", [Arity - Count, Name, Arity]);
126126

127127
format_error({unused_def, {Name, Arity}, defp}) ->
128128
io_lib:format("function ~ts/~B is unused", [Name, Arity]);

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

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -563,17 +563,17 @@ defmodule Kernel.WarningTest do
563563
end
564564
""")
565565
end) =~
566-
"the default values for the first 2 optional arguments in b/3 are never used\n nofile:3"
566+
"the default value for the last optional argument in b/3 is never used\n nofile:3"
567567

568568
assert capture_err(fn ->
569569
Code.eval_string(~S"""
570570
defmodule Sample3 do
571-
def a, do: b(1)
572-
defp b(arg1 \\ 1, arg2 \\ 2, arg3 \\ 3), do: [arg1, arg2, arg3]
571+
def a, do: b(1, 2)
572+
defp b(arg1, arg2 \\ 2, arg3 \\ 3, arg4 \\ 4), do: [arg1, arg2, arg3, arg4]
573573
end
574574
""")
575575
end) =~
576-
"the default value for the first optional argument in b/3 is never used\n nofile:3"
576+
"the default values for the last 2 optional arguments in b/4 are never used"
577577

578578
assert capture_err(fn ->
579579
Code.eval_string(~S"""
@@ -587,27 +587,16 @@ defmodule Kernel.WarningTest do
587587
assert capture_err(fn ->
588588
Code.eval_string(~S"""
589589
defmodule Sample5 do
590-
def a, do: b(1, 2, 3)
591-
defp b(arg1 \\ 1, arg2 \\ 2, arg3 \\ 3)
592-
593-
defp b(arg1, arg2, arg3), do: [arg1, arg2, arg3]
594-
end
595-
""")
596-
end) =~ "default values for the optional arguments in b/3 are never used\n nofile:3"
597-
598-
assert capture_err(fn ->
599-
Code.eval_string(~S"""
600-
defmodule Sample6 do
601590
def a, do: b(1, 2)
602591
defp b(arg1 \\ 1, arg2 \\ 2, arg3 \\ 3)
603592
604593
defp b(arg1, arg2, arg3), do: [arg1, arg2, arg3]
605594
end
606595
""")
607596
end) =~
608-
"the default values for the first 2 optional arguments in b/3 are never used\n nofile:3"
597+
"the default value for the last optional argument in b/3 is never used\n nofile:3"
609598
after
610-
purge([Sample1, Sample2, Sample3, Sample4, Sample5, Sample6])
599+
purge([Sample1, Sample2, Sample3, Sample4, Sample5])
611600
end
612601

613602
test "unused import" do

0 commit comments

Comments
 (0)