Skip to content

Commit

Permalink
Merge pull request #346 from koic/fix_false_positive_for_dynamic_find_by
Browse files Browse the repository at this point in the history
Fix a false positive for `Rails/DynamicFindBy`
  • Loading branch information
koic authored Sep 14, 2020
2 parents 13a0fbe + 01036ab commit 3ec7cda
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [#338](https://github.com/rubocop-hq/rubocop-rails/issues/338): Fix a false positive for `Rails/IndexBy` and `Rails/IndexWith` when the `each_with_object` hash is used in the transformed key or value. ([@eugeneius][])
* [#351](https://github.com/rubocop-hq/rubocop-rails/pull/351): Add `<>` operator to `Rails/WhereNot` cop. ([@Tietew][])
* [#352](https://github.com/rubocop-hq/rubocop-rails/pull/352): Do not register offense if given a splatted hash. ([@dvandersluis][])
* [#346](https://github.com/rubocop-hq/rubocop-rails/pull/346): Fix a false positive for `Rails/DynamicFindBy` when any of the arguments are splat argument. ([@koic][])

## 2.8.0 (2020-09-04)

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rails/dynamic_find_by.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def on_send(node)
method_name = node.method_name
static_name = static_method_name(method_name)
return unless static_name
return if node.arguments.any?(&:splat_type?)

add_offense(node,
message: format(MSG, static_name: static_name,
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/rails/dynamic_find_by_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@
expect_no_offenses('User.find_by(name: name)')
end

it 'accepts splat argument' do
expect_no_offenses('User.find_by_scan(*args)')
end

it 'accepts any of the arguments are splat argument' do
expect_no_offenses('User.find_by_foo_and_bar(arg, *args)')
end

it 'accepts method in whitelist' do
expect_no_offenses(<<~RUBY)
User.find_by_sql(["select * from users where name = ?", name])
Expand Down

0 comments on commit 3ec7cda

Please sign in to comment.