zf

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

bool_operation_on_same_values.ex (1381B)


      1 defmodule Credo.Check.Warning.BoolOperationOnSameValues do
      2   use Credo.Check,
      3     base_priority: :high,
      4     explanations: [
      5       check: """
      6       Boolean operations with identical values on the left and right side are
      7       most probably a logical fallacy or a copy-and-paste error.
      8 
      9       Examples:
     10 
     11           x && x
     12           x || x
     13           x and x
     14           x or x
     15 
     16       Each of these cases behaves the same as if you were just writing `x`.
     17       """
     18     ]
     19 
     20   @ops [:and, :or, :&&, :||]
     21 
     22   @doc false
     23   @impl true
     24   def run(%SourceFile{} = source_file, params) do
     25     issue_meta = IssueMeta.for(source_file, params)
     26 
     27     Credo.Code.prewalk(source_file, &traverse(&1, &2, issue_meta))
     28   end
     29 
     30   for op <- @ops do
     31     defp traverse({unquote(op), meta, [lhs, rhs]} = ast, issues, issue_meta) do
     32       if Credo.Code.remove_metadata(lhs) === Credo.Code.remove_metadata(rhs) do
     33         new_issue = issue_for(issue_meta, meta[:line], unquote(op))
     34         {ast, issues ++ [new_issue]}
     35       else
     36         {ast, issues}
     37       end
     38     end
     39   end
     40 
     41   defp traverse(ast, issues, _issue_meta) do
     42     {ast, issues}
     43   end
     44 
     45   defp issue_for(issue_meta, line_no, trigger) do
     46     format_issue(
     47       issue_meta,
     48       message:
     49         "There are identical sub-expressions to the left and to the right of the '#{trigger}' operator.",
     50       trigger: trigger,
     51       line_no: line_no
     52     )
     53   end
     54 end