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

Rails/Delegate false positive (arguments ignored) #5393

Closed
ryym opened this issue Jan 5, 2018 · 0 comments
Closed

Rails/Delegate false positive (arguments ignored) #5393

ryym opened this issue Jan 5, 2018 · 0 comments
Assignees
Labels

Comments

@ryym
Copy link

ryym commented Jan 5, 2018

Rails/Delegate warns incorrectly about a method call with arguments.

Sample code (a.rb):

# frozen_string_literal: true

# A
class A
  def b; end
end

# Foo
class Foo
  def a(n)
    p n
    A.new
  end

  def b
    a(1).b
  end
end

Foo.new.b

Expected behavior

rubocop a.rb reports no offenses.

Actual behavior

It reports an offense like this:

a.rb:13:3: C: Rails/Delegate: Use delegate to define delegations.
  def b
  ^^^

But obviously Foo#b method cannot be delegate to a.b. In fact, auto-fixed code raises an error.

$ bin/rails runner "load 'a.rb'"
1

$ rubocop -a a.rb
$ bin/rails runner "load 'a.rb'"
a.rb:10:in `a': wrong number of arguments (given 0, expected 1) (ArgumentError)
        from a.rb:15:in `b'
        ...

Auto-fixed code:

 # frozen_string_literal: true
 
 # A
 class A
   def b; end
 end
 
 # Foo
 class Foo
   def a(n)
     p n
     A.new
   end
 
-  def b
-    a(1).b
-  end
+  delegate :b, to: :a
 end
 
 Foo.new.b

Steps to reproduce the problem

Run rubocop a.rb using this .rubocop.yml:

AllCops:
  TargetRailsVersion: 5.1

Rails:
  Enabled: true

RuboCop version

$ rubocop -V
0.52.1 (using Parser 2.4.0.2, running on ruby 2.4.2 x86_64-darwin16)
@pocke pocke added the bug label Jan 5, 2018
@pocke pocke self-assigned this Jan 5, 2018
pocke added a commit to pocke/rubocop that referenced this issue Jan 7, 2018
@bbatsov bbatsov closed this as completed in b0e6b81 Jan 7, 2018
koic added a commit to koic/rubocop that referenced this issue Jan 7, 2018
Follow up of rubocop#5410.

This PR fixes the following error on Travis CI.

```console
% bundle exec rspec ./spec/project_spec.rb
Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}

Randomized with seed 45488
..............F.F

Failures:

  1) RuboCop Project changelog entry body ends with a punctuation
     Failure/Error: expect(bodies).to all(match(/[\.\!]$/))

       expected ["Add new `` cop.", "Add new `` cop.", "Add new ``
       cop.", "Add new `` cop.", "Add new `` cop.", "Add ...nclosed in
       braces are not noticed.", "Received malformed format string
       ArgumentError from rubocop."] to all match /[\.\!]$/

          object at index 18 failed to match:
             expected "Fix ``'s false positive with a method
             call with arguments. ([@pocke][]) " to match /[\.\!]$/
     # ./spec/project_spec.rb:174:in `block (5 levels) in <top (required)>'

  2) RuboCop Project changelog entry after version 0.14.0 has a link to
  the contributors at the end
     Failure/Error: expect(entries).to all(match(/\(\[@\S+\]\[\](?:, \[@\S+\]\[\])*\)$/))

       expected ["* [rubocop#3666](rubocop#3666): Add new
       `Naming/UncommunicativeBlockPara...552): `RaiseArgs` allows
       exception constructor calls with more than one 1
       argument. ([@bbatsov][])"] to all match /\(\[@\S+\]\[\](?:, \[@\S+\]\[\])*\)$/

          object at index 18 failed to match:
             expected "* [rubocop#5393](rubocop#5393):
             Fix `Rails/Delegate`'s false positive with a
             method call with arguments. ([@pocke][]) " to
             match /\(\[@\S+\]\[\](?:, \[@\S+\]\[\])*\)$/
     # ./spec/project_spec.rb:123:in `block (5 levels) in <top (required)>'

Finished in 1.36 seconds (files took 1.07 seconds to load)
17 examples, 2 failures

Failed examples:

rspec ./spec/project_spec.rb:173 # RuboCop Project changelog entry body
ends with a punctuation
rspec ./spec/project_spec.rb:122 # RuboCop Project changelog entry after
version 0.14.0 has a link to the contributors at the end
```

https://travis-ci.org/bbatsov/rubocop/jobs/325980721#L1146-L1162
bbatsov pushed a commit that referenced this issue Jan 7, 2018
Follow up of #5410.

This PR fixes the following error on Travis CI.

```console
% bundle exec rspec ./spec/project_spec.rb
Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}

Randomized with seed 45488
..............F.F

Failures:

  1) RuboCop Project changelog entry body ends with a punctuation
     Failure/Error: expect(bodies).to all(match(/[\.\!]$/))

       expected ["Add new `` cop.", "Add new `` cop.", "Add new ``
       cop.", "Add new `` cop.", "Add new `` cop.", "Add ...nclosed in
       braces are not noticed.", "Received malformed format string
       ArgumentError from rubocop."] to all match /[\.\!]$/

          object at index 18 failed to match:
             expected "Fix ``'s false positive with a method
             call with arguments. ([@pocke][]) " to match /[\.\!]$/
     # ./spec/project_spec.rb:174:in `block (5 levels) in <top (required)>'

  2) RuboCop Project changelog entry after version 0.14.0 has a link to
  the contributors at the end
     Failure/Error: expect(entries).to all(match(/\(\[@\S+\]\[\](?:, \[@\S+\]\[\])*\)$/))

       expected ["* [#3666](#3666): Add new
       `Naming/UncommunicativeBlockPara...552): `RaiseArgs` allows
       exception constructor calls with more than one 1
       argument. ([@bbatsov][])"] to all match /\(\[@\S+\]\[\](?:, \[@\S+\]\[\])*\)$/

          object at index 18 failed to match:
             expected "* [#5393](#5393):
             Fix `Rails/Delegate`'s false positive with a
             method call with arguments. ([@pocke][]) " to
             match /\(\[@\S+\]\[\](?:, \[@\S+\]\[\])*\)$/
     # ./spec/project_spec.rb:123:in `block (5 levels) in <top (required)>'

Finished in 1.36 seconds (files took 1.07 seconds to load)
17 examples, 2 failures

Failed examples:

rspec ./spec/project_spec.rb:173 # RuboCop Project changelog entry body
ends with a punctuation
rspec ./spec/project_spec.rb:122 # RuboCop Project changelog entry after
version 0.14.0 has a link to the contributors at the end
```

https://travis-ci.org/bbatsov/rubocop/jobs/325980721#L1146-L1162
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants