Skip to content
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

Style/Lambda with unused argument bug #6956

Closed
rramsden opened this issue Apr 23, 2019 · 1 comment · Fixed by #6958
Closed

Style/Lambda with unused argument bug #6956

rramsden opened this issue Apr 23, 2019 · 1 comment · Fixed by #6958
Labels

Comments

@rramsden
Copy link

Context

When using --auto-correct with a "stabby lambda" function where arguments are unused results in syntactically incorrect code:

c = -> event do
  puts 'Hello world'
end

Is autocorrected to the following:

c = lambda(_) do |_event|
  puts 'Hello world'
end

Which returns the following error when executed:

test.rb:3:in `<main>': undefined local variable or method `_' for main:Object (NameError)

Expected behavior

Should not inject _ arguments into lambda e.g. lambda(_) this is not allowed in Ruby. The result should be the following code:

c = lambda do |_event|
  puts 'Hello world'
end

Actual behavior

Inserts _ as an argument to lambda function e.g. lambda(_)

Steps to reproduce the problem

  1. Insert the example code in the above context into a file.
  2. Run rubocop --auto-correct on the file.
  3. Try executing the formatted code, it will not work.

RuboCop version

$ [bundle exec] rubocop -V
0.67.2 (using Parser 2.6.2.1, running on ruby 2.4.5 x86_64-darwin18)

Ruby version

$ ruby -v
ruby 2.4.5p335 (2018-10-18 revision 65137) [x86_64-darwin18]
@Drenmi Drenmi added the bug label Apr 23, 2019
koic added a commit to koic/rubocop that referenced this issue Apr 23, 2019
…and `Lint/UnusedBlockArgument`

Fixes rubocop#6956.

This PR prevents auto-correct confliction of `Lint/Lambda` and `Lint/UnusedBlockArgument`.

The following is the reproduction procedure.

```ruby
# example.rb
c = -> event do
  puts 'Hello world'
end
```

```console
% rubocop example.rb -a --only Style/Lambda,Lint/UnusedBlockArgument
```

## Before

A reserved word `lambda` is broken.

```diff
% git diff
diff --git a/example.rb b/example.rb
index 3445c6f..b8e24f4 100644
--- a/example.rb
+++ b/example.rb
@@ -1,3 +1,3 @@
-c = -> event do
+c = lambda_ do |_event|
   puts 'Hello world'
 end
```

## After

It will be a valid code.

```diff
% git diff
diff --git a/example.rb b/example.rb
index 3445c6f..9c22b37 100644
--- a/example.rb
+++ b/example.rb
@@ -1,3 +1,3 @@
-c = -> event do
+c = lambda do |_event|
   puts 'Hello world'
 end
```
@koic
Copy link
Member

koic commented Apr 23, 2019

Thanks for your feedback. I opened a PR #6958.

bbatsov pushed a commit that referenced this issue Apr 23, 2019
…nt/UnusedBlockArgument`

Fixes #6956.

This PR prevents auto-correct confliction of `Lint/Lambda` and `Lint/UnusedBlockArgument`.

The following is the reproduction procedure.

```ruby
# example.rb
c = -> event do
  puts 'Hello world'
end
```

```console
% rubocop example.rb -a --only Style/Lambda,Lint/UnusedBlockArgument
```

## Before

A reserved word `lambda` is broken.

```diff
% git diff
diff --git a/example.rb b/example.rb
index 3445c6f..b8e24f4 100644
--- a/example.rb
+++ b/example.rb
@@ -1,3 +1,3 @@
-c = -> event do
+c = lambda_ do |_event|
   puts 'Hello world'
 end
```

## After

It will be a valid code.

```diff
% git diff
diff --git a/example.rb b/example.rb
index 3445c6f..9c22b37 100644
--- a/example.rb
+++ b/example.rb
@@ -1,3 +1,3 @@
-c = -> event do
+c = lambda do |_event|
   puts 'Hello world'
 end
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants