rubocop 1.42.0 → 1.43.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e37ffc01dbd2782dd811d4c35129f893502179457e76d8e3501d7c3683563cc2
4
- data.tar.gz: 9dba5be8e2168e986170e8366a62c73940262bfffec0a374c3a01b3f207ca291
3
+ metadata.gz: adbae8e6d8a8ad828f302b9d3bba545a8b7fe54727480077a1878d4c2109dbf3
4
+ data.tar.gz: fb30d554306879712c48bec3d389b4232ee8f8dde6d410dd139b73796a076d3d
5
5
  SHA512:
6
- metadata.gz: 23769dc5239a734dc07a859a594e1cc0b732d05d24c61c1887525f9f69f748e0c23cbb84404e865e00a04bfb5c91e73d1e0bdb7908a72340ede4245fc38508c6
7
- data.tar.gz: f016891fb54e93a8f6c418d4aa97f7400a74c187c8cf5dbbe274020ea5bda900721f2a7fefbbb368472664cf21a4b586db4fb9373510c27da6428aa490d504e6
6
+ metadata.gz: 7aa85cf4d34b4994422b5aa921d746b68bc282c97f7563affa2b2574a9b1102eefce4e10bed58801bb6ef9fb2ee2d3d563a63f5b114d754a69c872f6830a793e
7
+ data.tar.gz: 1e088db8ccb3cdcf2ab7b9e624b68bfc4513681e4cc8ee6bd69084dfa91fda775fc28483a4764962829230d6d2fd5726855299ba1ae622bf85cc66f55451c8c9
data/README.md CHANGED
@@ -53,7 +53,7 @@ To prevent an unwanted RuboCop update you might want to use a conservative versi
53
53
  in your `Gemfile`:
54
54
 
55
55
  ```rb
56
- gem 'rubocop', '~> 1.42', require: false
56
+ gem 'rubocop', '~> 1.43', require: false
57
57
  ```
58
58
 
59
59
  See [our versioning policy](https://docs.rubocop.org/rubocop/versioning.html) for further details.
data/config/default.yml CHANGED
@@ -2452,6 +2452,11 @@ Lint/UselessMethodDefinition:
2452
2452
  VersionChanged: '0.91'
2453
2453
  Safe: false
2454
2454
 
2455
+ Lint/UselessRescue:
2456
+ Description: 'Checks for useless `rescue`s.'
2457
+ Enabled: pending
2458
+ VersionAdded: '1.43'
2459
+
2455
2460
  Lint/UselessRuby2Keywords:
2456
2461
  Description: 'Finds unnecessary uses of `ruby2_keywords`.'
2457
2462
  Enabled: pending
@@ -3853,7 +3858,8 @@ Style/HashEachMethods:
3853
3858
  Safe: false
3854
3859
  VersionAdded: '0.80'
3855
3860
  VersionChanged: '1.16'
3856
- AllowedReceivers: []
3861
+ AllowedReceivers:
3862
+ - Thread.current
3857
3863
 
3858
3864
  Style/HashExcept:
3859
3865
  Description: >-
@@ -5440,9 +5446,10 @@ Style/YodaCondition:
5440
5446
 
5441
5447
  Style/YodaExpression:
5442
5448
  Description: 'Forbid the use of yoda expressions.'
5443
- Enabled: pending
5449
+ Enabled: false
5444
5450
  Safe: false
5445
5451
  VersionAdded: '1.42'
5452
+ VersionChanged: '1.43'
5446
5453
  SupportedOperators:
5447
5454
  - '*'
5448
5455
  - '+'
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ # Checks for useless `rescue`s, which only reraise rescued exceptions.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # def foo
11
+ # do_something
12
+ # rescue
13
+ # raise
14
+ # end
15
+ #
16
+ # # bad
17
+ # def foo
18
+ # do_something
19
+ # rescue => e
20
+ # raise # or 'raise e', or 'raise $!', or 'raise $ERROR_INFO'
21
+ # end
22
+ #
23
+ # # good
24
+ # def foo
25
+ # do_something
26
+ # rescue
27
+ # do_cleanup
28
+ # raise
29
+ # end
30
+ #
31
+ # # bad (latest rescue)
32
+ # def foo
33
+ # do_something
34
+ # rescue ArgumentError
35
+ # # noop
36
+ # rescue
37
+ # raise
38
+ # end
39
+ #
40
+ # # good (not the latest rescue)
41
+ # def foo
42
+ # do_something
43
+ # rescue ArgumentError
44
+ # raise
45
+ # rescue
46
+ # # noop
47
+ # end
48
+ #
49
+ class UselessRescue < Base
50
+ MSG = 'Useless `rescue` detected.'
51
+
52
+ def on_rescue(node)
53
+ resbody_node = node.resbody_branches.last
54
+ add_offense(resbody_node) if only_reraising?(resbody_node)
55
+ end
56
+
57
+ private
58
+
59
+ def only_reraising?(resbody_node)
60
+ body = resbody_node.body
61
+ return false if body.nil? || !body.send_type? || !body.method?(:raise)
62
+ return true unless body.arguments?
63
+ return false if body.arguments.size > 1
64
+
65
+ exception_name = body.first_argument.source
66
+ [resbody_node.exception_variable&.source, '$!', '$ERROR_INFO'].include?(exception_name)
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -9,6 +9,20 @@ module RuboCop
9
9
  # Keyword arguments can optionally be excluded from the total count,
10
10
  # as they add less complexity than positional or optional parameters.
11
11
  #
12
+ # Any number of arguments for `initialize` method inside a block of
13
+ # `Struct.new` and `Data.define` like this is always allowed:
14
+ #
15
+ # [source,ruby]
16
+ # ----
17
+ # Struct.new(:one, :two, :three, :four, :five, keyword_init: true) do
18
+ # def initialize(one:, two:, three:, four:, five:)
19
+ # end
20
+ # end
21
+ # ----
22
+ #
23
+ # This is because checking the number of arguments of the `initialize` method
24
+ # does not make sense.
25
+ #
12
26
  # NOTE: Explicit block argument `&block` is not counted to prevent
13
27
  # erroneous change that is avoided by making block argument implicit.
14
28
  #
@@ -63,6 +77,16 @@ module RuboCop
63
77
  NAMED_KEYWORD_TYPES = %i[kwoptarg kwarg].freeze
64
78
  private_constant :NAMED_KEYWORD_TYPES
65
79
 
80
+ # @!method struct_new_or_data_define_block?(node)
81
+ def_node_matcher :struct_new_or_data_define_block?, <<~PATTERN
82
+ (block
83
+ {
84
+ (send (const {nil? cbase} :Struct) :new ...)
85
+ (send (const {nil? cbase} :Data) :define ...)
86
+ }
87
+ (args) ...)
88
+ PATTERN
89
+
66
90
  def on_def(node)
67
91
  optargs = node.arguments.select(&:optarg_type?)
68
92
  return if optargs.count <= max_optional_parameters
@@ -78,6 +102,9 @@ module RuboCop
78
102
  alias on_defs on_def
79
103
 
80
104
  def on_args(node)
105
+ parent = node.parent
106
+ return if parent.method?(:initialize) && struct_new_or_data_define_block?(parent.parent)
107
+
81
108
  count = args_count(node)
82
109
  return unless count > max_params
83
110
 
@@ -3,6 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  # This module checks for Ruby 3.1's hash value omission syntax.
6
+ # rubocop:disable Metrics/ModuleLength
6
7
  module HashShorthandSyntax
7
8
  OMIT_HASH_VALUE_MSG = 'Omit the hash value.'
8
9
  EXPLICIT_HASH_VALUE_MSG = 'Include the hash value.'
@@ -93,6 +94,8 @@ module RuboCop
93
94
  end
94
95
 
95
96
  def def_node_that_require_parentheses(node)
97
+ last_pair = node.parent.pairs.last
98
+ return unless last_pair.key.source == last_pair.value.source
96
99
  return unless (send_node = find_ancestor_send_node(node))
97
100
  return unless without_parentheses_call_expr_follows?(send_node)
98
101
 
@@ -104,7 +107,11 @@ module RuboCop
104
107
  def find_ancestor_send_node(node)
105
108
  ancestor = node.parent.parent
106
109
 
107
- ancestor if ancestor&.call_type? && !ancestor&.method?(:[])
110
+ ancestor if ancestor&.call_type? && !brackets?(ancestor)
111
+ end
112
+
113
+ def brackets?(send_node)
114
+ send_node.method?(:[]) || send_node.method?(:[]=)
108
115
  end
109
116
 
110
117
  def use_element_of_hash_literal_as_receiver?(ancestor, parent)
@@ -184,4 +191,5 @@ module RuboCop
184
191
  end
185
192
  end
186
193
  end
194
+ # rubocop:enable Metrics/ModuleLength
187
195
  end
@@ -5,6 +5,7 @@ module RuboCop
5
5
  # Common functionality for modifier cops.
6
6
  module StatementModifier
7
7
  include LineLengthHelp
8
+ include RangeHelp
8
9
 
9
10
  private
10
11
 
@@ -118,11 +118,23 @@ module RuboCop
118
118
  end
119
119
 
120
120
  def allowed_receiver?(receiver)
121
- receiver_name = receiver.send_type? ? receiver.method_name.to_s : receiver.source
121
+ receiver_name = receiver_name(receiver)
122
122
 
123
123
  allowed_receivers.include?(receiver_name)
124
124
  end
125
125
 
126
+ def receiver_name(receiver)
127
+ if receiver.send_type?
128
+ if receiver.receiver
129
+ "#{receiver_name(receiver.receiver)}.#{receiver.method_name}"
130
+ else
131
+ receiver.method_name.to_s
132
+ end
133
+ else
134
+ receiver.source
135
+ end
136
+ end
137
+
126
138
  def allowed_receivers
127
139
  cop_config.fetch('AllowedReceivers', [])
128
140
  end
@@ -254,6 +254,7 @@ module RuboCop
254
254
  op = pair_node.loc.operator
255
255
 
256
256
  key_with_hash_rocket = ":#{pair_node.key.source}#{pair_node.inverse_delimiter(true)}"
257
+ key_with_hash_rocket += pair_node.key.source if pair_node.value_omission?
257
258
  corrector.replace(pair_node.key, key_with_hash_rocket)
258
259
  corrector.remove(range_with_surrounding_space(op))
259
260
  end
@@ -102,20 +102,23 @@ module RuboCop
102
102
  end
103
103
 
104
104
  def call_in_literals?(node)
105
- node.parent &&
106
- (node.parent.pair_type? ||
107
- node.parent.array_type? ||
108
- node.parent.range_type? ||
109
- splat?(node.parent) ||
110
- ternary_if?(node.parent))
105
+ parent = node.parent&.block_type? ? node.parent.parent : node.parent
106
+ return unless parent
107
+
108
+ parent.pair_type? ||
109
+ parent.array_type? ||
110
+ parent.range_type? ||
111
+ splat?(parent) ||
112
+ ternary_if?(parent)
111
113
  end
112
114
 
113
115
  def call_in_logical_operators?(node)
114
116
  parent = node.parent&.block_type? ? node.parent.parent : node.parent
115
- parent &&
116
- (logical_operator?(parent) ||
117
+ return unless parent
118
+
119
+ logical_operator?(parent) ||
117
120
  (parent.send_type? &&
118
- parent.arguments.any? { |argument| logical_operator?(argument) }))
121
+ parent.arguments.any? { |argument| logical_operator?(argument) })
119
122
  end
120
123
 
121
124
  def call_in_optional_arguments?(node)
@@ -99,6 +99,7 @@ module RuboCop
99
99
  class MissingElse < Base
100
100
  include OnNormalIfUnless
101
101
  include ConfigurableEnforcedStyle
102
+ extend AutoCorrector
102
103
 
103
104
  MSG = '`%<type>s` condition requires an `else`-clause.'
104
105
  MSG_NIL = '`%<type>s` condition requires an `else`-clause with `nil` in it.'
@@ -126,7 +127,9 @@ module RuboCop
126
127
  def check(node)
127
128
  return if node.else?
128
129
 
129
- add_offense(node, message: format(message_template, type: node.type))
130
+ add_offense(node, message: format(message_template, type: node.type)) do |corrector|
131
+ autocorrect(corrector, node)
132
+ end
130
133
  end
131
134
 
132
135
  def message_template
@@ -140,6 +143,15 @@ module RuboCop
140
143
  end
141
144
  end
142
145
 
146
+ def autocorrect(corrector, node)
147
+ case empty_else_style
148
+ when :empty
149
+ corrector.insert_before(node.loc.end, 'else; nil; ')
150
+ when :nil
151
+ corrector.insert_before(node.loc.end, 'else; ')
152
+ end
153
+ end
154
+
143
155
  def if_style?
144
156
  style == :if
145
157
  end
@@ -28,7 +28,7 @@ module RuboCop
28
28
  return if node.receiver.const_type?
29
29
 
30
30
  _lhs, _op, rhs = *node
31
- return if rhs.nil? || rhs.children.first
31
+ return if !rhs || method_call_with_parenthesized_arg?(rhs) || anonymous_forwarding?(rhs)
32
32
 
33
33
  add_offense(dot) do |corrector|
34
34
  wrap_in_parentheses_if_chained(corrector, node)
@@ -38,6 +38,20 @@ module RuboCop
38
38
 
39
39
  private
40
40
 
41
+ # Checks for an acceptable case of `foo.+(bar).baz`.
42
+ def method_call_with_parenthesized_arg?(argument)
43
+ return false unless argument.parent.parent&.send_type?
44
+
45
+ argument.children.first && argument.parent.parenthesized?
46
+ end
47
+
48
+ def anonymous_forwarding?(argument)
49
+ return true if argument.forwarded_args_type? || argument.forwarded_restarg_type?
50
+ return true if argument.children.first&.forwarded_kwrestarg_type?
51
+
52
+ argument.block_pass_type? && argument.source == '&'
53
+ end
54
+
41
55
  def wrap_in_parentheses_if_chained(corrector, node)
42
56
  return unless node.parent&.call_type?
43
57
 
@@ -30,7 +30,7 @@ module RuboCop
30
30
  return unless double_splat_hash_braces?(grandparent)
31
31
 
32
32
  add_offense(grandparent) do |corrector|
33
- corrector.replace(grandparent, node.pairs.map(&:source).join(', '))
33
+ corrector.replace(grandparent, node.children.map(&:source).join(', '))
34
34
  end
35
35
  end
36
36
  end
@@ -77,7 +77,8 @@ module RuboCop
77
77
  # but it's not necessry to escape hyphen if it's the first or last character
78
78
  # within the character class. This method checks if that's the case.
79
79
  # e.g. "[0-9\\-]" or "[\\-0-9]" would return true
80
- node.source[index] == '[' || node.source[index + 3] == ']'
80
+ contents_range(node).source[index - 1] == '[' ||
81
+ contents_range(node).source[index + 2] == ']'
81
82
  end
82
83
 
83
84
  def delimiter?(node, char)
@@ -41,10 +41,13 @@ module RuboCop
41
41
 
42
42
  def on_pair(node)
43
43
  return unless string_hash_key?(node)
44
+
45
+ key_content = node.key.str_content
46
+ return unless key_content.valid_encoding?
44
47
  return if receive_environments_method?(node)
45
48
 
46
49
  add_offense(node.key) do |corrector|
47
- symbol_content = node.key.str_content.to_sym.inspect
50
+ symbol_content = key_content.to_sym.inspect
48
51
 
49
52
  corrector.replace(node.key, symbol_content)
50
53
  end
@@ -7,6 +7,13 @@ module RuboCop
7
7
  # and `^` operators) where the order of expression is reversed, eg. `1 + x`.
8
8
  # This cop complements `Style/YodaCondition` cop, which has a similar purpose.
9
9
  #
10
+ # This cop is disabled by default to respect user intentions such as:
11
+ #
12
+ # [source,ruby]
13
+ # ----
14
+ # config.server_port = 9000 + ENV["TEST_ENV_NUMBER"].to_i
15
+ # ----
16
+ #
10
17
  # @safety
11
18
  # This cop is unsafe because binary operators can be defined
12
19
  # differently on different classes, and are not guaranteed to
@@ -28,14 +28,19 @@ module RuboCop
28
28
  end
29
29
  end
30
30
 
31
+ SMART_PATH_CACHE = {} # rubocop:disable Style/MutableConstant
32
+ private_constant :SMART_PATH_CACHE
33
+
31
34
  def smart_path(path)
32
- # Ideally, we calculate this relative to the project root.
33
- base_dir = Dir.pwd
35
+ SMART_PATH_CACHE[path] ||= begin
36
+ # Ideally, we calculate this relative to the project root.
37
+ base_dir = Dir.pwd
34
38
 
35
- if path.start_with? base_dir
36
- relative_path(path, base_dir)
37
- else
38
- path
39
+ if path.start_with? base_dir
40
+ relative_path(path, base_dir)
41
+ else
42
+ path
43
+ end
39
44
  end
40
45
  end
41
46
 
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '1.42.0'
6
+ STRING = '1.43.0'
7
7
 
8
8
  MSG = '%<version>s (using Parser %<parser_version>s, ' \
9
9
  'rubocop-ast %<rubocop_ast_version>s, ' \
data/lib/rubocop.rb CHANGED
@@ -399,6 +399,7 @@ require_relative 'rubocop/cop/lint/useless_access_modifier'
399
399
  require_relative 'rubocop/cop/lint/useless_assignment'
400
400
  require_relative 'rubocop/cop/lint/useless_else_without_rescue'
401
401
  require_relative 'rubocop/cop/lint/useless_method_definition'
402
+ require_relative 'rubocop/cop/lint/useless_rescue'
402
403
  require_relative 'rubocop/cop/lint/useless_ruby2_keywords'
403
404
  require_relative 'rubocop/cop/lint/useless_setter_call'
404
405
  require_relative 'rubocop/cop/lint/useless_times'
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.42.0
4
+ version: 1.43.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
8
8
  - Jonas Arvidsson
9
9
  - Yuji Nakayama
10
- autorequire:
10
+ autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2023-01-01 00:00:00.000000000 Z
13
+ date: 2023-01-10 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: json
@@ -46,14 +46,14 @@ dependencies:
46
46
  requirements:
47
47
  - - ">="
48
48
  - !ruby/object:Gem::Version
49
- version: 3.1.2.1
49
+ version: 3.2.0.0
50
50
  type: :runtime
51
51
  prerelease: false
52
52
  version_requirements: !ruby/object:Gem::Requirement
53
53
  requirements:
54
54
  - - ">="
55
55
  - !ruby/object:Gem::Version
56
- version: 3.1.2.1
56
+ version: 3.2.0.0
57
57
  - !ruby/object:Gem::Dependency
58
58
  name: rainbow
59
59
  requirement: !ruby/object:Gem::Requirement
@@ -154,7 +154,7 @@ dependencies:
154
154
  requirements:
155
155
  - - ">="
156
156
  - !ruby/object:Gem::Version
157
- version: 1.4.0
157
+ version: 2.4.0
158
158
  - - "<"
159
159
  - !ruby/object:Gem::Version
160
160
  version: '3.0'
@@ -164,7 +164,7 @@ dependencies:
164
164
  requirements:
165
165
  - - ">="
166
166
  - !ruby/object:Gem::Version
167
- version: 1.4.0
167
+ version: 2.4.0
168
168
  - - "<"
169
169
  - !ruby/object:Gem::Version
170
170
  version: '3.0'
@@ -539,6 +539,7 @@ files:
539
539
  - lib/rubocop/cop/lint/useless_assignment.rb
540
540
  - lib/rubocop/cop/lint/useless_else_without_rescue.rb
541
541
  - lib/rubocop/cop/lint/useless_method_definition.rb
542
+ - lib/rubocop/cop/lint/useless_rescue.rb
542
543
  - lib/rubocop/cop/lint/useless_ruby2_keywords.rb
543
544
  - lib/rubocop/cop/lint/useless_setter_call.rb
544
545
  - lib/rubocop/cop/lint/useless_times.rb
@@ -995,10 +996,10 @@ metadata:
995
996
  homepage_uri: https://rubocop.org/
996
997
  changelog_uri: https://github.com/rubocop/rubocop/blob/master/CHANGELOG.md
997
998
  source_code_uri: https://github.com/rubocop/rubocop/
998
- documentation_uri: https://docs.rubocop.org/rubocop/1.42/
999
+ documentation_uri: https://docs.rubocop.org/rubocop/1.43/
999
1000
  bug_tracker_uri: https://github.com/rubocop/rubocop/issues
1000
1001
  rubygems_mfa_required: 'true'
1001
- post_install_message:
1002
+ post_install_message:
1002
1003
  rdoc_options: []
1003
1004
  require_paths:
1004
1005
  - lib
@@ -1013,8 +1014,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
1013
1014
  - !ruby/object:Gem::Version
1014
1015
  version: '0'
1015
1016
  requirements: []
1016
- rubygems_version: 3.3.7
1017
- signing_key:
1017
+ rubygems_version: 3.1.2
1018
+ signing_key:
1018
1019
  specification_version: 4
1019
1020
  summary: Automatic Ruby code style checking tool.
1020
1021
  test_files: []