2018-06-16 11:50:59 +00:00
|
|
|
require "../../../spec_helper"
|
2018-05-08 19:00:17 +00:00
|
|
|
|
2018-06-16 11:50:59 +00:00
|
|
|
module Ameba::Rule::Lint
|
2018-05-08 19:00:17 +00:00
|
|
|
subject = UnusedArgument.new
|
|
|
|
subject.ignore_defs = false
|
|
|
|
|
|
|
|
describe UnusedArgument do
|
|
|
|
it "doesn't report if arguments are used" do
|
2022-12-19 14:40:30 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
def method(a, b, c)
|
|
|
|
a + b + c
|
|
|
|
end
|
|
|
|
|
|
|
|
3.times do |i|
|
|
|
|
i + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
->(i : Int32) { i + 1 }
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports if method argument is unused" do
|
2022-12-19 14:40:30 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
def method(a, b, c)
|
2022-12-19 14:40:30 +00:00
|
|
|
# ^ error: Unused argument `c`. If it's necessary, use `_c` as an argument name to indicate that it won't be used.
|
2018-05-08 19:00:17 +00:00
|
|
|
a + b
|
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
def method(a, b, _c)
|
|
|
|
a + b
|
|
|
|
end
|
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports if block argument is unused" do
|
2022-12-19 14:40:30 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
|
|
|
[1, 2].each_with_index do |a, i|
|
|
|
|
# ^ error: Unused argument `i`. [...]
|
|
|
|
a
|
|
|
|
end
|
|
|
|
CRYSTAL
|
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
[1, 2].each_with_index do |a, _|
|
2018-05-08 19:00:17 +00:00
|
|
|
a
|
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports if proc argument is unused" do
|
2022-12-19 14:40:30 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
-> (a : Int32, b : String) do
|
2023-06-09 23:11:21 +00:00
|
|
|
# ^^^^^^^^^^ error: Unused argument `b`. If it's necessary, use `_b` as an argument name to indicate that it won't be used.
|
2018-05-08 19:00:17 +00:00
|
|
|
a = a + 1
|
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
-> (a : Int32, _b : String) do
|
|
|
|
a = a + 1
|
|
|
|
end
|
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports multiple unused args" do
|
2022-12-19 14:40:30 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
def method(a, b, c)
|
2022-12-19 14:40:30 +00:00
|
|
|
# ^ error: Unused argument `a`. If it's necessary, use `_a` as an argument name to indicate that it won't be used.
|
|
|
|
# ^ error: Unused argument `b`. If it's necessary, use `_b` as an argument name to indicate that it won't be used.
|
|
|
|
# ^ error: Unused argument `c`. If it's necessary, use `_c` as an argument name to indicate that it won't be used.
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
CRYSTAL
|
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
def method(_a, _b, _c)
|
2018-05-08 19:00:17 +00:00
|
|
|
nil
|
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't report if it is an instance var argument" do
|
2022-12-19 14:40:30 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
class A
|
|
|
|
def method(@name)
|
|
|
|
end
|
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't report if a typed argument is used" do
|
2022-12-19 14:40:30 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
def method(x : Int32)
|
|
|
|
3.times do
|
|
|
|
puts x
|
|
|
|
end
|
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't report if an argument with default value is used" do
|
2022-12-19 14:40:30 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
def method(x = 1)
|
|
|
|
puts x
|
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't report if argument starts with a _" do
|
2022-12-19 14:40:30 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
def method(_x)
|
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't report if it is a block and used" do
|
2022-12-19 14:40:30 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
def method(&block)
|
|
|
|
block.call
|
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
|
2022-12-13 20:30:44 +00:00
|
|
|
it "doesn't report if block arg is not used" do
|
2022-12-19 14:40:30 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
def method(&block)
|
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
|
2022-12-13 20:30:44 +00:00
|
|
|
it "doesn't report if unused and there is yield" do
|
2022-12-19 14:40:30 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
def method(&block)
|
|
|
|
yield 1
|
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
|
2022-12-12 17:14:49 +00:00
|
|
|
it "doesn't report if it's an anonymous block" do
|
2022-12-19 14:40:30 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2022-12-12 17:14:49 +00:00
|
|
|
def method(&)
|
|
|
|
yield 1
|
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2022-12-12 17:14:49 +00:00
|
|
|
end
|
|
|
|
|
2018-05-08 19:00:17 +00:00
|
|
|
it "doesn't report if variable is referenced implicitly" do
|
2022-12-19 14:40:30 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
class Bar < Foo
|
|
|
|
def method(a, b)
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
|
2018-05-08 19:18:15 +00:00
|
|
|
it "doesn't report if arg if referenced in case" do
|
2022-12-19 14:40:30 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2018-05-08 19:18:15 +00:00
|
|
|
def foo(a)
|
|
|
|
case a
|
|
|
|
when /foo/
|
|
|
|
end
|
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-05-08 19:18:15 +00:00
|
|
|
end
|
|
|
|
|
2018-12-12 20:21:33 +00:00
|
|
|
it "doesn't report if enum in a record" do
|
2022-12-19 14:40:30 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2018-12-12 20:21:33 +00:00
|
|
|
class Class
|
|
|
|
record Record do
|
|
|
|
enum Enum
|
|
|
|
CONSTANT
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-12-12 20:21:33 +00:00
|
|
|
end
|
|
|
|
|
2018-05-08 19:00:17 +00:00
|
|
|
context "super" do
|
|
|
|
it "reports if variable is not referenced implicitly by super" do
|
2022-12-19 14:40:30 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
class Bar < Foo
|
|
|
|
def method(a, b)
|
2022-12-19 14:40:30 +00:00
|
|
|
# ^ error: Unused argument `b`. If it's necessary, use `_b` as an argument name to indicate that it won't be used.
|
2018-05-08 19:00:17 +00:00
|
|
|
super a
|
|
|
|
end
|
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
|
2022-12-19 14:40:30 +00:00
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
class Bar < Foo
|
|
|
|
def method(a, _b)
|
|
|
|
super a
|
|
|
|
end
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "macro" do
|
|
|
|
it "doesn't report if it is a used macro argument" do
|
2022-12-19 14:40:30 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
macro my_macro(arg)
|
|
|
|
{% arg %}
|
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't report if it is a used macro block argument" do
|
2022-12-19 14:40:30 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
macro my_macro(&block)
|
|
|
|
{% block %}
|
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't report used macro args with equal names in record" do
|
2022-12-19 14:40:30 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
record X do
|
|
|
|
macro foo(a, b)
|
2022-11-14 00:24:29 +00:00
|
|
|
{{ a }} + {{ b }}
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
macro bar(a, b, c)
|
2022-11-14 00:24:29 +00:00
|
|
|
{{ a }} + {{ b }} + {{ c }}
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
2019-10-27 19:32:55 +00:00
|
|
|
|
|
|
|
it "doesn't report used args in macro literals" do
|
2022-12-19 14:40:30 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2019-10-27 19:32:55 +00:00
|
|
|
def print(f : Array(U)) forall U
|
|
|
|
f.size.times do |i|
|
|
|
|
{% if U == Float64 %}
|
|
|
|
puts f[i].round(3)
|
|
|
|
{% else %}
|
|
|
|
puts f[i]
|
|
|
|
{% end %}
|
|
|
|
end
|
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2019-10-27 19:32:55 +00:00
|
|
|
end
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context "properties" do
|
|
|
|
describe "#ignore_defs" do
|
|
|
|
it "lets the rule to ignore def scopes if true" do
|
2022-12-19 14:40:30 +00:00
|
|
|
rule = UnusedArgument.new
|
|
|
|
rule.ignore_defs = true
|
|
|
|
|
|
|
|
expect_no_issues rule, <<-CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
def method(a)
|
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "lets the rule not to ignore def scopes if false" do
|
2022-12-19 14:40:30 +00:00
|
|
|
rule = UnusedArgument.new
|
|
|
|
rule.ignore_defs = false
|
|
|
|
|
|
|
|
expect_issue rule, <<-CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
def method(a)
|
2022-12-19 14:40:30 +00:00
|
|
|
# ^ error: Unused argument `a`. If it's necessary, use `_a` as an argument name to indicate that it won't be used.
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "#ignore_blocks" do
|
|
|
|
it "lets the rule to ignore block scopes if true" do
|
2022-12-19 14:40:30 +00:00
|
|
|
rule = UnusedArgument.new
|
|
|
|
rule.ignore_blocks = true
|
|
|
|
|
|
|
|
expect_no_issues rule, <<-CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
3.times { |i| puts "yo!" }
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "lets the rule not to ignore block scopes if false" do
|
2022-12-19 14:40:30 +00:00
|
|
|
rule = UnusedArgument.new
|
|
|
|
rule.ignore_blocks = false
|
|
|
|
|
|
|
|
expect_issue rule, <<-CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
3.times { |i| puts "yo!" }
|
2022-12-19 14:40:30 +00:00
|
|
|
# ^ error: Unused argument `i`. If it's necessary, use `_` as an argument name to indicate that it won't be used.
|
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "#ignore_procs" do
|
|
|
|
it "lets the rule to ignore proc scopes if true" do
|
2022-12-19 14:40:30 +00:00
|
|
|
rule = UnusedArgument.new
|
|
|
|
rule.ignore_procs = true
|
|
|
|
|
|
|
|
expect_no_issues rule, <<-CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
->(a : Int32) {}
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "lets the rule not to ignore proc scopes if false" do
|
2022-12-19 14:40:30 +00:00
|
|
|
rule = UnusedArgument.new
|
|
|
|
rule.ignore_procs = false
|
|
|
|
|
|
|
|
expect_issue rule, <<-CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
->(a : Int32) {}
|
2023-06-09 23:11:21 +00:00
|
|
|
# ^^^^^^^^^ error: Unused argument `a`. If it's necessary, use `_a` as an argument name to indicate that it won't be used.
|
2022-12-19 14:40:30 +00:00
|
|
|
CRYSTAL
|
2018-05-08 19:00:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|