From 68aaace28eece64888efaf39a5c8f13ef3db2180 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?W=C3=ADgny?= Date: Mon, 9 Dec 2024 12:08:18 -0400 Subject: [PATCH] Check in maps --- .../unused_variable_names/collector.ex | 6 ++-- .../unused_variable_names_test.exs | 36 ++++++++++++++++++- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/lib/credo/check/consistency/unused_variable_names/collector.ex b/lib/credo/check/consistency/unused_variable_names/collector.ex index ff235e9bf..f2298543a 100644 --- a/lib/credo/check/consistency/unused_variable_names/collector.ex +++ b/lib/credo/check/consistency/unused_variable_names/collector.ex @@ -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 diff --git a/test/credo/check/consistency/unused_variable_names_test.exs b/test/credo/check/consistency/unused_variable_names_test.exs index 5708e2e6f..0f0c5524d 100644 --- a/test/credo/check/consistency/unused_variable_names_test.exs +++ b/test/credo/check/consistency/unused_variable_names_test.exs @@ -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