Useless Assign: ignore variables with '_' prefix

closes #164
This commit is contained in:
Vitalii Elenhaupt 2020-08-20 21:15:58 +03:00
parent 318fbdc4bf
commit b5bd76dfa8
No known key found for this signature in database
GPG key ID: CD0BF17825928BC0
4 changed files with 31 additions and 1 deletions

View file

@ -140,6 +140,23 @@ module Ameba::AST
end
end
describe "#ignored?" do
it "is true if variable is ignored" do
variable = Variable.new(Crystal::Var.new("_var"), scope)
variable.ignored?.should be_true
end
it "is false if variable is not ignored" do
variable = Variable.new(Crystal::Var.new("v_ar"), scope)
variable.ignored?.should be_false
end
it "is true if variable is a black hole" do
variable = Variable.new(Crystal::Var.new("_"), scope)
variable.ignored?.should be_true
end
end
describe "#eql?" do
var = Crystal::Var.new("foo").at(Crystal::Location.new(nil, 1, 2))
variable = Variable.new var, scope

View file

@ -54,6 +54,14 @@ module Ameba::Rule::Lint
subject.catch(s).should_not be_valid
end
it "does not report ignored assigments" do
s = Source.new %(
payload, _header = decode
puts payload
)
subject.catch(s).should be_valid
end
it "reports a useless assignment in a proc inside a block" do
s = Source.new %(
def method