From 9df66e890b29f273fc8390c058ac80a61bacec17 Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Mon, 12 Dec 2022 18:14:49 +0100 Subject: [PATCH] Do not report anonymous block arguments in `Lint/UnusedArgument` --- spec/ameba/rule/lint/unused_argument_spec.cr | 9 +++++++++ src/ameba/ast/variabling/argument.cr | 7 ++++++- src/ameba/rule/lint/unused_argument.cr | 3 ++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/spec/ameba/rule/lint/unused_argument_spec.cr b/spec/ameba/rule/lint/unused_argument_spec.cr index d147384e..125ff5f8 100644 --- a/spec/ameba/rule/lint/unused_argument_spec.cr +++ b/spec/ameba/rule/lint/unused_argument_spec.cr @@ -132,6 +132,15 @@ module Ameba::Rule::Lint subject.catch(s).should_not be_valid end + it "doesn't report if it's an anonymous block" do + s = Source.new %( + def method(&) + yield 1 + end + ) + subject.catch(s).should be_valid + end + it "doesn't report if variable is referenced implicitly" do s = Source.new %( class Bar < Foo diff --git a/src/ameba/ast/variabling/argument.cr b/src/ameba/ast/variabling/argument.cr index 1b99d812..59b54661 100644 --- a/src/ameba/ast/variabling/argument.cr +++ b/src/ameba/ast/variabling/argument.cr @@ -31,7 +31,12 @@ module Ameba::AST def initialize(@node, @variable) end - # Returns `true` if the name starts with '_', `false` if not. + # Returns `true` if the `name` is empty, `false` otherwise. + def anonymous? + name.blank? + end + + # Returns `true` if the `name` starts with '_', `false` otherwise. def ignored? name.starts_with? '_' end diff --git a/src/ameba/rule/lint/unused_argument.cr b/src/ameba/rule/lint/unused_argument.cr index 806b68ad..66879d27 100644 --- a/src/ameba/rule/lint/unused_argument.cr +++ b/src/ameba/rule/lint/unused_argument.cr @@ -55,7 +55,8 @@ module Ameba::Rule::Lint private def find_unused_arguments(source, scope) scope.arguments.each do |argument| - next if argument.ignored? || scope.references?(argument.variable) + next if argument.anonymous? || argument.ignored? + next if scope.references?(argument.variable) name_suggestion = scope.node.is_a?(Crystal::Block) ? '_' : "_#{argument.name}" issue_for argument.node, MSG % {argument.name, name_suggestion}