Merge pull request #449 from crystal-ameba/fix-issue-446

Do not report type declarations within generic records
This commit is contained in:
Sijawusz Pur Rahnama 2024-01-16 09:20:20 +01:00 committed by GitHub
commit 28fafea19f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 4 deletions

View file

@ -394,6 +394,12 @@ module Ameba::Rule::Lint
CRYSTAL CRYSTAL
end 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 it "doesn't report if this is an accessor declaration" do
accessor_macros = %w[setter class_setter] accessor_macros = %w[setter class_setter]
%w[getter class_getter property class_property].each do |name| %w[getter class_getter property class_property].each do |name|

View file

@ -173,9 +173,10 @@ module Ameba::AST
scope = @current_scope scope = @current_scope
case case
when scope.top_level? && record_macro?(node) then return false when (scope.top_level? || scope.type_definition?) && record_macro?(node)
when scope.type_definition? && record_macro?(node) then return false return false
when scope.type_definition? && accessor_macro?(node) then return false when scope.type_definition? && accessor_macro?(node)
return false
when scope.def? && special_node?(node) when scope.def? && special_node?(node)
scope.arguments.each do |arg| scope.arguments.each do |arg|
ref = arg.variable.reference(scope) ref = arg.variable.reference(scope)
@ -194,7 +195,14 @@ module Ameba::AST
end end
private def record_macro?(node) 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 end
private def skip?(node) private def skip?(node)