rubocop 1.31.0 → 1.32.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +23 -0
  4. data/lib/rubocop/cli.rb +1 -0
  5. data/lib/rubocop/config.rb +1 -1
  6. data/lib/rubocop/config_loader_resolver.rb +1 -1
  7. data/lib/rubocop/cop/base.rb +1 -1
  8. data/lib/rubocop/cop/generator.rb +4 -0
  9. data/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb +60 -0
  10. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  11. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +4 -3
  12. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +3 -3
  13. data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +57 -13
  14. data/lib/rubocop/cop/layout/line_continuation_spacing.rb +10 -0
  15. data/lib/rubocop/cop/layout/line_length.rb +2 -0
  16. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +4 -1
  17. data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +45 -0
  18. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +7 -0
  19. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +10 -4
  20. data/lib/rubocop/cop/lint/literal_as_condition.rb +5 -0
  21. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +60 -24
  22. data/lib/rubocop/cop/lint/number_conversion.rb +7 -1
  23. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +7 -0
  24. data/lib/rubocop/cop/lint/require_range_parentheses.rb +57 -0
  25. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +35 -1
  26. data/lib/rubocop/cop/metrics/block_length.rb +1 -0
  27. data/lib/rubocop/cop/metrics/method_length.rb +1 -0
  28. data/lib/rubocop/cop/mixin/check_line_breakable.rb +4 -0
  29. data/lib/rubocop/cop/mixin/def_node.rb +2 -7
  30. data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +12 -14
  31. data/lib/rubocop/cop/mixin/percent_array.rb +60 -1
  32. data/lib/rubocop/cop/naming/predicate_name.rb +8 -0
  33. data/lib/rubocop/cop/style/class_equality_comparison.rb +22 -0
  34. data/lib/rubocop/cop/style/empty_else.rb +37 -0
  35. data/lib/rubocop/cop/style/empty_heredoc.rb +59 -0
  36. data/lib/rubocop/cop/style/fetch_env_var.rb +10 -177
  37. data/lib/rubocop/cop/style/format_string_token.rb +6 -0
  38. data/lib/rubocop/cop/style/hash_except.rb +1 -1
  39. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +2 -0
  40. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +12 -0
  41. data/lib/rubocop/cop/style/module_function.rb +2 -2
  42. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +9 -0
  43. data/lib/rubocop/cop/style/numeric_predicate.rb +20 -6
  44. data/lib/rubocop/cop/style/redundant_parentheses.rb +2 -1
  45. data/lib/rubocop/cop/style/semicolon.rb +27 -3
  46. data/lib/rubocop/cop/style/symbol_array.rb +2 -3
  47. data/lib/rubocop/cop/style/symbol_proc.rb +9 -1
  48. data/lib/rubocop/cop/style/trivial_accessors.rb +3 -0
  49. data/lib/rubocop/cop/style/word_array.rb +2 -3
  50. data/lib/rubocop/formatter/simple_text_formatter.rb +2 -0
  51. data/lib/rubocop/formatter.rb +21 -21
  52. data/lib/rubocop/options.rb +3 -6
  53. data/lib/rubocop/rake_task.rb +5 -1
  54. data/lib/rubocop/rspec/shared_contexts.rb +14 -14
  55. data/lib/rubocop/rspec/support.rb +14 -0
  56. data/lib/rubocop/runner.rb +4 -0
  57. data/lib/rubocop/server/client_command/base.rb +1 -1
  58. data/lib/rubocop/version.rb +1 -1
  59. data/lib/rubocop.rb +4 -1
  60. metadata +23 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ce713e2a82b2cedb9ee589f23d263234dfd7998fbeb1efd1bcaa7cd41fcd9c41
4
- data.tar.gz: e60ca60484e5093ee4d266a0041c0f6b5a501fe693ace1969b9d6fb423c46d1e
3
+ metadata.gz: 270faf06487f8a915ef6293944db136da6fb51ec09b5613be4b848a471c858bb
4
+ data.tar.gz: 1c61746959019453862c2e90b8d3250ce3f9815b1eae286f1fd5b73fb267a1cb
5
5
  SHA512:
6
- metadata.gz: fbce1782b8cc7ec39ba95110a4d3feaacd66f73c6aadf572da79c6ca35af0f1339341adfa9a4765056d32e42759c206037153a94619f99ffeade552e43cbd016
7
- data.tar.gz: b93cb01f0f1e53302c61bc45ad1d680d89c5e338539aa084b59d072af27dc4eb7b74da88dc05512f879f0277eeaa8e84950fd55fbfcc5d0144eba7be7134df3b
6
+ metadata.gz: 86966e7ff42d0f514a447c8313cc9bdc13020ec1d375e584dba8ebae5bb535a1238380a57d55dfd697c735b3f9259f59939563f4b504f08570304e4ec1c2c7af
7
+ data.tar.gz: ce35c98b8b51b8b59c6c2aeac880a0618067a68f7080a2e44408a7e283471b2b57f84e57c127144632b61eaa8edf6e4369d4702c2baa222cb3b37fad38606bd5
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.31', require: false
56
+ gem 'rubocop', '~> 1.32', 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
@@ -977,6 +977,11 @@ Layout/LineContinuationLeadingSpace:
977
977
  AutoCorrect: false
978
978
  SafeAutoCorrect: false
979
979
  VersionAdded: '1.31'
980
+ VersionChanged: '1.32'
981
+ EnforcedStyle: trailing
982
+ SupportedStyles:
983
+ - leading
984
+ - trailing
980
985
 
981
986
  Layout/LineContinuationSpacing:
982
987
  Description: 'Checks the spacing in front of backslash in line continuations.'
@@ -1153,6 +1158,13 @@ Layout/MultilineMethodDefinitionBraceLayout:
1153
1158
  - new_line
1154
1159
  - same_line
1155
1160
 
1161
+ Layout/MultilineMethodParameterLineBreaks:
1162
+ Description: >-
1163
+ Checks that each parameter in a multi-line method definition
1164
+ starts on a separate line.
1165
+ Enabled: false
1166
+ VersionAdded: '1.32'
1167
+
1156
1168
  Layout/MultilineOperationIndentation:
1157
1169
  Description: >-
1158
1170
  Checks indentation of binary operations that span more than
@@ -2128,6 +2140,11 @@ Lint/RequireParentheses:
2128
2140
  Enabled: true
2129
2141
  VersionAdded: '0.18'
2130
2142
 
2143
+ Lint/RequireRangeParentheses:
2144
+ Description: 'Checks that a range literal is enclosed in parentheses when the end of the range is at a line break.'
2145
+ Enabled: pending
2146
+ VersionAdded: '1.32'
2147
+
2131
2148
  Lint/RequireRelativeSelfPath:
2132
2149
  Description: 'Checks for uses a file requiring itself with `require_relative`.'
2133
2150
  Enabled: pending
@@ -3495,6 +3512,12 @@ Style/EmptyElse:
3495
3512
  - empty
3496
3513
  - nil
3497
3514
  - both
3515
+ AllowComments: false
3516
+
3517
+ Style/EmptyHeredoc:
3518
+ Description: 'Checks for using empty heredoc to reduce redundancy.'
3519
+ Enabled: pending
3520
+ VersionAdded: '1.32'
3498
3521
 
3499
3522
  Style/EmptyLambdaParameter:
3500
3523
  Description: 'Omit parens for empty lambda parameters.'
data/lib/rubocop/cli.rb CHANGED
@@ -12,6 +12,7 @@ module RuboCop
12
12
  color debug display_style_guide display_time display_only_fail_level_offenses
13
13
  display_only_failed except extra_details fail_level fix_layout format
14
14
  ignore_disable_comments lint only only_guide_cops require safe
15
+ autocorrect safe_autocorrect autocorrect_all
15
16
  ].freeze
16
17
 
17
18
  class Finished < StandardError; end
@@ -25,7 +25,7 @@ module RuboCop
25
25
  @loaded_path = loaded_path
26
26
  @for_cop = Hash.new do |h, cop|
27
27
  qualified_cop_name = Cop::Registry.qualified_cop_name(cop, loaded_path)
28
- cop_options = self[qualified_cop_name] || {}
28
+ cop_options = self[qualified_cop_name].dup || {}
29
29
  cop_options['Enabled'] = enable_cop?(qualified_cop_name, cop_options)
30
30
  h[cop] = cop_options
31
31
  end
@@ -179,7 +179,7 @@ module RuboCop
179
179
 
180
180
  def determine_inherit_mode(hash, key)
181
181
  cop_cfg = hash[key]
182
- local_inherit = cop_cfg.delete('inherit_mode') if cop_cfg.is_a?(Hash)
182
+ local_inherit = cop_cfg['inherit_mode'] if cop_cfg.is_a?(Hash)
183
183
  local_inherit || hash['inherit_mode'] || {}
184
184
  end
185
185
 
@@ -48,7 +48,7 @@ module RuboCop
48
48
  InvestigationReport = Struct.new(:cop, :processed_source, :offenses, :corrector)
49
49
 
50
50
  # List of methods names to restrict calls for `on_send` / `on_csend`
51
- RESTRICT_ON_SEND = Set[].freeze
51
+ RESTRICT_ON_SEND = Set[].freeze # rubocop:disable InternalAffairs/UselessRestrictOnSend
52
52
 
53
53
  # List of cops that should not try to autocorrect at the same
54
54
  # time as this cop
@@ -62,6 +62,10 @@ module RuboCop
62
62
  # For example
63
63
  MSG = 'Use `#good_method` instead of `#bad_method`.'
64
64
 
65
+ # TODO: Don't call `on_send` unless the method name is in this list
66
+ # If you don't need `on_send` in the cop you created, remove it.
67
+ RESTRICT_ON_SEND = %%i[bad_method].freeze
68
+
65
69
  # @!method bad_method?(node)
66
70
  def_node_matcher :bad_method?, <<~PATTERN
67
71
  (send nil? :bad_method ...)
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InternalAffairs
6
+ # Check for useless `RESTRICT_ON_SEND`.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # class FooCop
11
+ # RESTRICT_ON_SEND = %i[bad_method].freeze
12
+ # end
13
+ #
14
+ # # good
15
+ # class FooCop
16
+ # RESTRICT_ON_SEND = %i[bad_method].freeze
17
+ # def on_send(node)
18
+ # # ...
19
+ # end
20
+ # end
21
+ #
22
+ # # good
23
+ # class FooCop
24
+ # RESTRICT_ON_SEND = %i[bad_method].freeze
25
+ # def after_send(node)
26
+ # # ...
27
+ # end
28
+ # end
29
+ #
30
+ class UselessRestrictOnSend < Base
31
+ extend AutoCorrector
32
+
33
+ MSG = 'Useless `RESTRICT_ON_SEND` is defined.'
34
+
35
+ # @!method defined_send_callback?(node)
36
+ def_node_search :defined_send_callback?, <<~PATTERN
37
+ {
38
+ (def {:on_send :after_send} ...)
39
+ (alias (sym {:on_send :after_send}) _source ...)
40
+ (send nil? :alias_method {(sym {:on_send :after_send}) (str {"on_send" "after_send"})} _source ...)
41
+ }
42
+ PATTERN
43
+
44
+ def on_casgn(node)
45
+ return if !restrict_on_send?(node) || defined_send_callback?(node.parent)
46
+
47
+ add_offense(node) do |corrector|
48
+ corrector.remove(node)
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def restrict_on_send?(node)
55
+ node.name == :RESTRICT_ON_SEND
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -20,3 +20,4 @@ require_relative 'internal_affairs/redundant_method_dispatch_node'
20
20
  require_relative 'internal_affairs/style_detected_api_use'
21
21
  require_relative 'internal_affairs/undefined_config'
22
22
  require_relative 'internal_affairs/useless_message_assertion'
23
+ require_relative 'internal_affairs/useless_restrict_on_send'
@@ -120,14 +120,15 @@ module RuboCop
120
120
  check_first(first_elem, left_bracket, left_parenthesis, 0)
121
121
  end
122
122
 
123
- check_right_bracket(array_node.loc.end, left_bracket, left_parenthesis)
123
+ check_right_bracket(array_node.loc.end, first_elem, left_bracket, left_parenthesis)
124
124
  end
125
125
 
126
- def check_right_bracket(right_bracket, left_bracket, left_parenthesis)
126
+ def check_right_bracket(right_bracket, first_elem, left_bracket, left_parenthesis)
127
127
  # if the right bracket is on the same line as the last value, accept
128
128
  return if /\S/.match?(right_bracket.source_line[0...right_bracket.column])
129
129
 
130
- expected_column, indent_base_type = indent_base(left_bracket, left_parenthesis)
130
+ expected_column, indent_base_type = indent_base(left_bracket, first_elem,
131
+ left_parenthesis)
131
132
  @column_delta = expected_column - right_bracket.column
132
133
  return if @column_delta.zero?
133
134
 
@@ -158,14 +158,14 @@ module RuboCop
158
158
  end
159
159
  end
160
160
 
161
- check_right_brace(hash_node.loc.end, left_brace, left_parenthesis)
161
+ check_right_brace(hash_node.loc.end, first_pair, left_brace, left_parenthesis)
162
162
  end
163
163
 
164
- def check_right_brace(right_brace, left_brace, left_parenthesis)
164
+ def check_right_brace(right_brace, first_pair, left_brace, left_parenthesis)
165
165
  # if the right brace is on the same line as the last value, accept
166
166
  return if /\S/.match?(right_brace.source_line[0...right_brace.column])
167
167
 
168
- expected_column, indent_base_type = indent_base(left_brace, left_parenthesis)
168
+ expected_column, indent_base_type = indent_base(left_brace, first_pair, left_parenthesis)
169
169
  @column_delta = expected_column - right_brace.column
170
170
  return if @column_delta.zero?
171
171
 
@@ -4,9 +4,10 @@ module RuboCop
4
4
  module Cop
5
5
  module Layout
6
6
  # Checks that strings broken over multiple lines (by a backslash) contain
7
- # trailing spaces instead of leading spaces.
7
+ # trailing spaces instead of leading spaces (default) or leading spaces
8
+ # instead of trailing spaces.
8
9
  #
9
- # @example
10
+ # @example EnforcedStyle: trailing (default)
10
11
  # # bad
11
12
  # 'this text contains a lot of' \
12
13
  # ' spaces'
@@ -23,18 +24,38 @@ module RuboCop
23
24
  # 'this text is too ' \
24
25
  # 'long'
25
26
  #
27
+ # @example EnforcedStyle: leading
28
+ # # bad
29
+ # 'this text contains a lot of ' \
30
+ # 'spaces'
31
+ #
32
+ # # good
33
+ # 'this text contains a lot of' \
34
+ # ' spaces'
35
+ #
36
+ # # bad
37
+ # 'this text is too ' \
38
+ # 'long'
39
+ #
40
+ # # good
41
+ # 'this text is too' \
42
+ # ' long'
26
43
  class LineContinuationLeadingSpace < Base
27
44
  include RangeHelp
28
45
 
29
- MSG = 'Move leading spaces to the end of previous line.'
30
-
31
46
  def on_dstr(node)
32
- range_start = node.loc.expression.begin_pos - node.loc.expression.column
47
+ end_of_first_line = node.loc.expression.begin_pos - node.loc.expression.column
33
48
 
34
49
  raw_lines(node).each_cons(2) do |raw_line_one, raw_line_two|
35
- range_start += raw_line_one.length
50
+ end_of_first_line += raw_line_one.length
36
51
 
37
- investigate(raw_line_one, raw_line_two, range_start)
52
+ next unless continuation?(raw_line_one)
53
+
54
+ if enforced_style_leading?
55
+ investigate_leading_style(raw_line_one, end_of_first_line)
56
+ else
57
+ investigate_trailing_style(raw_line_two, end_of_first_line)
58
+ end
38
59
  end
39
60
  end
40
61
 
@@ -44,24 +65,47 @@ module RuboCop
44
65
  processed_source.raw_source.lines[node.first_line - 1, line_range(node).size]
45
66
  end
46
67
 
47
- def investigate(first_line, second_line, range_start)
48
- return unless continuation?(first_line)
68
+ def investigate_leading_style(first_line, end_of_first_line)
69
+ matches = first_line.match(/(?<trailing_spaces>\s+)(?<ending>['"]\s*\\\n)/)
70
+ return if matches.nil?
49
71
 
50
- matches = second_line.match(/\A(?<indent>\s*['"])(?<leading_spaces>\s+)/)
72
+ add_offense(leading_offense_range(end_of_first_line, matches))
73
+ end
74
+
75
+ def investigate_trailing_style(second_line, end_of_first_line)
76
+ matches = second_line.match(/\A(?<beginning>\s*['"])(?<leading_spaces>\s+)/)
51
77
  return if matches.nil?
52
78
 
53
- add_offense(offense_range(range_start, matches))
79
+ add_offense(trailing_offense_range(end_of_first_line, matches))
54
80
  end
55
81
 
56
82
  def continuation?(line)
57
83
  line.end_with?("\\\n")
58
84
  end
59
85
 
60
- def offense_range(range_start, matches)
61
- begin_pos = range_start + matches[:indent].length
86
+ def leading_offense_range(end_of_first_line, matches)
87
+ end_pos = end_of_first_line - matches[:ending].length
88
+ begin_pos = end_pos - matches[:trailing_spaces].length
89
+ range_between(begin_pos, end_pos)
90
+ end
91
+
92
+ def trailing_offense_range(end_of_first_line, matches)
93
+ begin_pos = end_of_first_line + matches[:beginning].length
62
94
  end_pos = begin_pos + matches[:leading_spaces].length
63
95
  range_between(begin_pos, end_pos)
64
96
  end
97
+
98
+ def message(_range)
99
+ if enforced_style_leading?
100
+ 'Move trailing spaces to the start of next line.'
101
+ else
102
+ 'Move leading spaces to the end of previous line.'
103
+ end
104
+ end
105
+
106
+ def enforced_style_leading?
107
+ cop_config['EnforcedStyle'] == 'leading'
108
+ end
65
109
  end
66
110
  end
67
111
  end
@@ -32,10 +32,14 @@ module RuboCop
32
32
  extend AutoCorrector
33
33
 
34
34
  def on_new_investigation
35
+ last_line = last_line(processed_source)
36
+
35
37
  @ignored_ranges = string_literal_ranges(processed_source.ast) +
36
38
  comment_ranges(processed_source.comments)
37
39
 
38
40
  processed_source.raw_source.lines.each_with_index do |line, index|
41
+ break if index >= last_line
42
+
39
43
  line_number = index + 1
40
44
  investigate(line, line_number)
41
45
  end
@@ -103,6 +107,12 @@ module RuboCop
103
107
  comments.map(&:loc).map(&:expression)
104
108
  end
105
109
 
110
+ def last_line(processed_source)
111
+ last_token = processed_source.tokens.last
112
+
113
+ last_token ? last_token.line : processed_source.lines.length
114
+ end
115
+
106
116
  def ignore_range?(backtick_range)
107
117
  @ignored_ranges.any? { |range| range.contains?(backtick_range) }
108
118
  end
@@ -37,6 +37,7 @@ module RuboCop
37
37
  # * MultilineHashBraceLayout
38
38
  # * MultilineHashKeyLineBreaks
39
39
  # * MultilineMethodArgumentLineBreaks
40
+ # * MultilineMethodParameterLineBreaks
40
41
  # * ParameterAlignment
41
42
  #
42
43
  # Together, these cops will pretty print hashes, arrays,
@@ -79,6 +80,7 @@ module RuboCop
79
80
  alias on_array on_potential_breakable_node
80
81
  alias on_hash on_potential_breakable_node
81
82
  alias on_send on_potential_breakable_node
83
+ alias on_def on_potential_breakable_node
82
84
 
83
85
  def on_new_investigation
84
86
  check_for_breakable_semicolons(processed_source)
@@ -6,7 +6,7 @@ module RuboCop
6
6
  # Ensures that each argument in a multi-line method call
7
7
  # starts on a separate line.
8
8
  #
9
- # NOTE: this cop does not move the first argument, if you want that to
9
+ # NOTE: This cop does not move the first argument, if you want that to
10
10
  # be on a separate line, see `Layout/FirstMethodArgumentLineBreak`.
11
11
  #
12
12
  # @example
@@ -22,6 +22,9 @@ module RuboCop
22
22
  # b,
23
23
  # c
24
24
  # )
25
+ #
26
+ # # good
27
+ # foo(a, b, c)
25
28
  class MultilineMethodArgumentLineBreaks < Base
26
29
  include MultilineElementLineBreaks
27
30
  extend AutoCorrector
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Layout
6
+ # Ensures that each parameter in a multi-line method definition
7
+ # starts on a separate line.
8
+ #
9
+ # NOTE: This cop does not move the first argument, if you want that to
10
+ # be on a separate line, see `Layout/FirstMethodParameterLineBreak`.
11
+ #
12
+ # @example
13
+ #
14
+ # # bad
15
+ # def foo(a, b,
16
+ # c
17
+ # )
18
+ # end
19
+ #
20
+ # # good
21
+ # def foo(
22
+ # a,
23
+ # b,
24
+ # c
25
+ # )
26
+ # end
27
+ #
28
+ # # good
29
+ # def foo(a, b, c)
30
+ # end
31
+ class MultilineMethodParameterLineBreaks < Base
32
+ include MultilineElementLineBreaks
33
+ extend AutoCorrector
34
+
35
+ MSG = 'Each parameter in a multi-line method definition must start on a separate line.'
36
+
37
+ def on_def(node)
38
+ return if node.arguments.empty?
39
+
40
+ check_line_breaks(node, node.arguments)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -7,6 +7,7 @@ module RuboCop
7
7
  # when param passed without parentheses.
8
8
  #
9
9
  # This cop can customize ignored methods with `IgnoredMethods`.
10
+ # By default, there are no methods to ignored.
10
11
  #
11
12
  # @example
12
13
  #
@@ -29,10 +30,16 @@ module RuboCop
29
30
  # # Lambda arguments require no disambiguation
30
31
  # foo = ->(bar) { bar.baz }
31
32
  #
33
+ # @example IgnoredMethods: [] (default)
34
+ #
35
+ # # bad
36
+ # expect { do_something }.to change { object.attribute }
37
+ #
32
38
  # @example IgnoredMethods: [change]
33
39
  #
34
40
  # # good
35
41
  # expect { do_something }.to change { object.attribute }
42
+ #
36
43
  class AmbiguousBlockAssociation < Base
37
44
  include IgnoredMethods
38
45
 
@@ -8,22 +8,22 @@ module RuboCop
8
8
  # @example
9
9
  #
10
10
  # # bad
11
- #
12
11
  # File.exists?(some_path)
13
12
  # Dir.exists?(some_path)
14
13
  # iterator?
15
14
  # ENV.freeze # Calling `Env.freeze` raises `TypeError` since Ruby 2.7.
15
+ # ENV.clone
16
+ # ENV.dup # Calling `Env.dup` raises `TypeError` since Ruby 3.1.
16
17
  # Socket.gethostbyname(host)
17
18
  # Socket.gethostbyaddr(host)
18
19
  #
19
- # @example
20
- #
21
20
  # # good
22
- #
23
21
  # File.exist?(some_path)
24
22
  # Dir.exist?(some_path)
25
23
  # block_given?
26
24
  # ENV # `ENV.freeze` cannot prohibit changes to environment variables.
25
+ # ENV.to_h
26
+ # ENV.to_h # `ENV.dup` cannot dup `ENV`, use `ENV.to_h` to get a copy of `ENV` as a hash.
27
27
  # Addrinfo.getaddrinfo(nodename, service)
28
28
  # Addrinfo.tcp(host, port).getnameinfo
29
29
  class DeprecatedClassMethods < Base
@@ -111,6 +111,12 @@ module RuboCop
111
111
  DeprecatedClassMethod.new(:freeze, class_constant: :ENV) =>
112
112
  Replacement.new(nil, class_constant: :ENV),
113
113
 
114
+ DeprecatedClassMethod.new(:clone, class_constant: :ENV) =>
115
+ Replacement.new(:to_h, class_constant: :ENV),
116
+
117
+ DeprecatedClassMethod.new(:dup, class_constant: :ENV) =>
118
+ Replacement.new(:to_h, class_constant: :ENV),
119
+
114
120
  DeprecatedClassMethod.new(:gethostbyaddr, class_constant: :Socket, correctable: false) =>
115
121
  Replacement.new(:getnameinfo, class_constant: :Addrinfo, instance_method: true),
116
122
 
@@ -7,6 +7,9 @@ module RuboCop
7
7
  # operands in and/or expressions serving as the conditions of
8
8
  # if/while/until/case-when/case-in.
9
9
  #
10
+ # NOTE: Literals in `case-in` condition where the match variable is used in
11
+ # `in` are accepted as a pattern matching.
12
+ #
10
13
  # @example
11
14
  #
12
15
  # # bad
@@ -69,6 +72,8 @@ module RuboCop
69
72
 
70
73
  def on_case_match(case_match_node)
71
74
  if case_match_node.condition
75
+ return if case_match_node.descendants.any?(&:match_var_type?)
76
+
72
77
  check_case(case_match_node)
73
78
  else
74
79
  case_match_node.each_in_pattern do |in_pattern_node|