Skip to content

Commit e45f20e

Browse files
committed
Make the compiler report 'and'/'or' operators as obsolete
1 parent 44ffe88 commit e45f20e

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
@@ -356,11 +356,15 @@ format_error_1({redefine_bif_import,{F,A}}) ->
356356
import directive overrides auto-imported BIF ~w/~w --
357357
use "-compile({no_auto_import,[~w/~w]})." to resolve name clash
358358
""", [F,A,F,A]};
359-
format_error_1({deprecated, MFA, String, Rel}) ->
359+
format_error_1({deprecated, MFA, String, Rel}) when is_tuple(MFA) ->
360+
format_error_1({deprecated, format_mfa(MFA), String, Rel});
361+
format_error_1({deprecated, Thing, String, Rel}) when is_list(String) ->
360362
{~"~s is deprecated and will be removed in ~s; ~s",
361-
[format_mfa(MFA), Rel, String]};
362-
format_error_1({deprecated, MFA, String}) when is_list(String) ->
363-
{~"~s is deprecated; ~s", [format_mfa(MFA), String]};
363+
[Thing, Rel, String]};
364+
format_error_1({deprecated, MFA, String}) when is_tuple(MFA) ->
365+
format_error_1({deprecated, format_mfa(MFA), String});
366+
format_error_1({deprecated, Thing, String}) when is_list(String) ->
367+
{~"~s is deprecated; ~s", [Thing, String]};
364368
format_error_1({deprecated_type, {M1, F1, A1}, String, Rel}) ->
365369
{~"the type ~p:~p~s is deprecated and will be removed in ~s; ~s",
366370
[M1, F1, gen_type_paren(A1), Rel, String]};
@@ -2392,14 +2396,23 @@ gexpr({op,_Anno,EqOp,L,R}, Vt, St0) when EqOp =:= '=:='; EqOp =:= '=/=' ->
23922396
gexpr({op,Anno,Op,L,R}, Vt, St0) ->
23932397
{Avt,St1} = gexpr_list([L,R], Vt, St0),
23942398
case is_gexpr_op(Op, 2) of
2395-
true -> {Avt,St1};
2399+
true -> {Avt,warn_obsolete_op(Op, 2, Anno, St1)};
23962400
false -> {Avt,add_error(Anno, illegal_guard_expr, St1)}
23972401
end;
23982402
%% Everything else is illegal! You could put explicit tests here to
23992403
%% better error diagnostics.
24002404
gexpr(E, _Vt, St) ->
24012405
{[],add_error(element(2, E), illegal_guard_expr, St)}.
24022406

2407+
warn_obsolete_op(Op, A, Anno, St) ->
2408+
case {Op, A} of
2409+
{'and', 2} ->
2410+
add_warning(Anno, {deprecated, "'and'", "use 'andalso' instead", "OTP 29"}, St);
2411+
{'or', 2} ->
2412+
add_warning(Anno, {deprecated, "'or'", "use 'orelse' instead", "OTP 29"}, St);
2413+
_ -> St
2414+
end.
2415+
24032416
%% gexpr_list(Expressions, VarTable, State) ->
24042417
%% {UsedVarTable,State'}
24052418

@@ -2812,8 +2825,9 @@ expr({op,Anno,Op,L,R}, Vt, St0) when Op =:= 'orelse'; Op =:= 'andalso' ->
28122825
expr({op,_Anno,EqOp,L,R}, Vt, St0) when EqOp =:= '=:='; EqOp =:= '=/=' ->
28132826
St = expr_check_match_zero(R, expr_check_match_zero(L, St0)),
28142827
expr_list([L,R], Vt, St); %They see the same variables
2815-
expr({op,_Anno,_Op,L,R}, Vt, St) ->
2816-
expr_list([L,R], Vt, St); %They see the same variables
2828+
expr({op,Anno,Op,L,R}, Vt, St) ->
2829+
St1 = warn_obsolete_op(Op, 2, Anno, St),
2830+
expr_list([L,R], Vt, St1); %They see the same variables
28172831
%% The following are not allowed to occur anywhere!
28182832
expr({remote,_Anno,M,_F}, _Vt, St) ->
28192833
{[],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
@@ -606,7 +606,7 @@ unused_vars_warn_fun(Config) when is_list(Config) ->
606606
E;
607607
a([A,B,C,D,E]) -> % E unused.
608608
fun() ->
609-
(C == <<A:A>>) and (<<17:B>> == D)
609+
(C == <<A:A>>) andalso (<<17:B>> == D)
610610
end.
611611
">>,
612612
[warn_unused_vars],
@@ -1797,33 +1797,33 @@ guard(Config) when is_list(Config) ->
17971797
[]},
17981798
{guard4,
17991799
<<"-record(apa, {}).
1800-
t3(A) when float(A) or float(A) -> % coercing... (badarg)
1800+
t3(A) when float(A) orelse float(A) -> % coercing... (badarg)
18011801
float;
1802-
t3(A) when is_atom(A) or is_atom(A) ->
1802+
t3(A) when is_atom(A) orelse is_atom(A) ->
18031803
is_atom;
1804-
t3(A) when is_binary(A) or is_binary(A) ->
1804+
t3(A) when is_binary(A) orelse is_binary(A) ->
18051805
is_binary;
1806-
t3(A) when is_float(A) or is_float(A) ->
1806+
t3(A) when is_float(A) orelse is_float(A) ->
18071807
is_float;
1808-
t3(A) when is_function(A) or is_function(A) ->
1808+
t3(A) when is_function(A) orelse is_function(A) ->
18091809
is_function;
1810-
t3(A) when is_integer(A) or is_integer(A) ->
1810+
t3(A) when is_integer(A) orelse is_integer(A) ->
18111811
is_integer;
1812-
t3(A) when is_list(A) or is_list(A) ->
1812+
t3(A) when is_list(A) orelse is_list(A) ->
18131813
is_list;
1814-
t3(A) when is_number(A) or is_number(A) ->
1814+
t3(A) when is_number(A) orelse is_number(A) ->
18151815
is_number;
1816-
t3(A) when is_pid(A) or is_pid(A) ->
1816+
t3(A) when is_pid(A) orelse is_pid(A) ->
18171817
is_pid;
1818-
t3(A) when is_port(A) or is_port(A) ->
1818+
t3(A) when is_port(A) orelse is_port(A) ->
18191819
is_port;
1820-
t3(A) when is_record(A, apa) or is_record(A, apa) ->
1820+
t3(A) when is_record(A, apa) orelse is_record(A, apa) ->
18211821
is_record;
1822-
t3(A) when is_record(A, apa, 1) or is_record(A, apa, 1) ->
1822+
t3(A) when is_record(A, apa, 1) orelse is_record(A, apa, 1) ->
18231823
is_record;
1824-
t3(A) when is_reference(A) or is_reference(A) ->
1824+
t3(A) when is_reference(A) orelse is_reference(A) ->
18251825
is_reference;
1826-
t3(A) when is_tuple(A) or is_tuple(A) ->
1826+
t3(A) when is_tuple(A) orelse is_tuple(A) ->
18271827
is_tuple.
18281828
">>,
18291829
[nowarn_obsolete_guard],
@@ -1876,7 +1876,7 @@ guard(Config) when is_list(Config) ->
18761876
{guard7,
18771877
<<"-record(apa,{}).
18781878
t() ->
1879-
[X || X <- [1,#apa{},3], (3+is_record(X, apa)) or
1879+
[X || X <- [1,#apa{},3], (3+is_record(X, apa)) orelse
18801880
(is_record(X, apa)*2)].
18811881
">>,
18821882
[],
@@ -2934,7 +2934,7 @@ otp_5878(Config) when is_list(Config) ->
29342934
t() ->
29352935
case x() of
29362936
_ when l()
2937-
or
2937+
orelse
29382938
l() ->
29392939
foo
29402940
end.

0 commit comments

Comments
 (0)