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

[False positive] Performance/RedundantMatch on #match with block #2715

Closed
TheLonelyGhost opened this issue Jan 25, 2016 · 0 comments
Closed

Comments

@TheLonelyGhost
Copy link

$ rubocop -V
0.36.0 (using Parser 2.3.0.1, running on ruby 2.2.3 x86_64-darwin15)

Example code:

# Setter that parses out array-like syntax from the given `attr`
# EXAMPLE:
#
#     foo = {}
#     foo['derp'] = [ 'hello', 'world' ]
#     assign(foo, 'derp[1]', 'TheLonelyGhost')
#     foo['derp']
#     # => ['hello', 'TheLonelyGhost']
#
def assign(obj, attr, value)
  key = attr
  me = obj

  /^(?<hash_key>.+)\[(?<array_key>[0-9]+)\]$/.match(key) do |m|
    key = m[:array_key].to_i
    obj[m[:hash_key]] ||= []

    me = obj[m[:hash_key]]
  end

  me[key] = value
end

Rubocop Errors thrown:

C: Performance/RedundantMatch: Use `=~` in places where the `MatchData` returned by `#match` will not be used.

Problem:

This is incorrect. MatchData is being used here inside of the block, as per the example in Ruby Docs

If a block is given, invoke the block with MatchData if match succeed, so that you can write
pat.match(str) {|m| ...}
instead of

Workaround:

Add the following to .rubocop.yml:

Performance/RedundantMatch:
  Enabled: false
@bbatsov bbatsov closed this as completed in 81ae272 Feb 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant