summaryrefslogtreecommitdiff
path: root/rubocop
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2019-10-16 03:06:12 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2019-10-16 03:06:12 +0000
commitf155cc9034f2247c5d368f9b0212ad44248b0c5e (patch)
tree902480293b665d74a337aeae6a0521104f561988 /rubocop
parentc920712fab6abdc37de9444e6bbcd170c295b21a (diff)
downloadgitlab-ce-f155cc9034f2247c5d368f9b0212ad44248b0c5e.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r--rubocop/cop/gitlab/const_get_inherit_false.rb42
-rw-r--r--rubocop/rubocop.rb1
2 files changed, 43 insertions, 0 deletions
diff --git a/rubocop/cop/gitlab/const_get_inherit_false.rb b/rubocop/cop/gitlab/const_get_inherit_false.rb
new file mode 100644
index 00000000000..3d3bbc4c8d3
--- /dev/null
+++ b/rubocop/cop/gitlab/const_get_inherit_false.rb
@@ -0,0 +1,42 @@
+# frozen_string_literal: true
+
+module RuboCop
+ module Cop
+ module Gitlab
+ # Cop that encourages usage of inherit=false for 2nd argument when using const_get.
+ #
+ # See https://gitlab.com/gitlab-org/gitlab/issues/27678
+ class ConstGetInheritFalse < RuboCop::Cop::Cop
+ MSG = 'Use inherit=false when using const_get.'
+
+ def_node_matcher :const_get?, <<~PATTERN
+ (send _ :const_get ...)
+ PATTERN
+
+ def on_send(node)
+ return unless const_get?(node)
+ return if second_argument(node)&.false_type?
+
+ add_offense(node, location: :selector)
+ end
+
+ def autocorrect(node)
+ lambda do |corrector|
+ if arg = second_argument(node)
+ corrector.replace(arg.source_range, 'false')
+ else
+ first_argument = node.arguments[0]
+ corrector.insert_after(first_argument.source_range, ', false')
+ end
+ end
+ end
+
+ private
+
+ def second_argument(node)
+ node.arguments[1]
+ end
+ end
+ end
+ end
+end
diff --git a/rubocop/rubocop.rb b/rubocop/rubocop.rb
index 8e7df62ea75..70679aa1e78 100644
--- a/rubocop/rubocop.rb
+++ b/rubocop/rubocop.rb
@@ -1,3 +1,4 @@
+require_relative 'cop/gitlab/const_get_inherit_false'
require_relative 'cop/gitlab/module_with_instance_variables'
require_relative 'cop/gitlab/predicate_memoization'
require_relative 'cop/gitlab/httparty'