Update UselessAssign rule to report unreferenced type declarations

This commit is contained in:
Sijawusz Pur Rahnama 2023-12-14 02:35:59 +01:00
parent 4ad151e5e0
commit 9745637cf9
2 changed files with 44 additions and 4 deletions

View file

@ -492,8 +492,8 @@ module Ameba::Rule::Lint
subject.catch(s).should be_valid
end
it "doesn't report if type declaration assigned inside class" do
s = Source.new %(
it "reports if type declaration assigned inside class" do
s = Source.new path: "source.cr", code: %(
class A
foo : String? = "foo"
@ -502,7 +502,11 @@ module Ameba::Rule::Lint
end
end
)
subject.catch(s).should be_valid
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.location.to_s.should eq "source.cr:2:3"
issue.message.should eq "Useless assignment to variable `foo`"
end
end
@ -1068,6 +1072,43 @@ module Ameba::Rule::Lint
subject.catch(s).should be_valid
end
context "type declaration" do
it "reports if it's not referenced at a top level" do
s = Source.new %(
a : String?
)
subject.catch(s).should_not be_valid
end
it "reports if it's not referenced in a method" do
s = Source.new %(
def foo
a : String?
end
)
subject.catch(s).should_not be_valid
end
it "reports if it's not referenced in a class" do
s = Source.new %(
class Foo
a : String?
end
)
subject.catch(s).should_not be_valid
end
it "doesn't report if it's referenced" do
s = Source.new %(
def foo
a : String?
a
end
)
subject.catch(s).should be_valid
end
end
context "uninitialized" do
it "reports if uninitialized assignment is not referenced at a top level" do
s = Source.new %(

View file

@ -39,7 +39,6 @@ module Ameba::Rule::Lint
def test(source, node, scope : AST::Scope)
scope.variables.each do |var|
next if var.ignored? || var.used_in_macro? || var.captured_by_block?
next if scope.assigns_type_dec?(var.name)
var.assignments.each do |assign|
next if assign.referenced?