Skip to content

Do not report type declarations within generic records #449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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