Skip to content

Commit

Permalink
Clean up handling of boolean options
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorng committed Nov 14, 2024
1 parent 4c8f1c8 commit 96e9f68
Showing 1 changed file with 72 additions and 106 deletions.
178 changes: 72 additions & 106 deletions lib/stdlib/src/erl_lint.erl
Original file line number Diff line number Diff line change
Expand Up @@ -751,111 +751,9 @@ start() ->
start("nofile", []).

start(File, Opts) ->
Enabled0 =
[{unused_vars,
bool_option(warn_unused_vars, nowarn_unused_vars,
true, Opts)},
{underscore_match,
bool_option(warn_underscore_match, nowarn_underscore_match,
true, Opts)},
{export_all,
bool_option(warn_export_all, nowarn_export_all,
true, Opts)},
{export_vars,
bool_option(warn_export_vars, nowarn_export_vars,
false, Opts)},
{shadow_vars,
bool_option(warn_shadow_vars, nowarn_shadow_vars,
true, Opts)},
{unused_import,
bool_option(warn_unused_import, nowarn_unused_import,
false, Opts)},
{unused_function,
bool_option(warn_unused_function, nowarn_unused_function,
true, Opts)},
{unused_type,
bool_option(warn_unused_type, nowarn_unused_type,
true, Opts)},
{bif_clash,
bool_option(warn_bif_clash, nowarn_bif_clash,
true, Opts)},
{unused_record,
bool_option(warn_unused_record, nowarn_unused_record,
true, Opts)},
{deprecated_function,
bool_option(warn_deprecated_function, nowarn_deprecated_function,
true, Opts)},
{deprecated_type,
bool_option(warn_deprecated_type, nowarn_deprecated_type,
true, Opts)},
{deprecated_callback,
bool_option(warn_deprecated_callback, nowarn_deprecated_callback,
true, Opts)},
{obsolete_guard,
bool_option(warn_obsolete_guard, nowarn_obsolete_guard,
true, Opts)},
{untyped_record,
bool_option(warn_untyped_record, nowarn_untyped_record,
false, Opts)},
{missing_spec,
bool_option(warn_missing_spec, nowarn_missing_spec,
false, Opts)},
{missing_spec_documented,
bool_option(warn_missing_spec_documented, nowarn_missing_spec_documented,
false, Opts)},
{missing_spec_all,
bool_option(warn_missing_spec_all, nowarn_missing_spec_all,
false, Opts)},
{removed,
bool_option(warn_removed, nowarn_removed,
true, Opts)},
{nif_inline,
bool_option(warn_nif_inline, nowarn_nif_inline,
true, Opts)},
{keyword_warning,
bool_option(warn_keywords, nowarn_keywords,
false, Opts)},
{redefined_builtin_type,
bool_option(warn_redefined_builtin_type, nowarn_redefined_builtin_type,
true, Opts)},
{match_float_zero,
bool_option(warn_match_float_zero, nowarn_match_float_zero,
true, Opts)},
{update_literal,
bool_option(warn_update_literal, nowarn_update_literal,
true, Opts)},
%% Behaviour warnings.
{behaviours,
bool_option(warn_behaviours,
nowarn_behaviours,
true, Opts)},
{conflicting_behaviours,
bool_option(warn_conflicting_behaviours,
nowarn_conflicting_behaviours,
true, Opts)},
{undefined_behaviour_func,
bool_option(warn_undefined_behaviour_func,
nowarn_undefined_behaviour_func,
true, Opts)},
{undefined_behaviour,
bool_option(warn_undefined_behaviour,
nowarn_undefined_behaviour,
true, Opts)},
{undefined_behaviour_callbacks,
bool_option(warn_undefined_behaviour_callbacks,
nowarn_undefined_behaviour_callbacks,
true, Opts)},
{ill_defined_behaviour_callbacks,
bool_option(warn_ill_defined_behaviour_callbacks,
nowarn_ill_defined_behaviour_callbacks,
true, Opts)},
{ill_defined_optional_callbacks,
bool_option(warn_ill_defined_optional_callbacks,
nowarn_ill_defined_optional_callbacks,
true, Opts)}
],
Enabled1 = [Category || {Category,true} <- Enabled0],
Enabled = ordsets:from_list(Enabled1),
Enabled0 = [Category || {Category,true} <- bool_options()],
Enabled1 = ordsets:from_list(Enabled0),
Enabled = parse_options(Opts, Enabled1),
Calls = case ordsets:is_element(unused_function, Enabled) of
true ->
#{{module_info,1} => pseudolocals()};
Expand All @@ -876,6 +774,74 @@ start(File, Opts) ->
file = File
}.

parse_options([Opt0|Opts], Enabled0) when is_atom(Opt0) ->
{Opt2,Enable} = case atom_to_binary(Opt0) of
<<"warn_",Opt1/binary>> ->
{Opt1,true};
<<"nowarn_",Opt1/binary>> ->
{Opt1,false};
_ ->
{none,none}
end,
Opt = try
binary_to_existing_atom(Opt2)
catch
_:_ ->
[]
end,
Enabled =
maybe
true ?= is_atom(Opt),
true ?= lists:keymember(Opt, 1, bool_options()),
if
Enable ->
ordsets:add_element(Opt, Enabled0);
not Enable ->
ordsets:del_element(Opt, Enabled0)
end
else
_ ->
Enabled0
end,
parse_options(Opts, Enabled);
parse_options([_|Opts], Enabled) ->
parse_options(Opts, Enabled);
parse_options([], Enabled) ->
Enabled.

bool_options() ->
[{unused_vars,true},
{underscore_match,true},
{export_all,true},
{export_vars,false},
{shadow_vars,true},
{unused_import,false},
{unused_function,true},
{unused_type,true},
{bif_clash,true},
{unused_record,true},
{deprecated_function,true},
{deprecated_type,true},
{deprecated_callback,true},
{obsolete_guard,true},
{untyped_record,false},
{missing_spec,false},
{missing_spec_documented,false},
{missing_spec_all,false},
{removed,true},
{nif_inline,true},
{keywords,false},
{redefined_builtin_type,true},
{match_float_zero,true},
{update_literal,true},
{behaviours,true},
{conflicting_behaviours,true},
{undefined_behaviour_func,true},
{undefined_behaviour,true},
{undefined_behaviour_callbacks,true},
{ill_defined_behaviour_callbacks,true},
{ill_defined_optional_callbacks,true}].

%% is_warn_enabled(Category, St) -> boolean().
%% Check whether a warning of category Category is enabled.
is_warn_enabled(Type, #lint{enabled_warnings=Enabled}) ->
Expand Down Expand Up @@ -4707,7 +4673,7 @@ feature_keywords() ->
%% Add warning for atoms that will be reserved keywords in the future.
%% (Currently, no such keywords to warn for.)
keyword_warning(Anno, Atom, St) ->
case is_warn_enabled(keyword_warning, St) of
case is_warn_enabled(keywords, St) of
true ->
case erl_anno:text(Anno) of
[$'| _] ->
Expand Down

0 comments on commit 96e9f68

Please sign in to comment.