Skip to content

Commit

Permalink
Merge pull request #450 from crystal-ameba/fix-issue-447
Browse files Browse the repository at this point in the history
Exclude reporting type declarations passed as call arguments
  • Loading branch information
Sija committed Jan 18, 2024
2 parents 28fafea + a79e711 commit 7f50ff9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
16 changes: 13 additions & 3 deletions spec/ameba/rule/lint/useless_assign_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -388,19 +388,29 @@ module Ameba::Rule::Lint
CRYSTAL
end

it "doesn't report if this is a record declaration" do
it "doesn't report record declaration" do
expect_no_issues subject, <<-CRYSTAL
record Foo, foo : String
record Foo, foo = "foo"
CRYSTAL
end

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

it "doesn't report if this is an accessor declaration" do
it "doesn't report type declaration as a call argument" do
expect_no_issues subject, <<-CRYSTAL
foo Foo(T), foo : T
foo Foo, foo : Nil
foo foo : String, bar : Int32?
CRYSTAL
end

it "doesn't report accessor declarations" do
accessor_macros = %w[setter class_setter]
%w[getter class_getter property class_property].each do |name|
accessor_macros << name
Expand Down
9 changes: 7 additions & 2 deletions src/ameba/rule/lint/useless_assign.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ module Ameba::Rule::Lint

scope.variables.each do |var|
next if var.ignored? || var.used_in_macro? || var.captured_by_block?
next if exclude_type_declarations? && scope.assigns_type_dec?(var.name)

if scope.assigns_type_dec?(var.name)
next if exclude_type_declarations?
# exclude type declarations within calls
if node.is_a?(Crystal::Expressions)
next if node.expressions.first?.is_a?(Crystal::Call)
end
end
var.assignments.each do |assign|
next if assign.referenced?
issue_for assign.target_node, MSG % var.name
Expand Down

0 comments on commit 7f50ff9

Please sign in to comment.