Skip to content

Commit

Permalink
Check in maps
Browse files Browse the repository at this point in the history
  • Loading branch information
Wigny committed Dec 9, 2024
1 parent 264ce87 commit 68aaace
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,14 @@ defmodule Credo.Check.Consistency.UnusedVariableNames.Collector do
{_, _, params}, param_acc when is_list(params) ->
reduce_unused_variables(params, callback, param_acc)

# two elements tuple
{left, right}, param_acc ->
reduce_unused_variables([left, right], callback, param_acc)
tuple_ast, param_acc when tuple_size(tuple_ast) == 2 ->
reduce_unused_variables(Tuple.to_list(tuple_ast), callback, param_acc)

list_ast, param_acc when is_list(list_ast) ->
reduce_unused_variables(list_ast, callback, param_acc)

param_ast, param_acc ->
if unused_variable_ast?(param_ast) do
IO.inspect(param_ast, label: :param_ast)
callback.(param_ast, param_acc)
else
param_acc
Expand Down
36 changes: 35 additions & 1 deletion test/credo/check/consistency/unused_variable_names_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,41 @@ defmodule Credo.Check.Consistency.UnusedVariableNamesTest do
end)
end

test "it should report a violation for different naming schemes with a list (expects meaningful)" do
test "it should report a violation for different naming schemes with a map match (expects meaningful)" do
[
"""
defmodule Credo.SampleOne do
defmodule Foo do
def bar(%{a: _a, b: _b, c: _}) do
:ok
end
end
end
""",
"""
defmodule Credo.SampleTwo do
defmodule Foo do
def bar(map) do
case map do
%{a: _} -> :ok
_map -> :error
end
end
end
end
"""
]
|> to_source_files()
|> run_check(@described_check)
|> assert_issues(fn issues ->
assert length(issues) == 2

assert Enum.find(issues, &match?(%{trigger: "_", line_no: 3}, &1))
assert Enum.find(issues, &match?(%{trigger: "_", line_no: 5}, &1))
end)
end

test "it should report a violation for different naming schemes with a list match (expects meaningful)" do
[
"""
defmodule Credo.SampleOne do
Expand Down

0 comments on commit 68aaace

Please sign in to comment.