diff --git a/.rubocop.yml b/.rubocop.yml index f1c83c7d773..852b2f71932 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -130,3 +130,8 @@ RSpec/EmptyExampleGroup: - 'spec/rubocop/cop/lint/script_permission_spec.rb' - 'spec/rubocop/cop/style/empty_else_spec.rb' - 'spec/rubocop/result_cache_spec.rb' + +Performance/CollectionLiteralInLoop: + Exclude: + - 'Rakefile' + - 'spec/**/*.rb' diff --git a/Gemfile b/Gemfile index 474776f05f6..3fdb9d4c734 100644 --- a/Gemfile +++ b/Gemfile @@ -9,7 +9,7 @@ gem 'memory_profiler', platform: :mri gem 'pry' gem 'rake', '~> 13.0' gem 'rspec', '~> 3.7' -gem 'rubocop-performance', '~> 1.7.0' +gem 'rubocop-performance', '~> 1.8.1' gem 'rubocop-rspec', '~> 1.43.2' # Workaround for cc-test-reporter with SimpleCov 0.18. # Stop upgrading SimpleCov until the following issue will be resolved. diff --git a/lib/rubocop/config_validator.rb b/lib/rubocop/config_validator.rb index 81fb923e69e..43b3673037a 100644 --- a/lib/rubocop/config_validator.rb +++ b/lib/rubocop/config_validator.rb @@ -18,6 +18,11 @@ class ConfigValidator # @api private NEW_COPS_VALUES = %w[pending disable enable].freeze + # @api private + CONFIG_CHECK_KEYS = %w[Enabled Safe SafeAutoCorrect AutoCorrect].to_set.freeze + CONFIG_CHECK_DEPARTMENTS = %w[pending override_department].freeze + private_constant :CONFIG_CHECK_KEYS, :CONFIG_CHECK_DEPARTMENTS + def_delegators :@config, :smart_loaded_path, :for_all_cops def initialize(config) @@ -202,13 +207,9 @@ def check_cop_config_value(hash, parent = nil) hash.each do |key, value| check_cop_config_value(value, key) if value.is_a?(Hash) - next unless %w[Enabled - Safe - SafeAutoCorrect - AutoCorrect].include?(key) && value.is_a?(String) + next unless CONFIG_CHECK_KEYS.include?(key) && value.is_a?(String) - next if key == 'Enabled' && - %w[pending override_department].include?(value) + next if key == 'Enabled' && CONFIG_CHECK_DEPARTMENTS.include?(value) raise ValidationError, msg_not_boolean(parent, key, value) end diff --git a/lib/rubocop/cop/metrics/parameter_lists.rb b/lib/rubocop/cop/metrics/parameter_lists.rb index 3c3dcb30ee3..cc25ec439d1 100644 --- a/lib/rubocop/cop/metrics/parameter_lists.rb +++ b/lib/rubocop/cop/metrics/parameter_lists.rb @@ -12,6 +12,9 @@ class ParameterLists < Base MSG = 'Avoid parameter lists longer than %d parameters. ' \ '[%d/%d]' + NAMED_KEYWORD_TYPES = %i[kwoptarg kwarg].freeze + private_constant :NAMED_KEYWORD_TYPES + def on_args(node) count = args_count(node) return unless count > max_params @@ -33,7 +36,7 @@ def args_count(node) if count_keyword_args? node.children.size else - node.children.count { |a| !%i[kwoptarg kwarg].include?(a.type) } + node.children.count { |a| !NAMED_KEYWORD_TYPES.include?(a.type) } end end diff --git a/lib/rubocop/cop/style/trailing_underscore_variable.rb b/lib/rubocop/cop/style/trailing_underscore_variable.rb index ef5ec64179e..5b8c7b4f771 100644 --- a/lib/rubocop/cop/style/trailing_underscore_variable.rb +++ b/lib/rubocop/cop/style/trailing_underscore_variable.rb @@ -36,6 +36,8 @@ class TrailingUnderscoreVariable < Base MSG = 'Do not use trailing `_`s in parallel assignment. ' \ 'Prefer `%s`.' UNDERSCORE = '_' + DISALLOW = %i[lvasgn splat].freeze + private_constant :DISALLOW def on_masgn(node) ranges = unneeded_ranges(node) @@ -64,7 +66,7 @@ def find_first_offense(variables) def find_first_possible_offense(variables) variables.reduce(nil) do |offense, variable| - break offense unless %i[lvasgn splat].include?(variable.type) + break offense unless DISALLOW.include?(variable.type) var, = *variable var, = *var diff --git a/lib/rubocop/formatter/offense_count_formatter.rb b/lib/rubocop/formatter/offense_count_formatter.rb index 96995e3ae7c..b805314eeee 100644 --- a/lib/rubocop/formatter/offense_count_formatter.rb +++ b/lib/rubocop/formatter/offense_count_formatter.rb @@ -67,7 +67,7 @@ def ordered_offense_counts(offense_counts) end def total_offense_count(offense_counts) - offense_counts.values.inject(0, :+) + offense_counts.values.sum end end end diff --git a/lib/rubocop/formatter/worst_offenders_formatter.rb b/lib/rubocop/formatter/worst_offenders_formatter.rb index d3a73921d8a..31d44da2328 100644 --- a/lib/rubocop/formatter/worst_offenders_formatter.rb +++ b/lib/rubocop/formatter/worst_offenders_formatter.rb @@ -55,7 +55,7 @@ def ordered_offense_counts(offense_counts) end def total_offense_count(offense_counts) - offense_counts.values.inject(0, :+) + offense_counts.values.sum end end end diff --git a/lib/rubocop/options.rb b/lib/rubocop/options.rb index 12e8ce9ead8..2d679b18b4c 100644 --- a/lib/rubocop/options.rb +++ b/lib/rubocop/options.rb @@ -242,6 +242,9 @@ def long_opt_symbol(args) # @api private class OptionsValidator class << self + SYNTAX_DEPARTMENTS = %w[Syntax Lint/Syntax].freeze + private_constant :SYNTAX_DEPARTMENTS + # Cop name validation must be done later than option parsing, so it's not # called from within Options. def validate_cop_list(names) @@ -253,7 +256,7 @@ def validate_cop_list(names) names.each do |name| next if cop_names.include?(name) next if departments.include?(name) - next if %w[Syntax Lint/Syntax].include?(name) + next if SYNTAX_DEPARTMENTS.include?(name) raise IncorrectCopNameError, format_message_from(name, cop_names) end