Unused argument name suggession

This commit is contained in:
Vitalii Elenhaupt 2018-05-13 14:13:29 +03:00
parent 0b54b10579
commit 402a0c0dab
No known key found for this signature in database
GPG Key ID: 7558EF3A4056C706
2 changed files with 20 additions and 10 deletions

View File

@ -27,7 +27,8 @@ module Ameba::Rule
end
)
subject.catch(s).should_not be_valid
s.errors.first.message.should eq "Unused argument `c`"
s.errors.first.message.should eq "Unused argument `c`. If it's necessary, use `_c` " \
"as an argument name to indicate that it won't be used."
end
it "reports if block argument is unused" do
@ -37,7 +38,8 @@ module Ameba::Rule
end
)
subject.catch(s).should_not be_valid
s.errors.first.message.should eq "Unused argument `i`"
s.errors.first.message.should eq "Unused argument `i`. If it's necessary, use `_` " \
"as an argument name to indicate that it won't be used."
end
it "reports if proc argument is unused" do
@ -47,7 +49,8 @@ module Ameba::Rule
end
)
subject.catch(s).should_not be_valid
s.errors.first.message.should eq "Unused argument `b`"
s.errors.first.message.should eq "Unused argument `b`. If it's necessary, use `_b` " \
"as an argument name to indicate that it won't be used."
end
it "reports multiple unused args" do
@ -57,9 +60,12 @@ module Ameba::Rule
end
)
subject.catch(s).should_not be_valid
s.errors[0].message.should eq "Unused argument `a`"
s.errors[1].message.should eq "Unused argument `b`"
s.errors[2].message.should eq "Unused argument `c`"
s.errors[0].message.should eq "Unused argument `a`. If it's necessary, use `_a` " \
"as an argument name to indicate that it won't be used."
s.errors[1].message.should eq "Unused argument `b`. If it's necessary, use `_b` " \
"as an argument name to indicate that it won't be used."
s.errors[2].message.should eq "Unused argument `c`. If it's necessary, use `_c` " \
"as an argument name to indicate that it won't be used."
end
it "doesn't report if it is an instance var argument" do
@ -158,7 +164,8 @@ module Ameba::Rule
end
)
subject.catch(s).should_not be_valid
s.errors.first.message.should eq "Unused argument `b`"
s.errors.first.message.should eq "Unused argument `b`. If it's necessary, use `_b` " \
"as an argument name to indicate that it won't be used."
end
it "reports rule, location and message" do
@ -169,7 +176,8 @@ module Ameba::Rule
subject.catch(s).should_not be_valid
error = s.errors.first
error.rule.should_not be_nil
error.message.should eq "Unused argument `a`"
error.message.should eq "Unused argument `a`. If it's necessary, use `_a` " \
"as an argument name to indicate that it won't be used."
error.location.to_s.should eq "source.cr:2:22"
end
end

View File

@ -34,7 +34,8 @@ module Ameba::Rule
ignore_procs false
end
MSG = "Unused argument `%s`"
MSG = "Unused argument `%s`. If it's necessary, use `%s` " \
"as an argument name to indicate that it won't be used."
def test(source)
AST::ScopeVisitor.new self, source
@ -56,7 +57,8 @@ module Ameba::Rule
scope.arguments.each do |argument|
next if argument.ignored? || scope.references?(argument.variable)
source.error self, argument.location, MSG % argument.name
name_suggestion = scope.node.is_a?(Crystal::Block) ? '_' : "_#{argument.name}"
source.error self, argument.location, MSG % [argument.name, name_suggestion]
end
end
end