diff --git a/spec/ameba/rule/lint/useless_assign_spec.cr b/spec/ameba/rule/lint/useless_assign_spec.cr index d764a7da..72afaefa 100644 --- a/spec/ameba/rule/lint/useless_assign_spec.cr +++ b/spec/ameba/rule/lint/useless_assign_spec.cr @@ -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 %( diff --git a/src/ameba/rule/lint/useless_assign.cr b/src/ameba/rule/lint/useless_assign.cr index 85ac424f..7aec210d 100644 --- a/src/ameba/rule/lint/useless_assign.cr +++ b/src/ameba/rule/lint/useless_assign.cr @@ -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?