Skip to content

Commit a256c55

Browse files
wojtekmachericmj
authored andcommitted
Add option :all to :no_warn_undefined (#9691)
1 parent ec65f8d commit a256c55

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

lib/elixir/lib/code.ex

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,8 +1018,8 @@ defmodule Code do
10181018
10191019
* `:no_warn_undefined` (since v1.10.0) - list of modules and `{Mod, fun, arity}`
10201020
tuples that will not emit warnings that the module or function does not exist
1021-
at compilation time. This can be useful when doing dynamic compilation.
1022-
Defaults to `[]`.
1021+
at compilation time. Pass atom `:all` to skip warning for all undefined
1022+
functions. This can be useful when doing dynamic compilation. Defaults to `[]`.
10231023
10241024
* `:tracers` (since v1.10.0) - a list of tracers (modules) to be used during
10251025
compilation. See the module docs for more information. Defaults to `[]`.
@@ -1049,6 +1049,16 @@ defmodule Code do
10491049
:ok
10501050
end
10511051

1052+
def put_compiler_option(:no_warn_undefined, value) do
1053+
if value != :all and not is_list(value) do
1054+
raise "compiler option :no_warn_undefined should be a list or the atom :all, " <>
1055+
"got: #{inspect(value)}"
1056+
end
1057+
1058+
:elixir_config.put(:no_warn_undefined, value)
1059+
:ok
1060+
end
1061+
10521062
def put_compiler_option(key, value) when key in @list_compiler_options do
10531063
if not is_list(value) do
10541064
raise "compiler option #{inspect(key)} should be a list, got: #{inspect(value)}"

lib/elixir/lib/module/checker.ex

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,11 @@ defmodule Module.Checker do
7575
end
7676

7777
defp undefined_and_deprecation_warnings(map, cache) do
78-
no_warn_undefined = map.no_warn_undefined ++ Code.get_compiler_option(:no_warn_undefined)
79-
8078
state = %{
8179
cache: cache,
8280
file: map.file,
8381
module: map.module,
84-
no_warn_undefined: no_warn_undefined,
82+
no_warn_undefined: merge_no_warn_undefined(map),
8583
function: nil,
8684
warnings: []
8785
}
@@ -93,6 +91,16 @@ defmodule Module.Checker do
9391
|> sort_warnings()
9492
end
9593

94+
defp merge_no_warn_undefined(map) do
95+
case Code.get_compiler_option(:no_warn_undefined) do
96+
:all ->
97+
:all
98+
99+
list when is_list(list) ->
100+
map.no_warn_undefined ++ list
101+
end
102+
end
103+
96104
defp check_definitions(definitions, state) do
97105
Enum.reduce(definitions, state, &check_definition/2)
98106
end
@@ -211,6 +219,10 @@ defmodule Module.Checker do
211219
defp warn_undefined?(:erlang, :orelse, 2, _state), do: false
212220
defp warn_undefined?(:erlang, :andalso, 2, _state), do: false
213221

222+
defp warn_undefined?(_, _, _, %{no_warn_undefined: :all}) do
223+
false
224+
end
225+
214226
defp warn_undefined?(module, fun, arity, state) do
215227
not Enum.any?(state.no_warn_undefined, &(&1 == module or &1 == {module, fun, arity}))
216228
end

lib/elixir/test/elixir/module/checker_test.exs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,49 @@ defmodule Module.CheckerTest do
535535
Code.compiler_options(no_warn_undefined: no_warn_undefined)
536536
end
537537
end
538+
539+
test "global no_warn_undefined :all" do
540+
no_warn_undefined = Code.get_compiler_option(:no_warn_undefined)
541+
542+
try do
543+
Code.compiler_options(no_warn_undefined: :all)
544+
545+
files = %{
546+
"a.ex" => """
547+
defmodule A do
548+
def a, do: MissingModule.func(1)
549+
end
550+
"""
551+
}
552+
553+
assert_no_warnings(files)
554+
after
555+
Code.compiler_options(no_warn_undefined: no_warn_undefined)
556+
end
557+
end
558+
559+
test "global no_warn_undefined :all and local exclude" do
560+
no_warn_undefined = Code.get_compiler_option(:no_warn_undefined)
561+
562+
try do
563+
Code.compiler_options(no_warn_undefined: :all)
564+
565+
files = %{
566+
"a.ex" => """
567+
defmodule A do
568+
@compile {:no_warn_undefined, MissingModule}
569+
570+
def a, do: MissingModule.func(1)
571+
def b, do: MissingModule2.func(1, 2)
572+
end
573+
"""
574+
}
575+
576+
assert_no_warnings(files)
577+
after
578+
Code.compiler_options(no_warn_undefined: no_warn_undefined)
579+
end
580+
end
538581
end
539582

540583
describe "deprecated" do

lib/mix/lib/mix/cli.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ defmodule Mix.CLI do
3737
file = System.get_env("MIX_EXS") || "mix.exs"
3838

3939
if File.regular?(file) do
40+
old_value = Code.get_compiler_option(:no_warn_undefined)
41+
Code.put_compiler_option(:no_warn_undefined, :all)
4042
Code.compile_file(file)
43+
Code.put_compiler_option(:no_warn_undefined, old_value)
4144
end
4245
end
4346

0 commit comments

Comments
 (0)