Skip to content

Commit e69dc29

Browse files
authored
Merge pull request #1166 from Wigny/wigny/add-unusedvariablenames-checks
Add `UnusedVariableNames` missing checks
2 parents dd666fa + 68aaace commit e69dc29

File tree

2 files changed

+137
-2
lines changed

2 files changed

+137
-2
lines changed

lib/credo/check/consistency/unused_variable_names/collector.ex

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ defmodule Credo.Check.Consistency.UnusedVariableNames.Collector do
1717
|> Enum.reverse()
1818
end
1919

20-
defp traverse(callback, {:=, _, params} = ast, acc) do
20+
defp traverse(callback, {match, _, params} = ast, acc) when match in ~w[= <-]a do
2121
{ast, reduce_unused_variables(params, callback, acc)}
2222
end
2323

2424
defp traverse(callback, {def, _, [{_, _, params} | _]} = ast, acc)
25-
when def in [:def, :defp] do
25+
when def in [:def, :defp, :defmacro, :defmacrop] do
2626
{ast, reduce_unused_variables(params, callback, acc)}
2727
end
2828

@@ -39,6 +39,12 @@ defmodule Credo.Check.Consistency.UnusedVariableNames.Collector do
3939
{_, _, params}, param_acc when is_list(params) ->
4040
reduce_unused_variables(params, callback, param_acc)
4141

42+
tuple_ast, param_acc when tuple_size(tuple_ast) == 2 ->
43+
reduce_unused_variables(Tuple.to_list(tuple_ast), callback, param_acc)
44+
45+
list_ast, param_acc when is_list(list_ast) ->
46+
reduce_unused_variables(list_ast, callback, param_acc)
47+
4248
param_ast, param_acc ->
4349
if unused_variable_ast?(param_ast) do
4450
callback.(param_ast, param_acc)

test/credo/check/consistency/unused_variable_names_test.exs

+129
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,135 @@ defmodule Credo.Check.Consistency.UnusedVariableNamesTest do
221221
end)
222222
end
223223

224+
test "it should report a violation for different naming schemes in a two elem tuple match (expects meaningful)" do
225+
[
226+
"""
227+
defmodule Credo.SampleOne do
228+
defmodule Foo do
229+
def bar(x1, x2) do
230+
{_a, _b} = x1
231+
{_c, _} = x2
232+
end
233+
end
234+
end
235+
""",
236+
"""
237+
defmodule Credo.SampleTwo do
238+
defmodule Foo do
239+
def bar(x1, x2) do
240+
with {:ok, _} <- x1,
241+
{:ok, _b} <- x2, do: :ok
242+
end
243+
end
244+
end
245+
"""
246+
]
247+
|> to_source_files()
248+
|> run_check(@described_check)
249+
|> assert_issues(fn issues ->
250+
assert length(issues) == 2
251+
252+
assert Enum.find(issues, &match?(%{trigger: "_", line_no: 5}, &1))
253+
assert Enum.find(issues, &match?(%{trigger: "_", line_no: 4}, &1))
254+
end)
255+
end
256+
257+
test "it should report a violation for different naming schemes with a map match (expects meaningful)" do
258+
[
259+
"""
260+
defmodule Credo.SampleOne do
261+
defmodule Foo do
262+
def bar(%{a: _a, b: _b, c: _}) do
263+
:ok
264+
end
265+
end
266+
end
267+
""",
268+
"""
269+
defmodule Credo.SampleTwo do
270+
defmodule Foo do
271+
def bar(map) do
272+
case map do
273+
%{a: _} -> :ok
274+
_map -> :error
275+
end
276+
end
277+
end
278+
end
279+
"""
280+
]
281+
|> to_source_files()
282+
|> run_check(@described_check)
283+
|> assert_issues(fn issues ->
284+
assert length(issues) == 2
285+
286+
assert Enum.find(issues, &match?(%{trigger: "_", line_no: 3}, &1))
287+
assert Enum.find(issues, &match?(%{trigger: "_", line_no: 5}, &1))
288+
end)
289+
end
290+
291+
test "it should report a violation for different naming schemes with a list match (expects meaningful)" do
292+
[
293+
"""
294+
defmodule Credo.SampleOne do
295+
defmodule Foo do
296+
def bar(list) do
297+
case list do
298+
[] -> :empty
299+
[head | _] -> head
300+
end
301+
end
302+
end
303+
end
304+
""",
305+
"""
306+
defmodule Credo.SampleTwo do
307+
defmodule Foo do
308+
def bar([_a, _b | rest]) do
309+
rest
310+
end
311+
end
312+
end
313+
"""
314+
]
315+
|> to_source_files()
316+
|> run_check(@described_check)
317+
|> assert_issue(fn issue ->
318+
assert "_" == issue.trigger
319+
assert 6 == issue.line_no
320+
end)
321+
end
322+
323+
test "it should report a violation for different naming schemes with a macro (expects meaningful)" do
324+
[
325+
"""
326+
defmodule Credo.SampleOne do
327+
defmodule Foo do
328+
defmacro __using__(_) do
329+
end
330+
end
331+
332+
def bar(_opts) do
333+
end
334+
end
335+
""",
336+
"""
337+
defmodule Credo.SampleTwo do
338+
defmodule Foo do
339+
defmacrop bar(_opts) do
340+
end
341+
end
342+
end
343+
"""
344+
]
345+
|> to_source_files()
346+
|> run_check(@described_check)
347+
|> assert_issue(fn issue ->
348+
assert "_" == issue.trigger
349+
assert 3 == issue.line_no
350+
end)
351+
end
352+
224353
test "it should report a violation for naming schemes other than the forced one" do
225354
[
226355
"""

0 commit comments

Comments
 (0)