-
Notifications
You must be signed in to change notification settings - Fork 39
Add Lint/UnusedBlockArgument
rule
#320
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6ffb635
Add `Scope#yields?`
Sija 858557b
Add `Lint/UnusedBlockArgument` rule
Sija 7f6bd22
Do not report unused block arguments in `Lint/UnusedArgument` rule
Sija b6f3d41
Small refactor
Sija bb0c0ee
Consider `previous_def` implicitly accessing variables, in the same w…
Sija File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
module Ameba::Rule::Lint | ||
# A rule that reports unused block arguments. | ||
# For example, this is considered invalid: | ||
# | ||
# ``` | ||
# def foo(a, b, &block) | ||
# a + b | ||
# end | ||
# | ||
# def bar(&block) | ||
# yield 42 | ||
# end | ||
# ``` | ||
# | ||
# and should be written as: | ||
# | ||
# ``` | ||
# def foo(a, b, &_block) | ||
# a + b | ||
# end | ||
# | ||
# def bar(&) | ||
# yield 42 | ||
# end | ||
# ``` | ||
# | ||
# YAML configuration example: | ||
# | ||
# ``` | ||
# Lint/UnusedBlockArgument: | ||
# Enabled: true | ||
# ``` | ||
class UnusedBlockArgument < Base | ||
properties do | ||
description "Disallows unused block arguments" | ||
end | ||
|
||
MSG_UNUSED = "Unused block argument `%1$s`. If it's necessary, use `_%1$s` " \ | ||
"as an argument name to indicate that it won't be used." | ||
|
||
MSG_YIELDED = "Use `&` as an argument name to indicate that it won't be referenced." | ||
|
||
def test(source) | ||
AST::ScopeVisitor.new self, source | ||
end | ||
|
||
def test(source, node : Crystal::Def, scope : AST::Scope) | ||
return unless block_arg = node.block_arg | ||
return unless block_arg = scope.arguments.find(&.node.== block_arg) | ||
|
||
return if block_arg.anonymous? | ||
return if scope.references?(block_arg.variable) | ||
|
||
if scope.yields? | ||
issue_for block_arg.node, MSG_YIELDED | ||
else | ||
return if block_arg.ignored? | ||
issue_for block_arg.node, MSG_UNUSED % block_arg.name | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we use that newer matcher
expect_no_issues
/expect_issue
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, we should but I was too lazy to rewrite those, xmas & all that jazz, sorry!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are still many specs to rewrite anyway...