rubocop 1.64.1 → 1.65.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +11 -1
  4. data/lib/rubocop/config_loader.rb +1 -1
  5. data/lib/rubocop/config_loader_resolver.rb +9 -3
  6. data/lib/rubocop/cop/cop.rb +22 -4
  7. data/lib/rubocop/cop/gemspec/add_runtime_dependency.rb +38 -0
  8. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -2
  9. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +3 -3
  10. data/lib/rubocop/cop/layout/assignment_indentation.rb +3 -2
  11. data/lib/rubocop/cop/layout/case_indentation.rb +1 -1
  12. data/lib/rubocop/cop/layout/condition_position.rb +0 -4
  13. data/lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb +1 -1
  14. data/lib/rubocop/cop/layout/first_argument_indentation.rb +2 -2
  15. data/lib/rubocop/cop/layout/heredoc_indentation.rb +1 -1
  16. data/lib/rubocop/cop/layout/indentation_width.rb +1 -1
  17. data/lib/rubocop/cop/layout/line_length.rb +20 -20
  18. data/lib/rubocop/cop/layout/space_around_operators.rb +3 -0
  19. data/lib/rubocop/cop/legacy/corrector.rb +12 -2
  20. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +0 -2
  21. data/lib/rubocop/cop/lint/ambiguous_operator.rb +0 -2
  22. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +0 -2
  23. data/lib/rubocop/cop/lint/boolean_symbol.rb +0 -2
  24. data/lib/rubocop/cop/lint/circular_argument_reference.rb +0 -13
  25. data/lib/rubocop/cop/lint/debugger.rb +0 -4
  26. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +0 -10
  27. data/lib/rubocop/cop/lint/duplicate_case_condition.rb +1 -5
  28. data/lib/rubocop/cop/lint/duplicate_hash_key.rb +0 -4
  29. data/lib/rubocop/cop/lint/duplicate_methods.rb +0 -10
  30. data/lib/rubocop/cop/lint/each_with_object_argument.rb +0 -4
  31. data/lib/rubocop/cop/lint/else_layout.rb +0 -2
  32. data/lib/rubocop/cop/lint/empty_ensure.rb +1 -11
  33. data/lib/rubocop/cop/lint/empty_interpolation.rb +0 -4
  34. data/lib/rubocop/cop/lint/empty_when.rb +1 -3
  35. data/lib/rubocop/cop/lint/ensure_return.rb +1 -6
  36. data/lib/rubocop/cop/lint/float_comparison.rb +3 -1
  37. data/lib/rubocop/cop/lint/float_out_of_range.rb +0 -4
  38. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +0 -10
  39. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +15 -12
  40. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +0 -7
  41. data/lib/rubocop/cop/lint/interpolation_check.rb +0 -4
  42. data/lib/rubocop/cop/lint/literal_as_condition.rb +1 -1
  43. data/lib/rubocop/cop/lint/literal_assignment_in_condition.rb +1 -1
  44. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +0 -4
  45. data/lib/rubocop/cop/lint/loop.rb +6 -12
  46. data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -7
  47. data/lib/rubocop/cop/lint/next_without_accumulator.rb +0 -4
  48. data/lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb +0 -5
  49. data/lib/rubocop/cop/lint/percent_string_array.rb +0 -4
  50. data/lib/rubocop/cop/lint/percent_symbol_array.rb +0 -4
  51. data/lib/rubocop/cop/lint/rand_one.rb +0 -4
  52. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +3 -1
  53. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +1 -1
  54. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +0 -4
  55. data/lib/rubocop/cop/lint/require_parentheses.rb +0 -4
  56. data/lib/rubocop/cop/lint/rescue_exception.rb +0 -4
  57. data/lib/rubocop/cop/lint/return_in_void_context.rb +0 -2
  58. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +0 -4
  59. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +6 -10
  60. data/lib/rubocop/cop/lint/to_enum_arguments.rb +2 -9
  61. data/lib/rubocop/cop/lint/unified_integer.rb +0 -4
  62. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +1 -0
  63. data/lib/rubocop/cop/lint/unreachable_code.rb +0 -5
  64. data/lib/rubocop/cop/lint/useless_assignment.rb +1 -5
  65. data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +0 -4
  66. data/lib/rubocop/cop/lint/useless_setter_call.rb +0 -4
  67. data/lib/rubocop/cop/lint/void.rb +5 -0
  68. data/lib/rubocop/cop/metrics/block_nesting.rb +19 -7
  69. data/lib/rubocop/cop/mixin/alignment.rb +5 -1
  70. data/lib/rubocop/cop/mixin/allowed_methods.rb +7 -1
  71. data/lib/rubocop/cop/mixin/allowed_pattern.rb +15 -3
  72. data/lib/rubocop/cop/mixin/configurable_max.rb +5 -1
  73. data/lib/rubocop/cop/mixin/rescue_node.rb +4 -0
  74. data/lib/rubocop/cop/naming/predicate_name.rb +52 -26
  75. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +10 -1
  76. data/lib/rubocop/cop/style/arguments_forwarding.rb +1 -1
  77. data/lib/rubocop/cop/style/def_with_parentheses.rb +0 -2
  78. data/lib/rubocop/cop/style/each_for_simple_loop.rb +0 -1
  79. data/lib/rubocop/cop/style/eval_with_location.rb +12 -11
  80. data/lib/rubocop/cop/style/file_read.rb +2 -5
  81. data/lib/rubocop/cop/style/file_write.rb +2 -5
  82. data/lib/rubocop/cop/style/global_std_stream.rb +7 -1
  83. data/lib/rubocop/cop/style/hash_except.rb +8 -5
  84. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +0 -1
  85. data/lib/rubocop/cop/style/invertible_unless_condition.rb +2 -2
  86. data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +77 -43
  87. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +5 -0
  88. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +1 -1
  89. data/lib/rubocop/cop/style/missing_else.rb +0 -4
  90. data/lib/rubocop/cop/style/multiline_when_then.rb +0 -4
  91. data/lib/rubocop/cop/style/quoted_symbols.rb +1 -1
  92. data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
  93. data/lib/rubocop/cop/style/redundant_condition.rb +0 -1
  94. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -1
  95. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +8 -24
  96. data/lib/rubocop/cop/style/send_with_literal_method_name.rb +15 -1
  97. data/lib/rubocop/cop/style/sole_nested_conditional.rb +21 -2
  98. data/lib/rubocop/cop/style/super_arguments.rb +28 -10
  99. data/lib/rubocop/cop/style/symbol_proc.rb +8 -1
  100. data/lib/rubocop/cop/style/while_until_do.rb +0 -2
  101. data/lib/rubocop/cop/style/while_until_modifier.rb +0 -1
  102. data/lib/rubocop/cop/style/zero_length_predicate.rb +32 -24
  103. data/lib/rubocop/cop/team.rb +8 -0
  104. data/lib/rubocop/cop/util.rb +7 -1
  105. data/lib/rubocop/cop/variable_force.rb +13 -1
  106. data/lib/rubocop/cops_documentation_generator.rb +1 -1
  107. data/lib/rubocop/core_ext/string.rb +2 -6
  108. data/lib/rubocop/ext/regexp_node.rb +9 -31
  109. data/lib/rubocop/ext/regexp_parser.rb +4 -21
  110. data/lib/rubocop/formatter/html_formatter.rb +3 -1
  111. data/lib/rubocop/options.rb +3 -1
  112. data/lib/rubocop/rspec/shared_contexts.rb +20 -0
  113. data/lib/rubocop/rspec/support.rb +1 -0
  114. data/lib/rubocop/server/cache.rb +10 -0
  115. data/lib/rubocop/server/client_command/exec.rb +2 -2
  116. data/lib/rubocop/server/client_command/start.rb +1 -1
  117. data/lib/rubocop/server/core.rb +4 -0
  118. data/lib/rubocop/version.rb +1 -1
  119. data/lib/rubocop.rb +6 -0
  120. metadata +7 -6
@@ -28,16 +28,12 @@ module RuboCop
28
28
  # @example
29
29
  #
30
30
  # # bad
31
- #
32
31
  # def some_method
33
32
  # some_var = 1
34
33
  # do_something
35
34
  # end
36
35
  #
37
- # @example
38
- #
39
36
  # # good
40
- #
41
37
  # def some_method
42
38
  # some_var = 1
43
39
  # do_something(some_var)
@@ -61,7 +57,7 @@ module RuboCop
61
57
  def check_for_unused_assignments(variable)
62
58
  return if variable.should_be_unused?
63
59
 
64
- variable.assignments.each do |assignment|
60
+ variable.assignments.reverse_each do |assignment|
65
61
  next if assignment.used? || part_of_ignored_node?(assignment.node)
66
62
 
67
63
  message = message_for_useless_assignment(assignment)
@@ -10,17 +10,13 @@ module RuboCop
10
10
  # @example
11
11
  #
12
12
  # # bad
13
- #
14
13
  # begin
15
14
  # do_something
16
15
  # else
17
16
  # do_something_else # This will never be run.
18
17
  # end
19
18
  #
20
- # @example
21
- #
22
19
  # # good
23
- #
24
20
  # begin
25
21
  # do_something
26
22
  # rescue
@@ -17,16 +17,12 @@ module RuboCop
17
17
  # @example
18
18
  #
19
19
  # # bad
20
- #
21
20
  # def something
22
21
  # x = Something.new
23
22
  # x.attr = 5
24
23
  # end
25
24
  #
26
- # @example
27
- #
28
25
  # # good
29
- #
30
26
  # def something
31
27
  # x = Something.new
32
28
  # x.attr = 5
@@ -111,6 +111,8 @@ module RuboCop
111
111
  end
112
112
 
113
113
  def check_expression(expr)
114
+ expr = expr.body if expr.if_type? && expr.modifier_form?
115
+
114
116
  check_literal(expr)
115
117
  check_var(expr)
116
118
  check_self(expr)
@@ -121,6 +123,7 @@ module RuboCop
121
123
  end
122
124
 
123
125
  def check_void_op(node, &block)
126
+ node = node.children.first while node.begin_type?
124
127
  return unless node.send_type? && OPERATORS.include?(node.method_name)
125
128
  return if block && yield(node)
126
129
 
@@ -211,6 +214,8 @@ module RuboCop
211
214
  end
212
215
 
213
216
  def autocorrect_void_expression(corrector, node)
217
+ return if node.parent.if_type? && node.parent.modifier_form?
218
+
214
219
  corrector.remove(range_with_surrounding_space(range: node.source_range, side: :left))
215
220
  end
216
221
 
@@ -3,12 +3,12 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Metrics
6
- # Checks for excessive nesting of conditional and looping
7
- # constructs.
6
+ # Checks for excessive nesting of conditional and looping constructs.
8
7
  #
9
- # You can configure if blocks are considered using the `CountBlocks`
10
- # option. When set to `false` (the default) blocks are not counted
11
- # towards the nesting level. Set to `true` to count blocks as well.
8
+ # You can configure if blocks are considered using the `CountBlocks` and `CountModifierForms`
9
+ # options. When both are set to `false` (the default) blocks and modifier forms are not
10
+ # counted towards the nesting level. Set them to `true` to include these in the nesting level
11
+ # calculation as well.
12
12
  #
13
13
  # The maximum level of nesting allowed is configurable.
14
14
  class BlockNesting < Base
@@ -27,7 +27,7 @@ module RuboCop
27
27
 
28
28
  def check_nesting_level(node, max, current_level)
29
29
  if consider_node?(node)
30
- current_level += 1 unless node.if_type? && node.elsif?
30
+ current_level += 1 if count_if_block?(node)
31
31
  if current_level > max
32
32
  self.max = current_level
33
33
  unless part_of_ignored_node?(node)
@@ -41,6 +41,14 @@ module RuboCop
41
41
  end
42
42
  end
43
43
 
44
+ def count_if_block?(node)
45
+ return true unless node.if_type?
46
+ return false if node.elsif?
47
+ return count_modifier_forms? if node.modifier_form?
48
+
49
+ true
50
+ end
51
+
44
52
  def consider_node?(node)
45
53
  return true if NESTING_BLOCKS.include?(node.type)
46
54
 
@@ -52,7 +60,11 @@ module RuboCop
52
60
  end
53
61
 
54
62
  def count_blocks?
55
- cop_config['CountBlocks']
63
+ cop_config.fetch('CountBlocks', false)
64
+ end
65
+
66
+ def count_modifier_forms?
67
+ cop_config.fetch('CountModifierForms', false)
56
68
  end
57
69
  end
58
70
  end
@@ -65,8 +65,12 @@ module RuboCop
65
65
  inner.begin_pos >= outer.begin_pos && inner.end_pos <= outer.end_pos
66
66
  end
67
67
 
68
- # @deprecated Use processed_source.comment_at_line(line)
68
+ # @deprecated Use processed_source.line_with_comment?(line)
69
69
  def end_of_line_comment(line)
70
+ warn Rainbow(<<~WARNING).yellow, uplevel: 1
71
+ `end_of_line_comment` is deprecated. Use `processed_source.line_with_comment?` instead.
72
+ WARNING
73
+
70
74
  processed_source.line_with_comment?(line)
71
75
  end
72
76
 
@@ -15,7 +15,13 @@ module RuboCop
15
15
  end
16
16
 
17
17
  # @deprecated Use allowed_method? instead
18
- alias ignored_method? allowed_method?
18
+ def ignored_method?
19
+ warn Rainbow(<<~WARNING).yellow, uplevel: 1
20
+ `ignored_method?` is deprecated. Use `allowed_method?` instead.
21
+ WARNING
22
+
23
+ allowed_method?
24
+ end
19
25
 
20
26
  # @api public
21
27
  def allowed_methods
@@ -18,14 +18,26 @@ module RuboCop
18
18
  end
19
19
 
20
20
  # @deprecated Use allowed_line? instead
21
- alias ignored_line? allowed_line?
21
+ def ignored_line?
22
+ warn Rainbow(<<~WARNING).yellow, uplevel: 1
23
+ `ignored_line?` is deprecated. Use `allowed_line?` instead.
24
+ WARNING
25
+
26
+ allowed_line?
27
+ end
22
28
 
23
29
  def matches_allowed_pattern?(line)
24
30
  allowed_patterns.any? { |pattern| Regexp.new(pattern).match?(line) }
25
31
  end
26
32
 
27
- # @deprecated Use matches_allowed_pattern?? instead
28
- alias matches_ignored_pattern? matches_allowed_pattern?
33
+ # @deprecated Use matches_allowed_pattern? instead
34
+ def matches_ignored_pattern?
35
+ warn Rainbow(<<~WARNING).yellow, uplevel: 1
36
+ `matches_ignored_pattern?` is deprecated. Use `matches_allowed_pattern?` instead.
37
+ WARNING
38
+
39
+ matches_allowed_pattern?
40
+ end
29
41
 
30
42
  def allowed_patterns
31
43
  # Since there could be a pattern specified in the default config, merge the two
@@ -4,11 +4,15 @@ module RuboCop
4
4
  module Cop
5
5
  # Handles `Max` configuration parameters, especially setting them to an
6
6
  # appropriate value with --auto-gen-config.
7
- # @deprecated Use `exclude_limit ParameterName` instead.
7
+ # @deprecated Use `exclude_limit <ParameterName>` instead.
8
8
  module ConfigurableMax
9
9
  private
10
10
 
11
11
  def max=(value)
12
+ warn Rainbow(<<~WARNING).yellow, uplevel: 1
13
+ `max=` is deprecated. Use `exclude_limit <ParameterName>` instead.
14
+ WARNING
15
+
12
16
  cfg = config_to_allow_offenses
13
17
  cfg[:exclude_limit] ||= {}
14
18
  current_max = cfg[:exclude_limit][max_parameter_name]
@@ -18,6 +18,10 @@ module RuboCop
18
18
 
19
19
  # @deprecated Use ResbodyNode#exceptions instead
20
20
  def rescued_exceptions(resbody)
21
+ warn Rainbow(<<~WARNING).yellow, uplevel: 1
22
+ `rescued_exceptions` is deprecated. Use `ResbodyNode#exceptions` instead.
23
+ WARNING
24
+
21
25
  rescue_group, = *resbody
22
26
  if rescue_group
23
27
  rescue_group.values
@@ -3,59 +3,75 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Naming
6
- # Checks that predicate methods names end with a question mark and
6
+ # Checks that predicate method names end with a question mark and
7
7
  # do not start with a forbidden prefix.
8
8
  #
9
- # A method is determined to be a predicate method if its name starts
10
- # with one of the prefixes defined in the `NamePrefix` configuration.
11
- # You can change what prefixes are considered by changing this option.
12
- # Any method name that starts with one of these prefixes is required by
13
- # the cop to end with a `?`. Other methods can be allowed by adding to
14
- # the `AllowedMethods` configuration.
9
+ # A method is determined to be a predicate method if its name starts with
10
+ # one of the prefixes listed in the `NamePrefix` configuration. The list
11
+ # defaults to `is_`, `has_`, and `have_` but may be overridden.
15
12
  #
16
- # NOTE: The `is_a?` method is allowed by default.
13
+ # Predicate methods must end with a question mark.
17
14
  #
18
- # If `ForbiddenPrefixes` is set, methods that start with the configured
19
- # prefixes will not be allowed and will be removed by autocorrection.
15
+ # When `ForbiddenPrefixes` is also set (as it is by default), predicate
16
+ # methods which begin with a forbidden prefix are not allowed, even if
17
+ # they end with a `?`. These methods should be changed to remove the
18
+ # prefix.
20
19
  #
21
- # In other words, if `ForbiddenPrefixes` is empty, a method named `is_foo`
22
- # will register an offense only due to the lack of question mark (and will be
23
- # autocorrected to `is_foo?`). If `ForbiddenPrefixes` contains `is_`,
24
- # `is_foo` will register an offense both because the ? is missing and because of
25
- # the `is_` prefix, and will be corrected to `foo?`.
26
- #
27
- # NOTE: `ForbiddenPrefixes` is only applied to prefixes in `NamePrefix`;
28
- # a prefix in the former but not the latter will not be considered by
29
- # this cop.
30
- #
31
- # @example
20
+ # @example NamePrefix: ['is_', 'has_', 'have_'] (default)
32
21
  # # bad
33
22
  # def is_even(value)
34
23
  # end
35
24
  #
36
- # def is_even?(value)
25
+ # # When ForbiddenPrefixes: ['is_', 'has_', 'have_'] (default)
26
+ # # good
27
+ # def even?(value)
37
28
  # end
38
29
  #
30
+ # # When ForbiddenPrefixes: []
39
31
  # # good
40
- # def even?(value)
32
+ # def is_even?(value)
41
33
  # end
42
34
  #
35
+ # @example NamePrefix: ['seems_to_be_']
43
36
  # # bad
44
- # def has_value
37
+ # def seems_to_be_even(value)
45
38
  # end
46
39
  #
47
- # def has_value?
40
+ # # When ForbiddenPrefixes: ['seems_to_be_']
41
+ # # good
42
+ # def even?(value)
48
43
  # end
49
44
  #
45
+ # # When ForbiddenPrefixes: []
50
46
  # # good
51
- # def value?
47
+ # def seems_to_be_even?(value)
52
48
  # end
53
49
  #
54
50
  # @example AllowedMethods: ['is_a?'] (default)
51
+ # # Despite starting with the `is_` prefix, this method is allowed
55
52
  # # good
56
53
  # def is_a?(value)
57
54
  # end
58
55
  #
56
+ # @example AllowedMethods: ['is_even?']
57
+ # # good
58
+ # def is_even?(value)
59
+ # end
60
+ #
61
+ # @example MethodDefinitionMacros: ['define_method', 'define_singleton_method'] (default)
62
+ # # bad
63
+ # define_method(:is_even) { |value| }
64
+ #
65
+ # # good
66
+ # define_method(:even?) { |value| }
67
+ #
68
+ # @example MethodDefinitionMacros: ['def_node_matcher']
69
+ # # bad
70
+ # def_node_matcher(:is_even) { |value| }
71
+ #
72
+ # # good
73
+ # # def_node_matcher(:even?) { |value| }
74
+ #
59
75
  class PredicateName < Base
60
76
  include AllowedMethods
61
77
 
@@ -93,6 +109,16 @@ module RuboCop
93
109
  end
94
110
  alias on_defs on_def
95
111
 
112
+ def validate_config
113
+ forbidden_prefixes.each do |forbidden_prefix|
114
+ next if predicate_prefixes.include?(forbidden_prefix)
115
+
116
+ raise ValidationError, <<~MSG.chomp
117
+ The `Naming/PredicateName` cop is misconfigured. Prefix #{forbidden_prefix} must be included in NamePrefix because it is included in ForbiddenPrefixes.
118
+ MSG
119
+ end
120
+ end
121
+
96
122
  private
97
123
 
98
124
  def allowed_method_name?(method_name, prefix)
@@ -113,13 +113,21 @@ module RuboCop
113
113
  end
114
114
  end
115
115
 
116
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
116
117
  def correct_node(corrector, node, offending_name, preferred_name)
117
118
  return unless node
118
119
 
119
120
  node.each_node(:lvar, :lvasgn, :masgn) do |child_node|
120
121
  next unless variable_name_matches?(child_node, offending_name)
121
122
 
122
- corrector.replace(child_node, preferred_name) if child_node.lvar_type?
123
+ if child_node.lvar_type?
124
+ parent_node = child_node.parent
125
+ if parent_node.respond_to?(:value_omission?) && parent_node.value_omission?
126
+ corrector.insert_after(parent_node.loc.operator, " #{preferred_name}")
127
+ else
128
+ corrector.replace(child_node, preferred_name)
129
+ end
130
+ end
123
131
 
124
132
  if child_node.masgn_type? || child_node.lvasgn_type?
125
133
  correct_reassignment(corrector, child_node, offending_name, preferred_name)
@@ -127,6 +135,7 @@ module RuboCop
127
135
  end
128
136
  end
129
137
  end
138
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
130
139
 
131
140
  # If the exception variable is reassigned, that assignment needs to be corrected.
132
141
  # Further `lvar` nodes will not be corrected though since they now refer to a
@@ -148,7 +148,7 @@ module RuboCop
148
148
 
149
149
  restarg, kwrestarg, blockarg = extract_forwardable_args(node.arguments)
150
150
  forwardable_args = redundant_forwardable_named_args(restarg, kwrestarg, blockarg)
151
- send_nodes = node.each_descendant(:send, :csend, :super).to_a
151
+ send_nodes = node.each_descendant(:send, :csend, :super, :yield).to_a
152
152
 
153
153
  send_classifications = classify_send_nodes(
154
154
  node, send_nodes, non_splat_or_block_pass_lvar_references(node.body), forwardable_args
@@ -28,8 +28,6 @@ module RuboCop
28
28
  # # good (without parentheses it's a syntax error)
29
29
  # def foo() do_something end
30
30
  #
31
- # @example
32
- #
33
31
  # # bad
34
32
  # def Baz.foo()
35
33
  # do_something
@@ -16,7 +16,6 @@ module RuboCop
16
16
  # # good
17
17
  # 5.times { }
18
18
  #
19
- # @example
20
19
  # # bad
21
20
  # (0...10).each {}
22
21
  #
@@ -17,6 +17,18 @@ module RuboCop
17
17
  # will not attempt to automatically add a binding, or add filename and
18
18
  # line values.
19
19
  #
20
+ # NOTE: This cop works only when a string literal is given as a code string.
21
+ # No offense is reported if a string variable is given as below:
22
+ #
23
+ # [source,ruby]
24
+ # ----
25
+ # code = <<-RUBY
26
+ # def do_something
27
+ # end
28
+ # RUBY
29
+ # eval code # not checked.
30
+ # ----
31
+ #
20
32
  # @example
21
33
  # # bad
22
34
  # eval <<-RUBY
@@ -42,17 +54,6 @@ module RuboCop
42
54
  # end
43
55
  # RUBY
44
56
  #
45
- # This cop works only when a string literal is given as a code string.
46
- # No offense is reported if a string variable is given as below:
47
- #
48
- # @example
49
- # # not checked
50
- # code = <<-RUBY
51
- # def do_something
52
- # end
53
- # RUBY
54
- # eval code
55
- #
56
57
  class EvalWithLocation < Base
57
58
  extend AutoCorrector
58
59
 
@@ -6,8 +6,7 @@ module RuboCop
6
6
  # Favor `File.(bin)read` convenience methods.
7
7
  #
8
8
  # @example
9
- # ## text mode
10
- # # bad
9
+ # # bad - text mode
11
10
  # File.open(filename).read
12
11
  # File.open(filename, &:read)
13
12
  # File.open(filename) { |f| f.read }
@@ -23,9 +22,7 @@ module RuboCop
23
22
  # # good
24
23
  # File.read(filename)
25
24
  #
26
- # @example
27
- # ## binary mode
28
- # # bad
25
+ # # bad - binary mode
29
26
  # File.open(filename, 'rb').read
30
27
  # File.open(filename, 'rb', &:read)
31
28
  # File.open(filename, 'rb') do |f|
@@ -17,8 +17,7 @@ module RuboCop
17
17
  # ----
18
18
  #
19
19
  # @example
20
- # ## text mode
21
- # # bad
20
+ # # bad - text mode
22
21
  # File.open(filename, 'w').write(content)
23
22
  # File.open(filename, 'w') do |f|
24
23
  # f.write(content)
@@ -27,9 +26,7 @@ module RuboCop
27
26
  # # good
28
27
  # File.write(filename, content)
29
28
  #
30
- # @example
31
- # ## binary mode
32
- # # bad
29
+ # # bad - binary mode
33
30
  # File.open(filename, 'wb').write(content)
34
31
  # File.open(filename, 'wb') do |f|
35
32
  # f.write(content)
@@ -44,7 +44,9 @@ module RuboCop
44
44
  PATTERN
45
45
 
46
46
  def on_const(node)
47
- const_name = node.children[1]
47
+ return if namespaced?(node)
48
+
49
+ const_name = node.short_name
48
50
  return unless STD_STREAMS.include?(const_name)
49
51
 
50
52
  gvar_name = gvar_name(const_name).to_sym
@@ -61,6 +63,10 @@ module RuboCop
61
63
  format(MSG, gvar_name: gvar_name(const_name), const_name: const_name)
62
64
  end
63
65
 
66
+ def namespaced?(node)
67
+ !node.namespace.nil? && (node.relative? || !node.namespace.cbase_type?)
68
+ end
69
+
64
70
  def gvar_name(const_name)
65
71
  "$#{const_name.to_s.downcase}"
66
72
  end
@@ -23,9 +23,9 @@ module RuboCop
23
23
  # {foo: 1, bar: 2, baz: 3}.reject {|k, v| k == :bar }
24
24
  # {foo: 1, bar: 2, baz: 3}.select {|k, v| k != :bar }
25
25
  # {foo: 1, bar: 2, baz: 3}.filter {|k, v| k != :bar }
26
- # {foo: 1, bar: 2, baz: 3}.reject {|k, v| %i[foo bar].include?(k) }
27
- # {foo: 1, bar: 2, baz: 3}.select {|k, v| !%i[foo bar].include?(k) }
28
- # {foo: 1, bar: 2, baz: 3}.filter {|k, v| !%i[foo bar].include?(k) }
26
+ # {foo: 1, bar: 2, baz: 3}.reject {|k, v| %i[bar].include?(k) }
27
+ # {foo: 1, bar: 2, baz: 3}.select {|k, v| !%i[bar].include?(k) }
28
+ # {foo: 1, bar: 2, baz: 3}.filter {|k, v| !%i[bar].include?(k) }
29
29
  #
30
30
  # # good
31
31
  # {foo: 1, bar: 2, baz: 3}.except(:bar)
@@ -73,8 +73,9 @@ module RuboCop
73
73
  PATTERN
74
74
 
75
75
  def on_send(node)
76
+ method_name = node.method_name
76
77
  block = node.parent
77
- return unless bad_method?(block) && semantically_except_method?(node, block)
78
+ return unless bad_method?(method_name, block) && semantically_except_method?(node, block)
78
79
 
79
80
  except_key = except_key(block)
80
81
  return if except_key.nil? || !safe_to_register_offense?(block, except_key)
@@ -91,7 +92,7 @@ module RuboCop
91
92
  private
92
93
 
93
94
  # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
94
- def bad_method?(block)
95
+ def bad_method?(method_name, block)
95
96
  if active_support_extensions_enabled?
96
97
  bad_method_with_active_support?(block) do |key_arg, send_node|
97
98
  if send_node.method?(:in?) && send_node.receiver&.source != key_arg.source
@@ -103,6 +104,8 @@ module RuboCop
103
104
  end
104
105
  else
105
106
  bad_method_with_poro?(block) do |key_arg, send_node|
107
+ return false if method_name == :reject && block.body.method?(:!)
108
+
106
109
  !send_node.method?(:include?) || send_node.first_argument&.source == key_arg.source
107
110
  end
108
111
  end
@@ -44,7 +44,6 @@ module RuboCop
44
44
  # # good
45
45
  # foo == bar
46
46
  #
47
- # @example
48
47
  # # bad
49
48
  # if foo.do_something?
50
49
  # true
@@ -72,8 +72,8 @@ module RuboCop
72
72
 
73
73
  private
74
74
 
75
- def invertible?(node)
76
- case node.type
75
+ def invertible?(node) # rubocop:disable Metrics/CyclomaticComplexity
76
+ case node&.type
77
77
  when :begin
78
78
  invertible?(node.children.first)
79
79
  when :send