[erlang-patches] [erlang-bugs] Compiler/linter bug breaking unused variable warnings

Anthony Ramine n.oxyde@REDACTED
Thu Jun 6 01:47:10 CEST 2013


Hello,

When analyzing complex expressions (i.e. comprehensions, cases, tries, ifs and receives), erl_lint does not forget about old unused variables when returning the updated variable table. This causes a bug where old
unused variables are not recorded as such:

t(X, Y) ->
  #r{a=[ K || K <- Y ],b=[ K || K <- Y ].

As erl_lint uses vtmerge_pat/2 to merge the results of the analysis of the two list comprehensions, X is marked as used and the warning is not emitted.

The function vtmerge_pat/2 is used instead of the similar vtmerge/2 which does not mark multiple occurrences of a variable as usage to handle cases like the following one:

t(X, Y) ->
  #r{a=A=X,b=A=Y}.

Other simpler expressions like conses, tuples and external fun references does not correctly follow this behaviour.

This patch fixes both issues and makes erl_lint not return old unused variables in updated tables and makes all compound expressions use vtmerge_pat/2.

	git fetch https://github.com/nox/otp.git fix-erl_lint-variable-usage

	https://github.com/nox/otp/compare/erlang:maint...fix-erl_lint-variable-usage
	https://github.com/nox/otp/compare/erlang:maint...fix-erl_lint-variable-usage.patch

Regards,

-- 
Anthony Ramine

Le 31 mai 2013 à 00:46, Anthony Ramine a écrit :

> Hello,
> 
> Smaller test case reproducing the bug, without KeyList2 nor filter/1:
> 
> -8<--
> -module(missing_warning).
> 
> -export([test_missing_warning/2,
>        test_with_warning1/2,
>        test_with_warning2/2
>       ]).
> 
> -record(data, {aList, bList}).
> 
> test_missing_warning(Data, KeyList) -> %% Data, KeyList never used - no warning.
>   #data{aList = [Key || Key <- []],
>         bList = [Key || Key <- []]}.
> 
> test_with_warning1(Data, KeyList) -> %% Data, KeyList never used - get warning.
>   #data{aList = [Key || Key <- []]}. %% Only one LC in the record.
> 
> test_with_warning2(Data, KeyList) -> %% Data, KeyList never used - get warning.
>   {data,
>    [Key || Key <- []], %% Not in a record.
>    [Key || Key <- []]}.
> -->8-
> 
> Regards,
> 
> -- 
> Anthony Ramine
> 
> Le 29 mai 2013 à 20:02, <Anders.Ramsell@REDACTED> <Anders.Ramsell@REDACTED> a écrit :
> 
>> 
>> When a function creates a record and more than one field is bound to the value of a list comprehension the compiler/linter fails to generate warnings for unused variables in that function. I just tested this on R16B and the problem is still there.
>> 
>> I use the following module to test this:
>> 
>> --8<----------------------------------------------------------
>> -module(missing_warning).
>> 
>> -export([test_missing_warning/2,
>>        test_with_warning1/2,
>>        test_with_warning2/2
>>       ]).
>> 
>> -record(data, {aList, bList}).
>> 
>> test_missing_warning(Data, KeyList) -> %% Data never used - no warning.
>>   KeyList2 = filter(KeyList), %% KeyList2 never used - no warning.
>>   #data{aList = [Key || Key <- KeyList],
>>         bList = [Key || Key <- KeyList]}.
>> 
>> test_with_warning1(Data, KeyList) -> %% Data never used - get warning.
>>   KeyList2 = filter(KeyList), %% KeyList2 never used - get warning.
>>   #data{aList = [Key || Key <- KeyList]}. %% Only one LC in the record.
>> 
>> test_with_warning2(Data, KeyList) -> %% Data never used - get warning.
>>   KeyList2 = filter(KeyList), %% KeyList2 never used - get warning.
>>   {data,
>>    [Key || Key <- KeyList], %% Not in a record. 
>>    [Key || Key <- KeyList]}.
>> 
>> filter(L) -> L.
>> --8<----------------------------------------------------------
>> 
>> In all three test functions the variables Data (in the function head) and KeyList2 (in the function body) are unused.
>> Compiling the module should produce six warnings but I only get four.
>> You get the same result with other "advanced" calls like
>> 	lists:map(fun(Key) -> Key end, KeyList)
>> so it's not limited to list comprehensions.
>> If the fields are bound to e.g. the variable KeyList directly the warnings work just fine.
>> 
>> /Anders
>> 
>> _______________________________________________
>> erlang-bugs mailing list
>> erlang-bugs@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-bugs
> 




More information about the erlang-patches mailing list