zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

unused_enum_operation.ex (1947B)


      1 defmodule Credo.Check.Warning.UnusedEnumOperation do
      2   use Credo.Check,
      3     base_priority: :high,
      4     explanations: [
      5       check: """
      6       With the exception of `Enum.each/2`, the result of a call to the
      7       Enum module's functions has to be used.
      8 
      9       While this is correct ...
     10 
     11           def prepend_my_username(my_username, usernames) do
     12             valid_usernames = Enum.reject(usernames, &is_nil/1)
     13 
     14             [my_username] ++ valid_usernames
     15           end
     16 
     17       ... we forgot to save the downcased username in this example:
     18 
     19           # This is bad because it does not modify the usernames variable!
     20 
     21           def prepend_my_username(my_username, usernames) do
     22             Enum.reject(usernames, &is_nil/1)
     23 
     24             [my_username] ++ valid_usernames
     25           end
     26 
     27       Since Elixir variables are immutable, Enum operations never work on the
     28       variable you pass in, but return a new variable which has to be used somehow
     29       (the exception being `Enum.each/2` which iterates a list and returns `:ok`).
     30       """
     31     ]
     32 
     33   alias Credo.Check.Warning.UnusedOperation
     34 
     35   @checked_module :Enum
     36   @funs_with_return_value ~w(
     37       all? any? at chunk chunk chunk_by concat concat count count dedup
     38       dedup_by drop drop_while empty? fetch fetch! filter filter_map
     39       find find_index find_value flat_map flat_map_reduce group_by
     40       intersperse into into join map map_join map_reduce max max_by
     41       member min min_by min_max min_max_by partition random reduce
     42       reduce_while reject reverse reverse reverse_slice scan
     43       scan shuffle slice slice sort sort sort_by split split_while
     44       sum take take_every take_random take_while to_list uniq uniq_by
     45       unzip with_index zip
     46     )a
     47 
     48   @doc false
     49   @impl true
     50   def run(%SourceFile{} = source_file, params) do
     51     UnusedOperation.run(
     52       source_file,
     53       params,
     54       @checked_module,
     55       @funs_with_return_value,
     56       &format_issue/2
     57     )
     58   end
     59 end