浏览 2742 次
锁定老帖子 主题:分析表达式警告的原因
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-05-21
最后修改:2009-05-21
root@yufeng-desktop:~# nl expr.erl 1 -module(expr). 2 -export([test/0]). 3 test()-> 4 1, 5 1.0, 6 [], 7 [1,2,3], 8 <<>>, 9 <<1,2,3>>, 10 c:pid(0,1,2), 11 make_ref(), 12 atom, 13 fun()-> ok end, 14 open_port({spawn, "cat"}, [in, out]), 15 "hello", 16 "", 17 {1,2,3}, 18 {}, 19 true, 20 false, 21 1. 22 root@yufeng-desktop:~# erlc +return expr.erl ./expr.erl:4: Warning: a term is constructed, but never used ./expr.erl:5: Warning: a term is constructed, but never used ./expr.erl:7: Warning: a term is constructed, but never used ./expr.erl:8: Warning: a term is constructed, but never used ./expr.erl:9: Warning: a term is constructed, but never used ./expr.erl:11: Warning: the call to make_ref/0 has no effect ./expr.erl:13: Warning: a term is constructed, but never used ./expr.erl:15: Warning: a term is constructed, but never used ./expr.erl:17: Warning: a term is constructed, but never used ./expr.erl:18: Warning: a term is constructed, but never used Compiler function compile:compile/3 returned: {ok,expr, [{"./expr.erl", [{4,sys_core_fold,useless_building}, {5,sys_core_fold,useless_building}, {7,sys_core_fold,useless_building}, {8,sys_core_fold,useless_building}, {9,sys_core_fold,useless_building}, {11,sys_core_fold,{no_effect,{erlang,make_ref,0}}}, {13,sys_core_fold,useless_building}, {15,sys_core_fold,useless_building}, {17,sys_core_fold,useless_building}, {18,sys_core_fold,useless_building}]}]} 看下lib/compiler/src/sys_core_fold.erl的源代码: expr(#c_literal{val=Val}=L, Ctxt, _Sub) -> case Ctxt of effect -> case Val of [] -> %% Keep as [] - might give slightly better code. L; _ when is_atom(Val) -> %% For cleanliness replace with void(). void(); _ -> %% Warn and replace with void(). add_warning(L, useless_building), void() end; value -> L end; expr(#c_cons{anno=Anno,hd=H0,tl=T0}=Cons, Ctxt, Sub) -> H1 = expr(H0, Ctxt, Sub), T1 = expr(T0, Ctxt, Sub), case Ctxt of effect -> add_warning(Cons, useless_building), expr(make_effect_seq([H1,T1], Sub), Ctxt, Sub); value -> ann_c_cons(Anno, H1, T1) end; expr(#c_tuple{anno=Anno,es=Es0}=Tuple, Ctxt, Sub) -> Es = expr_list(Es0, Ctxt, Sub), case Ctxt of effect -> add_warning(Tuple, useless_building), expr(make_effect_seq(Es, Sub), Ctxt, Sub); value -> ann_c_tuple(Anno, Es) end; expr(#c_binary{segments=Ss}=Bin0, Ctxt, Sub) -> %% Warn for useless building, but always build the binary %% anyway to preserve a possible exception. case Ctxt of effect -> add_warning(Bin0, useless_building); value -> ok end, Bin1 = Bin0#c_binary{segments=bitstr_list(Ss, Sub)}, Bin = bin_un_utf(Bin1), eval_binary(Bin); expr(#c_fun{}=Fun, effect, _) -> %% A fun is created, but not used. Warn, and replace with the void value. add_warning(Fun, useless_building), void(); 得出的结论是: 如果编译器能够明确的知道你表达式的值的话,除非这几个东西 atom, [], boolean 不警告以外,其他无用的一律警告, 而且尽可能的不产生代码。 这就解释为什么大部分的函数返回值是 原子或者 []. 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-05-24
恩,很好,这个编译器也一目了然。。
|
|
返回顶楼 | |