From 517caf24298fca4162127673d089246748ff3a5a Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 2 Apr 2021 20:14:21 +0900 Subject: [PATCH] Mark `Rails/WhereExists` as unsafe auto-correction ## Summary This cop is unsafe because the behavior may change on the following case: ```ruby Author.includes(:articles).where(articles: {id: id}).exists? #=> Perform `eager_load` behaviour (`LEFT JOIN` query) and get result. Author.includes(:articles).exists?(articles: {id: id}) #=> Perform `preload` behaviour and `ActiveRecord::StatementInvalid` error occurs. ``` ## Additional Information And this cop may be considered disabled by default as there is no consensus on the default style. - https://github.com/rubocop/rubocop-rails/pull/286 - https://github.com/rubocop/rubocop-rails/pull/342 --- config/default.yml | 1 + docs/modules/ROOT/pages/cops_rails.adoc | 13 ++++++++++++- lib/rubocop/cop/rails/where_exists.rb | 11 +++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/config/default.yml b/config/default.yml index 09a4eecf0f..8a65db9a77 100644 --- a/config/default.yml +++ b/config/default.yml @@ -770,6 +770,7 @@ Rails/WhereEquals: Rails/WhereExists: Description: 'Prefer `exists?(...)` over `where(...).exists?`.' Enabled: 'pending' + SafeAutoCorrect: false EnforcedStyle: exists SupportedStyles: - exists diff --git a/docs/modules/ROOT/pages/cops_rails.adoc b/docs/modules/ROOT/pages/cops_rails.adoc index 527b51f230..e23879d844 100644 --- a/docs/modules/ROOT/pages/cops_rails.adoc +++ b/docs/modules/ROOT/pages/cops_rails.adoc @@ -4512,7 +4512,7 @@ User.where(users: { name: 'Gabe' }) | Pending | Yes -| Yes +| Yes (Unsafe) | 2.7 | 2.8 |=== @@ -4525,6 +4525,17 @@ then the cop enforces `exists?(...)` over `where(...).exists?`. When EnforcedStyle is 'where' then the cop enforces `where(...).exists?` over `exists?(...)`. +This cop is unsafe auto-correction because the behavior may change on the following case: + +[source,ruby] +---- +Author.includes(:articles).where(articles: {id: id}).exists? +#=> Perform `eager_load` behaviour (`LEFT JOIN` query) and get result. + +Author.includes(:articles).exists?(articles: {id: id}) +#=> Perform `preload` behaviour and `ActiveRecord::StatementInvalid` error occurs. +---- + === Examples ==== EnforcedStyle: exists (default) diff --git a/lib/rubocop/cop/rails/where_exists.rb b/lib/rubocop/cop/rails/where_exists.rb index 2c40e2312a..8e78bb2db8 100644 --- a/lib/rubocop/cop/rails/where_exists.rb +++ b/lib/rubocop/cop/rails/where_exists.rb @@ -11,6 +11,17 @@ module Rails # When EnforcedStyle is 'where' then the cop enforces # `where(...).exists?` over `exists?(...)`. # + # This cop is unsafe auto-correction because the behavior may change on the following case: + # + # [source,ruby] + # ---- + # Author.includes(:articles).where(articles: {id: id}).exists? + # #=> Perform `eager_load` behaviour (`LEFT JOIN` query) and get result. + # + # Author.includes(:articles).exists?(articles: {id: id}) + # #=> Perform `preload` behaviour and `ActiveRecord::StatementInvalid` error occurs. + # ---- + # # @example EnforcedStyle: exists (default) # # bad # User.where(name: 'john').exists?