Skip to content

New Rule: Block not captured #158

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

Closed
j8r opened this issue Jun 19, 2020 · 8 comments · Fixed by #320
Closed

New Rule: Block not captured #158

j8r opened this issue Jun 19, 2020 · 8 comments · Fixed by #320
Assignees
Labels

Comments

@j8r
Copy link

j8r commented Jun 19, 2020

Anonymous block annotations are present since a bit of time now. Adding & shows that the method yields a block, but using &block either means that or that the block is captured.

Here is a simple example:

# Valid
def m(&block)
  block.call 1
end

# Also valid
def m(&block)
  callback = block
end

# Invalid
def m(&block)
  yield
end

# Correct syntax
def m(&)
  yield
end
@veelenga
Copy link
Member

Basically, we would like to report if there is a block argument and it is not used. Is this correct?

@Sija Sija added rule and removed feature labels Oct 30, 2022
@Sija Sija self-assigned this Dec 9, 2022
@Sija
Copy link
Member

Sija commented Dec 11, 2022

Hmm, this should be already covered by Lint/UnusedArgument rule 🤔

@straight-shoota
Copy link
Contributor

straight-shoota commented Dec 11, 2022

And I hope eventually by crystal tool format (crystal-lang/crystal#8764)

@Sija
Copy link
Member

Sija commented Dec 12, 2022

@veelenga For some reason it doesn't seem to work though 🤔

@veelenga
Copy link
Member

veelenga commented Dec 12, 2022

That's strange. It is covered by specs:

it "reports if block arg is not used" do
s = Source.new %(
def method(&block)
end
)
subject.catch(s).should_not be_valid
end
it "reports if unused and there is yield" do
s = Source.new %(
def method(&block)
yield 1
end
)
subject.catch(s).should_not be_valid
end

But you are right, it doesn't work on the example in the description.

@veelenga veelenga reopened this Dec 12, 2022
@Sija
Copy link
Member

Sija commented Dec 12, 2022

That's strange. It is covered by specs:

@veelenga I know, I was trying to understand wtf is goin' on there, but to no avail yet.

@veelenga
Copy link
Member

veelenga commented Dec 12, 2022

@Sija it ignores defs by default. There is a configuration property:

That was done because of this discussion (which makes sense) #52 (comment)

The rule still has to be tuned in order to properly support the example below:

def m(&)
  yield
end

currently it reports an issue:

[W] Lint/UnusedArgument: Unused argument ``. If it's necessary, use `_` as an argument name to indicate that it won't be used.
> def m(&)
        ^

@veelenga
Copy link
Member

veelenga commented Dec 12, 2022

Maybe it makes more sense to correct Lint/UnusedArgument rule in order to ignore an example with (&) argument and create a new rule that will specifically report the unused block argument.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants