diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-27 15:09:50 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-27 15:09:50 +0300 |
commit | 8827c1d18473a3d84db181c89212975418a25e37 (patch) | |
tree | 3f16c4b02b0b46fb2f501a6d5e9fc0f93f1a406f /rubocop | |
parent | 43b35a88ca54379d8df4dad84b5a84f9eecae3f2 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/avoid_becomes.rb | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/rubocop/cop/avoid_becomes.rb b/rubocop/cop/avoid_becomes.rb new file mode 100644 index 00000000000..cfd6b3d2164 --- /dev/null +++ b/rubocop/cop/avoid_becomes.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + # Cop that blacklists the use of ".becomes(SomeConstant)". + # + # The use of becomes() will result in a new object being created, throwing + # away any eager loaded assocations. This in turn can cause N+1 query + # problems, even when a developer eager loaded all necessary associations. + # + # See https://gitlab.com/gitlab-org/gitlab/-/issues/23182 for more information. + class AvoidBecomes < RuboCop::Cop::Cop + MSG = 'Avoid the use of becomes(SomeConstant), as this creates a ' \ + 'new object and throws away any eager loaded associations. ' \ + 'When creating URLs in views, just use the path helpers directly. ' \ + 'For example, instead of `link_to(..., [group.becomes(Namespace), ...])` ' \ + 'use `link_to(..., namespace_foo_path(group, ...))`. Most of the time there is no ' \ + 'need to pass in namespace to the path helpers after implementaton of ' \ + 'https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/12566' + + def_node_matcher :becomes?, <<~PATTERN + (send {send ivar lvar} :becomes ...) + PATTERN + + def on_send(node) + add_offense(node, location: :expression) if becomes?(node) + end + end + end +end |