mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Add Lint/UnusedBlockArgument
rule
This commit is contained in:
parent
6ffb635dcc
commit
858557bc07
2 changed files with 172 additions and 0 deletions
110
spec/ameba/rule/lint/unused_block_argument_spec.cr
Normal file
110
spec/ameba/rule/lint/unused_block_argument_spec.cr
Normal file
|
@ -0,0 +1,110 @@
|
|||
require "../../../spec_helper"
|
||||
|
||||
module Ameba::Rule::Lint
|
||||
subject = UnusedBlockArgument.new
|
||||
|
||||
describe UnusedBlockArgument do
|
||||
it "doesn't report if it is an instance var argument" do
|
||||
s = Source.new %(
|
||||
class A
|
||||
def initialize(&@callback)
|
||||
end
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
end
|
||||
|
||||
it "doesn't report if anonymous" do
|
||||
s = Source.new %(
|
||||
def method(a, b, c, &)
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
end
|
||||
|
||||
it "doesn't report if argument name starts with a `_`" do
|
||||
s = Source.new %(
|
||||
def method(a, b, c, &_block)
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
end
|
||||
|
||||
it "doesn't report if it is a block and used" do
|
||||
s = Source.new %(
|
||||
def method(a, b, c, &block)
|
||||
block.call
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
end
|
||||
|
||||
it "reports if block arg is not used" do
|
||||
s = Source.new %(
|
||||
def method(a, b, c, &block)
|
||||
end
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
end
|
||||
|
||||
it "reports if unused and there is yield" do
|
||||
s = Source.new %(
|
||||
def method(a, b, c, &block)
|
||||
3.times do |i|
|
||||
i.try do
|
||||
yield i
|
||||
end
|
||||
end
|
||||
end
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
end
|
||||
|
||||
it "doesn't report if anonymous and there is yield" do
|
||||
s = Source.new %(
|
||||
def method(a, b, c, &)
|
||||
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
|
||||
def method(a, b, c, &block)
|
||||
super
|
||||
end
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
end
|
||||
|
||||
context "super" do
|
||||
it "reports if variable is not referenced implicitly by super" do
|
||||
s = Source.new %(
|
||||
class Bar < Foo
|
||||
def method(a, b, c, &block)
|
||||
super a, b, c
|
||||
end
|
||||
end
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
s.issues.first.message.should eq "Unused block argument `block`. If it's necessary, " \
|
||||
"use `_block` as an argument name to indicate " \
|
||||
"that it won't be used."
|
||||
end
|
||||
end
|
||||
|
||||
context "macro" do
|
||||
it "doesn't report if it is a used macro block argument" do
|
||||
s = Source.new %(
|
||||
macro my_macro(&block)
|
||||
{% block %}
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue