From a79e711fae731ba5af0730697a32bb9d2ffa40dc Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Thu, 18 Jan 2024 00:34:28 +0100 Subject: [PATCH] Exclude reporting type declarations passed as call arguments --- spec/ameba/rule/lint/useless_assign_spec.cr | 16 +++++++++++++--- src/ameba/rule/lint/useless_assign.cr | 9 +++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/spec/ameba/rule/lint/useless_assign_spec.cr b/spec/ameba/rule/lint/useless_assign_spec.cr index a37d8110..33f32213 100644 --- a/spec/ameba/rule/lint/useless_assign_spec.cr +++ b/spec/ameba/rule/lint/useless_assign_spec.cr @@ -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 diff --git a/src/ameba/rule/lint/useless_assign.cr b/src/ameba/rule/lint/useless_assign.cr index 900e5d5e..fbf0c0d3 100644 --- a/src/ameba/rule/lint/useless_assign.cr +++ b/src/ameba/rule/lint/useless_assign.cr @@ -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