diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-07-20 15:40:28 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-07-20 15:40:28 +0000 |
commit | b595cb0c1dec83de5bdee18284abe86614bed33b (patch) | |
tree | 8c3d4540f193c5ff98019352f554e921b3a41a72 /rubocop | |
parent | 2f9104a328fc8a4bddeaa4627b595166d24671d0 (diff) | |
download | gitlab-ce-b595cb0c1dec83de5bdee18284abe86614bed33b.tar.gz |
Add latest changes from gitlab-org/gitlab@15-2-stable-eev15.2.0-rc42
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/database/rescue_query_canceled.rb | 46 | ||||
-rw-r--r-- | rubocop/cop/database/rescue_statement_timeout.rb | 46 | ||||
-rw-r--r-- | rubocop/cop/gitlab/namespaced_class.rb | 4 |
3 files changed, 94 insertions, 2 deletions
diff --git a/rubocop/cop/database/rescue_query_canceled.rb b/rubocop/cop/database/rescue_query_canceled.rb new file mode 100644 index 00000000000..1238f9ed911 --- /dev/null +++ b/rubocop/cop/database/rescue_query_canceled.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module Database + # Checks for `rescue` blocks targeting the `ActiveRecord::QueryCanceled` class. + # + # @example + # + # # bad + # + # begin + # run_an_expensive_long_query + # rescue ActiveRecord::QueryCanceled + # try_something_else + # end + # + # @example + # + # # good + # + # run_cheap_queries_with_each_batch + class RescueQueryCanceled < RuboCop::Cop::Cop + MSG = <<~EOF + Avoid rescuing the `ActiveRecord::QueryCanceled` class. + + Using this pattern should be a very rare exception or a temporary patch only. + Consider refactoring using less expensive queries and `each_batch`. + EOF + + def on_resbody(node) + return unless node.children.first + + rescue_args = node.children.first.children + return unless rescue_args.any? { |a| targets_exception?(a) } + + add_offense(node) + end + + def targets_exception?(rescue_arg_node) + rescue_arg_node.const_name == 'ActiveRecord::QueryCanceled' + end + end + end + end +end diff --git a/rubocop/cop/database/rescue_statement_timeout.rb b/rubocop/cop/database/rescue_statement_timeout.rb new file mode 100644 index 00000000000..0b75a441e29 --- /dev/null +++ b/rubocop/cop/database/rescue_statement_timeout.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module Database + # Checks for `rescue` blocks targeting the `ActiveRecord::StatementTimeout` class. + # + # @example + # + # # bad + # + # begin + # run_an_expensive_long_query + # rescue ActiveRecord::StatementTimeout + # try_something_else + # end + # + # @example + # + # # good + # + # run_cheap_queries_with_each_batch + class RescueStatementTimeout < RuboCop::Cop::Cop + MSG = <<~EOF + Avoid rescuing the `ActiveRecord::StatementTimeout` class. + + Using this pattern should be a very rare exception or a temporary patch only. + Consider refactoring using less expensive queries and `each_batch`. + EOF + + def on_resbody(node) + return unless node.children.first + + rescue_args = node.children.first.children + return unless rescue_args.any? { |a| targets_exception?(a) } + + add_offense(node) + end + + def targets_exception?(rescue_arg_node) + rescue_arg_node.const_name == 'ActiveRecord::StatementTimeout' + end + end + end + end +end diff --git a/rubocop/cop/gitlab/namespaced_class.rb b/rubocop/cop/gitlab/namespaced_class.rb index ce46e055c21..07e3fb8975f 100644 --- a/rubocop/cop/gitlab/namespaced_class.rb +++ b/rubocop/cop/gitlab/namespaced_class.rb @@ -34,7 +34,7 @@ module RuboCop # # class Gitlab::MyDomain::MyClass # end - class NamespacedClass < RuboCop::Cop::Cop + class NamespacedClass < RuboCop::Cop::Base MSG = 'Classes must be declared inside a module indicating a product domain namespace. For more info: https://gitlab.com/gitlab-org/gitlab/-/issues/321982' # These namespaces are considered top-level semantically. @@ -51,7 +51,7 @@ module RuboCop # Remove class name because it's not a domain namespace. add_potential_domain_namespace(node) { _1.pop } - add_offense(node) if domain_namespaces.none? + add_offense(node.loc.name) if domain_namespaces.none? end private |