Skip to content
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

Exclude reporting type declarations passed as call arguments #450

Merged
merged 1 commit into from
Jan 18, 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
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