Skip to content

Commit b20ddac

Browse files
committed
Make the compiler report 'and'/'or' operators as obsolete
1 parent 270cec6 commit b20ddac

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

lib/stdlib/src/erl_lint.erl

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,15 @@ format_error_1({redefine_bif_import,{F,A}}) ->
366366
import directive overrides auto-imported BIF ~w/~w --
367367
use "-compile({no_auto_import,[~w/~w]})." to resolve name clash
368368
""", [F,A,F,A]};
369-
format_error_1({deprecated, MFA, String, Rel}) ->
369+
format_error_1({deprecated, MFA, String, Rel}) when is_tuple(MFA) ->
370+
format_error_1({deprecated, format_mfa(MFA), String, Rel});
371+
format_error_1({deprecated, Thing, String, Rel}) when is_list(String) ->
370372
{~"~s is deprecated and will be removed in ~s; ~s",
371-
[format_mfa(MFA), Rel, String]};
372-
format_error_1({deprecated, MFA, String}) when is_list(String) ->
373-
{~"~s is deprecated; ~s", [format_mfa(MFA), String]};
373+
[Thing, Rel, String]};
374+
format_error_1({deprecated, MFA, String}) when is_tuple(MFA) ->
375+
format_error_1({deprecated, format_mfa(MFA), String});
376+
format_error_1({deprecated, Thing, String}) when is_list(String) ->
377+
{~"~s is deprecated; ~s", [Thing, String]};
374378
format_error_1({deprecated_type, {M1, F1, A1}, String, Rel}) ->
375379
{~"the type ~p:~p~s is deprecated and will be removed in ~s; ~s",
376380
[M1, F1, gen_type_paren(A1), Rel, String]};
@@ -2498,14 +2502,23 @@ gexpr({op,_Anno,EqOp,L,R}, Vt, St0) when EqOp =:= '=:='; EqOp =:= '=/=' ->
24982502
gexpr({op,Anno,Op,L,R}, Vt, St0) ->
24992503
{Avt,St1} = gexpr_list([L,R], Vt, St0),
25002504
case is_gexpr_op(Op, 2) of
2501-
true -> {Avt,St1};
2505+
true -> {Avt,warn_obsolete_op(Op, 2, Anno, St1)};
25022506
false -> {Avt,add_error(Anno, illegal_guard_expr, St1)}
25032507
end;
25042508
%% Everything else is illegal! You could put explicit tests here to
25052509
%% better error diagnostics.
25062510
gexpr(E, _Vt, St) ->
25072511
{[],add_error(element(2, E), illegal_guard_expr, St)}.
25082512

2513+
warn_obsolete_op(Op, A, Anno, St) ->
2514+
case {Op, A} of
2515+
{'and', 2} ->
2516+
add_warning(Anno, {deprecated, "'and'", "use 'andalso' instead", "OTP 29"}, St);
2517+
{'or', 2} ->
2518+
add_warning(Anno, {deprecated, "'or'", "use 'orelse' instead", "OTP 29"}, St);
2519+
_ -> St
2520+
end.
2521+
25092522
%% gexpr_list(Expressions, VarTable, State) ->
25102523
%% {UsedVarTable,State'}
25112524

@@ -2918,8 +2931,9 @@ expr({op,Anno,Op,L,R}, Vt, St0) when Op =:= 'orelse'; Op =:= 'andalso' ->
29182931
expr({op,_Anno,EqOp,L,R}, Vt, St0) when EqOp =:= '=:='; EqOp =:= '=/=' ->
29192932
St = expr_check_match_zero(R, expr_check_match_zero(L, St0)),
29202933
expr_list([L,R], Vt, St); %They see the same variables
2921-
expr({op,_Anno,_Op,L,R}, Vt, St) ->
2922-
expr_list([L,R], Vt, St); %They see the same variables
2934+
expr({op,Anno,Op,L,R}, Vt, St) ->
2935+
St1 = warn_obsolete_op(Op, 2, Anno, St),
2936+
expr_list([L,R], Vt, St1); %They see the same variables
29232937
%% The following are not allowed to occur anywhere!
29242938
expr({remote,_Anno,M,_F}, _Vt, St) ->
29252939
{[],add_error(erl_parse:first_anno(M), illegal_expr, St)};

lib/stdlib/test/erl_lint_SUITE.erl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ unused_vars_warn_fun(Config) when is_list(Config) ->
610610
E;
611611
a([A,B,C,D,E]) -> % E unused.
612612
fun() ->
613-
(C == <<A:A>>) and (<<17:B>> == D)
613+
(C == <<A:A>>) andalso (<<17:B>> == D)
614614
end.
615615
">>,
616616
[warn_unused_vars],
@@ -1792,33 +1792,33 @@ guard(Config) when is_list(Config) ->
17921792
[]},
17931793
{guard4,
17941794
<<"-record(apa, {}).
1795-
t3(A) when float(A) or float(A) -> % coercing... (badarg)
1795+
t3(A) when float(A) orelse float(A) -> % coercing... (badarg)
17961796
float;
1797-
t3(A) when is_atom(A) or is_atom(A) ->
1797+
t3(A) when is_atom(A) orelse is_atom(A) ->
17981798
is_atom;
1799-
t3(A) when is_binary(A) or is_binary(A) ->
1799+
t3(A) when is_binary(A) orelse is_binary(A) ->
18001800
is_binary;
1801-
t3(A) when is_float(A) or is_float(A) ->
1801+
t3(A) when is_float(A) orelse is_float(A) ->
18021802
is_float;
1803-
t3(A) when is_function(A) or is_function(A) ->
1803+
t3(A) when is_function(A) orelse is_function(A) ->
18041804
is_function;
1805-
t3(A) when is_integer(A) or is_integer(A) ->
1805+
t3(A) when is_integer(A) orelse is_integer(A) ->
18061806
is_integer;
1807-
t3(A) when is_list(A) or is_list(A) ->
1807+
t3(A) when is_list(A) orelse is_list(A) ->
18081808
is_list;
1809-
t3(A) when is_number(A) or is_number(A) ->
1809+
t3(A) when is_number(A) orelse is_number(A) ->
18101810
is_number;
1811-
t3(A) when is_pid(A) or is_pid(A) ->
1811+
t3(A) when is_pid(A) orelse is_pid(A) ->
18121812
is_pid;
1813-
t3(A) when is_port(A) or is_port(A) ->
1813+
t3(A) when is_port(A) orelse is_port(A) ->
18141814
is_port;
1815-
t3(A) when is_record(A, apa) or is_record(A, apa) ->
1815+
t3(A) when is_record(A, apa) orelse is_record(A, apa) ->
18161816
is_record;
1817-
t3(A) when is_record(A, apa, 1) or is_record(A, apa, 1) ->
1817+
t3(A) when is_record(A, apa, 1) orelse is_record(A, apa, 1) ->
18181818
is_record;
1819-
t3(A) when is_reference(A) or is_reference(A) ->
1819+
t3(A) when is_reference(A) orelse is_reference(A) ->
18201820
is_reference;
1821-
t3(A) when is_tuple(A) or is_tuple(A) ->
1821+
t3(A) when is_tuple(A) orelse is_tuple(A) ->
18221822
is_tuple.
18231823
">>,
18241824
[nowarn_obsolete_guard],
@@ -1871,7 +1871,7 @@ guard(Config) when is_list(Config) ->
18711871
{guard7,
18721872
<<"-record(apa,{}).
18731873
t() ->
1874-
[X || X <- [1,#apa{},3], (3+is_record(X, apa)) or
1874+
[X || X <- [1,#apa{},3], (3+is_record(X, apa)) orelse
18751875
(is_record(X, apa)*2)].
18761876
">>,
18771877
[],
@@ -2929,7 +2929,7 @@ otp_5878(Config) when is_list(Config) ->
29292929
t() ->
29302930
case x() of
29312931
_ when l()
2932-
or
2932+
orelse
29332933
l() ->
29342934
foo
29352935
end.

0 commit comments

Comments
 (0)