Skip to content

Do not report anonymous block arguments in Lint/UnusedArgument #319

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

Merged
merged 1 commit into from
Dec 12, 2022
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
9 changes: 9 additions & 0 deletions spec/ameba/rule/lint/unused_argument_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/ameba/ast/variabling/argument.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/ameba/rule/lint/unused_argument.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down