Skip to content

Commit

Permalink
Merge pull request #449 from crystal-ameba/fix-issue-446
Browse files Browse the repository at this point in the history
Do not report type declarations within generic records
  • Loading branch information
Sija authored Jan 16, 2024
2 parents b56d347 + f2677d6 commit 28fafea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
6 changes: 6 additions & 0 deletions spec/ameba/rule/lint/useless_assign_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,12 @@ module Ameba::Rule::Lint
CRYSTAL
end

it "doesn't report if this is a record declaration (generics)" do
expect_no_issues subject, <<-CRYSTAL
record Foo(T), foo : T
CRYSTAL
end

it "doesn't report if this is an accessor declaration" do
accessor_macros = %w[setter class_setter]
%w[getter class_getter property class_property].each do |name|
Expand Down
16 changes: 12 additions & 4 deletions src/ameba/ast/visitors/scope_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,10 @@ module Ameba::AST
scope = @current_scope

case
when scope.top_level? && record_macro?(node) then return false
when scope.type_definition? && record_macro?(node) then return false
when scope.type_definition? && accessor_macro?(node) then return false
when (scope.top_level? || scope.type_definition?) && record_macro?(node)
return false
when scope.type_definition? && accessor_macro?(node)
return false
when scope.def? && special_node?(node)
scope.arguments.each do |arg|
ref = arg.variable.reference(scope)
Expand All @@ -194,7 +195,14 @@ module Ameba::AST
end

private def record_macro?(node)
node.name == "record" && node.args.first?.is_a?(Crystal::Path)
return false unless node.name == "record"

case node.args.first?
when Crystal::Path, Crystal::Generic
true
else
false
end
end

private def skip?(node)
Expand Down

0 comments on commit 28fafea

Please sign in to comment.