rubocop 0.90.0 → 0.91.0

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 +2 -2
  3. data/config/default.yml +35 -0
  4. data/lib/rubocop.rb +10 -1
  5. data/lib/rubocop/cli/command/execute_runner.rb +8 -0
  6. data/lib/rubocop/config_loader.rb +3 -3
  7. data/lib/rubocop/config_store.rb +3 -3
  8. data/lib/rubocop/cop/bundler/duplicated_gem.rb +5 -1
  9. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -0
  10. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +2 -0
  11. data/lib/rubocop/cop/generator.rb +1 -1
  12. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +1 -0
  13. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -0
  14. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +1 -0
  15. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -0
  16. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +1 -0
  17. data/lib/rubocop/cop/layout/begin_end_alignment.rb +77 -0
  18. data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
  19. data/lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb +6 -5
  20. data/lib/rubocop/cop/layout/end_alignment.rb +5 -10
  21. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +17 -4
  22. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -3
  23. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +1 -6
  24. data/lib/rubocop/cop/lint/ambiguous_operator.rb +2 -0
  25. data/lib/rubocop/cop/lint/big_decimal_new.rb +1 -2
  26. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +54 -0
  27. data/lib/rubocop/cop/lint/debugger.rb +2 -3
  28. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +1 -3
  29. data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -4
  30. data/lib/rubocop/cop/lint/duplicate_require.rb +7 -2
  31. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -0
  32. data/lib/rubocop/cop/lint/empty_file.rb +1 -4
  33. data/lib/rubocop/cop/lint/erb_new_arguments.rb +2 -0
  34. data/lib/rubocop/cop/lint/float_comparison.rb +2 -2
  35. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +2 -2
  36. data/lib/rubocop/cop/lint/identity_comparison.rb +49 -0
  37. data/lib/rubocop/cop/lint/inherit_exception.rb +2 -2
  38. data/lib/rubocop/cop/lint/multiple_comparison.rb +3 -1
  39. data/lib/rubocop/cop/lint/number_conversion.rb +1 -0
  40. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +1 -2
  41. data/lib/rubocop/cop/lint/raise_exception.rb +1 -0
  42. data/lib/rubocop/cop/lint/rand_one.rb +2 -1
  43. data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -0
  44. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +3 -1
  45. data/lib/rubocop/cop/lint/struct_new_override.rb +1 -0
  46. data/lib/rubocop/cop/lint/to_json.rb +16 -5
  47. data/lib/rubocop/cop/lint/unreachable_loop.rb +2 -1
  48. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +3 -1
  49. data/lib/rubocop/cop/lint/uri_regexp.rb +2 -1
  50. data/lib/rubocop/cop/lint/useless_method_definition.rb +20 -27
  51. data/lib/rubocop/cop/lint/useless_times.rb +97 -0
  52. data/lib/rubocop/cop/mixin/comments_help.rb +3 -9
  53. data/lib/rubocop/cop/mixin/configurable_naming.rb +2 -2
  54. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +9 -0
  55. data/lib/rubocop/cop/mixin/hash_transform_method.rb +9 -1
  56. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
  57. data/lib/rubocop/cop/naming/file_name.rb +1 -1
  58. data/lib/rubocop/cop/security/eval.rb +1 -0
  59. data/lib/rubocop/cop/security/json_load.rb +1 -0
  60. data/lib/rubocop/cop/security/marshal_load.rb +1 -0
  61. data/lib/rubocop/cop/security/open.rb +1 -0
  62. data/lib/rubocop/cop/security/yaml_load.rb +1 -0
  63. data/lib/rubocop/cop/style/access_modifier_declarations.rb +3 -11
  64. data/lib/rubocop/cop/style/alias.rb +2 -0
  65. data/lib/rubocop/cop/style/array_join.rb +1 -0
  66. data/lib/rubocop/cop/style/attr.rb +1 -0
  67. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +2 -0
  68. data/lib/rubocop/cop/style/case_equality.rb +3 -0
  69. data/lib/rubocop/cop/style/class_and_module_children.rb +2 -0
  70. data/lib/rubocop/cop/style/class_check.rb +6 -9
  71. data/lib/rubocop/cop/style/class_methods_definitions.rb +42 -16
  72. data/lib/rubocop/cop/style/class_vars.rb +1 -2
  73. data/lib/rubocop/cop/style/conditional_assignment.rb +49 -60
  74. data/lib/rubocop/cop/style/dir.rb +1 -0
  75. data/lib/rubocop/cop/style/double_negation.rb +1 -0
  76. data/lib/rubocop/cop/style/empty_literal.rb +3 -1
  77. data/lib/rubocop/cop/style/eval_with_location.rb +1 -3
  78. data/lib/rubocop/cop/style/even_odd.rb +1 -0
  79. data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -2
  80. data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
  81. data/lib/rubocop/cop/style/float_division.rb +2 -0
  82. data/lib/rubocop/cop/style/format_string.rb +1 -4
  83. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +12 -2
  84. data/lib/rubocop/cop/style/hash_transform_keys.rb +5 -11
  85. data/lib/rubocop/cop/style/hash_transform_values.rb +5 -11
  86. data/lib/rubocop/cop/style/implicit_runtime_error.rb +1 -0
  87. data/lib/rubocop/cop/style/lambda_call.rb +3 -1
  88. data/lib/rubocop/cop/style/mixin_usage.rb +1 -0
  89. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +14 -1
  90. data/lib/rubocop/cop/style/nil_comparison.rb +2 -0
  91. data/lib/rubocop/cop/style/non_nil_check.rb +2 -0
  92. data/lib/rubocop/cop/style/not.rb +1 -0
  93. data/lib/rubocop/cop/style/numeric_predicate.rb +1 -3
  94. data/lib/rubocop/cop/style/preferred_hash_methods.rb +2 -0
  95. data/lib/rubocop/cop/style/raise_args.rb +2 -0
  96. data/lib/rubocop/cop/style/random_with_offset.rb +1 -0
  97. data/lib/rubocop/cop/style/redundant_exception.rb +1 -3
  98. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -0
  99. data/lib/rubocop/cop/style/redundant_freeze.rb +2 -1
  100. data/lib/rubocop/cop/style/redundant_parentheses.rb +12 -3
  101. data/lib/rubocop/cop/style/redundant_self_assignment.rb +2 -2
  102. data/lib/rubocop/cop/style/redundant_sort.rb +1 -7
  103. data/lib/rubocop/cop/style/safe_navigation.rb +5 -0
  104. data/lib/rubocop/cop/style/sample.rb +2 -1
  105. data/lib/rubocop/cop/style/send.rb +2 -3
  106. data/lib/rubocop/cop/style/signal_exception.rb +2 -0
  107. data/lib/rubocop/cop/style/single_argument_dig.rb +1 -0
  108. data/lib/rubocop/cop/style/slicing_with_range.rb +2 -1
  109. data/lib/rubocop/cop/style/stderr_puts.rb +1 -0
  110. data/lib/rubocop/cop/style/string_concatenation.rb +16 -2
  111. data/lib/rubocop/cop/style/strip.rb +1 -0
  112. data/lib/rubocop/cop/style/unpack_first.rb +1 -0
  113. data/lib/rubocop/cop/style/zero_length_predicate.rb +1 -5
  114. data/lib/rubocop/core_ext/string.rb +1 -1
  115. data/lib/rubocop/ext/regexp_node.rb +4 -4
  116. data/lib/rubocop/options.rb +19 -1
  117. data/lib/rubocop/result_cache.rb +30 -13
  118. data/lib/rubocop/runner.rb +29 -14
  119. data/lib/rubocop/version.rb +1 -1
  120. metadata +8 -4
@@ -19,6 +19,7 @@ module RuboCop
19
19
  extend AutoCorrector
20
20
 
21
21
  MSG = 'Replace with `Integer#%<method>s?`.'
22
+ RESTRICT_ON_SEND = %i[== !=].freeze
22
23
 
23
24
  def_node_matcher :even_odd_candidate?, <<~PATTERN
24
25
  (send
@@ -52,6 +52,8 @@ module RuboCop
52
52
  'instead of ' \
53
53
  '`Pathname.new(__FILE__).parent.expand_path`.'
54
54
 
55
+ RESTRICT_ON_SEND = %i[expand_path].freeze
56
+
55
57
  def_node_matcher :file_expand_path, <<~PATTERN
56
58
  (send
57
59
  (const {nil? cbase} :File) :expand_path
@@ -75,8 +77,6 @@ module RuboCop
75
77
  PATTERN
76
78
 
77
79
  def on_send(node)
78
- return unless node.method?(:expand_path)
79
-
80
80
  if (current_path, default_dir = file_expand_path(node))
81
81
  inspect_offense_for_expand_path(node, current_path, default_dir)
82
82
  elsif (default_dir = pathname_parent_expand_path(node))
@@ -83,7 +83,7 @@ module RuboCop
83
83
  replacement = ' &block'
84
84
  replacement = ",#{replacement}" unless arg_range.source.end_with?(',')
85
85
  corrector.insert_after(arg_range, replacement) unless last_arg.blockarg_type?
86
- elsif node.send_type?
86
+ elsif node.call_type?
87
87
  corrector.insert_after(node, '(&block)')
88
88
  else
89
89
  corrector.insert_after(node.loc.name, '(&block)')
@@ -48,6 +48,8 @@ module RuboCop
48
48
  fdiv: 'Prefer using `fdiv` for float divisions.'
49
49
  }.freeze
50
50
 
51
+ RESTRICT_ON_SEND = %i[/].freeze
52
+
51
53
  def_node_matcher :right_coerce?, <<~PATTERN
52
54
  (send _ :/ (send _ :to_f))
53
55
  PATTERN
@@ -40,8 +40,7 @@ module RuboCop
40
40
  extend AutoCorrector
41
41
 
42
42
  MSG = 'Favor `%<prefer>s` over `%<current>s`.'
43
-
44
- FORMAT_METHODS = %i[format sprintf %].freeze
43
+ RESTRICT_ON_SEND = %i[format sprintf %].freeze
45
44
 
46
45
  def_node_matcher :formatter, <<~PATTERN
47
46
  {
@@ -56,8 +55,6 @@ module RuboCop
56
55
  PATTERN
57
56
 
58
57
  def on_send(node)
59
- return unless FORMAT_METHODS.include?(node.method_name)
60
-
61
58
  formatter(node) do |selector|
62
59
  detected_style = selector == :% ? :percent : selector
63
60
 
@@ -6,6 +6,9 @@ module RuboCop
6
6
  # Checks for presence or absence of braces around hash literal as a last
7
7
  # array item depending on configuration.
8
8
  #
9
+ # NOTE: This cop will ignore arrays where all items are hashes, regardless of
10
+ # EnforcedStyle.
11
+ #
9
12
  # @example EnforcedStyle: braces (default)
10
13
  # # bad
11
14
  # [1, 2, one: 1, two: 2]
@@ -13,6 +16,9 @@ module RuboCop
13
16
  # # good
14
17
  # [1, 2, { one: 1, two: 2 }]
15
18
  #
19
+ # # good
20
+ # [{ one: 1 }, { two: 2 }]
21
+ #
16
22
  # @example EnforcedStyle: no_braces
17
23
  # # bad
18
24
  # [1, 2, { one: 1, two: 2 }]
@@ -20,6 +26,8 @@ module RuboCop
20
26
  # # good
21
27
  # [1, 2, one: 1, two: 2]
22
28
  #
29
+ # # good
30
+ # [{ one: 1 }, { two: 2 }]
23
31
  class HashAsLastArrayItem < Base
24
32
  include ConfigurableEnforcedStyle
25
33
  extend AutoCorrector
@@ -38,9 +46,10 @@ module RuboCop
38
46
 
39
47
  def last_array_item?(node)
40
48
  parent = node.parent
41
- return false unless parent
49
+ return false unless parent&.array_type?
50
+ return false if parent.child_nodes.all?(&:hash_type?)
42
51
 
43
- parent.array_type? && parent.children.last.equal?(node)
52
+ parent.children.last.equal?(node)
44
53
  end
45
54
 
46
55
  def check_braces(node)
@@ -53,6 +62,7 @@ module RuboCop
53
62
 
54
63
  def check_no_braces(node)
55
64
  return unless node.braces?
65
+ return if node.children.empty? # Empty hash cannot be "unbraced"
56
66
 
57
67
  add_offense(node, message: 'Omit the braces around the hash.') do |corrector|
58
68
  corrector.remove(node.loc.begin)
@@ -34,15 +34,13 @@ module RuboCop
34
34
 
35
35
  def_node_matcher :on_bad_each_with_object, <<~PATTERN
36
36
  (block
37
- ({send csend}
38
- !{(send _ :each_with_index) (array ...)}
39
- :each_with_object (hash))
37
+ ({send csend} !#array_receiver? :each_with_object (hash))
40
38
  (args
41
39
  (mlhs
42
40
  (arg $_)
43
41
  (arg _val))
44
42
  (arg _memo))
45
- ({send csend} (lvar _memo) :[]= $_ $(lvar _val)))
43
+ ({send csend} (lvar _memo) :[]= $!`_memo $(lvar _val)))
46
44
  PATTERN
47
45
 
48
46
  def_node_matcher :on_bad_hash_brackets_map, <<~PATTERN
@@ -50,7 +48,7 @@ module RuboCop
50
48
  (const _ :Hash)
51
49
  :[]
52
50
  (block
53
- ({send csend} !(send _ :each_with_index) {:map :collect})
51
+ ({send csend} !#array_receiver? {:map :collect})
54
52
  (args
55
53
  (arg $_)
56
54
  (arg _val))
@@ -60,9 +58,7 @@ module RuboCop
60
58
  def_node_matcher :on_bad_map_to_h, <<~PATTERN
61
59
  ({send csend}
62
60
  (block
63
- ({send csend}
64
- !{(send _ :each_with_index) (array ...)}
65
- {:map :collect})
61
+ ({send csend} !#array_receiver? {:map :collect})
66
62
  (args
67
63
  (arg $_)
68
64
  (arg _val))
@@ -72,9 +68,7 @@ module RuboCop
72
68
 
73
69
  def_node_matcher :on_bad_to_h, <<~PATTERN
74
70
  (block
75
- ({send csend}
76
- !{(send _ :each_with_index) (array ...)}
77
- :to_h)
71
+ ({send csend} !#array_receiver? :to_h)
78
72
  (args
79
73
  (arg $_)
80
74
  (arg _val))
@@ -31,15 +31,13 @@ module RuboCop
31
31
 
32
32
  def_node_matcher :on_bad_each_with_object, <<~PATTERN
33
33
  (block
34
- ({send csend}
35
- !{(send _ :each_with_index) (array ...)}
36
- :each_with_object (hash))
34
+ ({send csend} !#array_receiver? :each_with_object (hash))
37
35
  (args
38
36
  (mlhs
39
37
  (arg _key)
40
38
  (arg $_))
41
39
  (arg _memo))
42
- ({send csend} (lvar _memo) :[]= $(lvar _key) $_))
40
+ ({send csend} (lvar _memo) :[]= $(lvar _key) $!`_memo))
43
41
  PATTERN
44
42
 
45
43
  def_node_matcher :on_bad_hash_brackets_map, <<~PATTERN
@@ -47,7 +45,7 @@ module RuboCop
47
45
  (const _ :Hash)
48
46
  :[]
49
47
  (block
50
- ({send csend} !(send _ :each_with_index) {:map :collect})
48
+ ({send csend} !#array_receiver? {:map :collect})
51
49
  (args
52
50
  (arg _key)
53
51
  (arg $_))
@@ -57,9 +55,7 @@ module RuboCop
57
55
  def_node_matcher :on_bad_map_to_h, <<~PATTERN
58
56
  ({send csend}
59
57
  (block
60
- ({send csend}
61
- !{(send _ :each_with_index) (array ...)}
62
- {:map :collect})
58
+ ({send csend} !#array_receiver? {:map :collect})
63
59
  (args
64
60
  (arg _key)
65
61
  (arg $_))
@@ -69,9 +65,7 @@ module RuboCop
69
65
 
70
66
  def_node_matcher :on_bad_to_h, <<~PATTERN
71
67
  (block
72
- ({send csend}
73
- !{(send _ :each_with_index) (array ...)}
74
- :to_h)
68
+ ({send csend} !#array_receiver? :to_h)
75
69
  (args
76
70
  (arg _key)
77
71
  (arg $_))
@@ -17,6 +17,7 @@ module RuboCop
17
17
  class ImplicitRuntimeError < Base
18
18
  MSG = 'Use `%<method>s` with an explicit exception class and message,' \
19
19
  ' rather than just a message.'
20
+ RESTRICT_ON_SEND = %i[raise fail].freeze
20
21
 
21
22
  def_node_matcher :implicit_runtime_error_raise_or_fail,
22
23
  '(send nil? ${:raise :fail} {str dstr})'
@@ -22,8 +22,10 @@ module RuboCop
22
22
  include ConfigurableEnforcedStyle
23
23
  extend AutoCorrector
24
24
 
25
+ RESTRICT_ON_SEND = %i[call].freeze
26
+
25
27
  def on_send(node)
26
- return unless node.receiver && node.method?(:call)
28
+ return unless node.receiver
27
29
 
28
30
  if offense?(node) && opposite_style_detected
29
31
  add_offense(node) do |corrector|
@@ -43,6 +43,7 @@ module RuboCop
43
43
  class MixinUsage < Base
44
44
  MSG = '`%<statement>s` is used at the top level. Use inside `class` ' \
45
45
  'or `module`.'
46
+ RESTRICT_ON_SEND = %i[include extend prepend].freeze
46
47
 
47
48
  def_node_matcher :include_statement, <<~PATTERN
48
49
  (send nil? ${:include :extend :prepend}
@@ -5,6 +5,9 @@ module RuboCop
5
5
  module Style
6
6
  # This cop checks for multi-line ternary op expressions.
7
7
  #
8
+ # NOTE: `return if ... else ... end` is syntax error. If `return` is used before
9
+ # multiline ternary operator expression, it cannot be auto-corrected.
10
+ #
8
11
  # @example
9
12
  # # bad
10
13
  # a = cond ?
@@ -29,9 +32,13 @@ module RuboCop
29
32
  'use `if` or `unless` instead.'
30
33
 
31
34
  def on_if(node)
32
- return unless node.ternary? && node.multiline?
35
+ return unless offense?(node)
33
36
 
34
37
  add_offense(node) do |corrector|
38
+ # `return if ... else ... end` is syntax error. If `return` is used before
39
+ # multiline ternary operator expression, it cannot be auto-corrected.
40
+ next unless offense?(node) && !node.parent.return_type?
41
+
35
42
  corrector.replace(node, <<~RUBY.chop)
36
43
  if #{node.condition.source}
37
44
  #{node.if_branch.source}
@@ -41,6 +48,12 @@ module RuboCop
41
48
  RUBY
42
49
  end
43
50
  end
51
+
52
+ private
53
+
54
+ def offense?(node)
55
+ node.ternary? && node.multiline?
56
+ end
44
57
  end
45
58
  end
46
59
  end
@@ -35,6 +35,8 @@ module RuboCop
35
35
  PREDICATE_MSG = 'Prefer the use of the `nil?` predicate.'
36
36
  EXPLICIT_MSG = 'Prefer the use of the `==` comparison.'
37
37
 
38
+ RESTRICT_ON_SEND = %i[== === nil?].freeze
39
+
38
40
  def_node_matcher :nil_comparison?, '(send _ {:== :===} nil)'
39
41
  def_node_matcher :nil_check?, '(send _ :nil?)'
40
42
 
@@ -41,6 +41,8 @@ module RuboCop
41
41
  class NonNilCheck < Base
42
42
  extend AutoCorrector
43
43
 
44
+ RESTRICT_ON_SEND = %i[!= nil? !].freeze
45
+
44
46
  def_node_matcher :not_equal_to_nil?, '(send _ :!= nil)'
45
47
  def_node_matcher :unless_check?, '(if (send _ :nil?) ...)'
46
48
  def_node_matcher :nil_check?, '(send _ :nil?)'
@@ -18,6 +18,7 @@ module RuboCop
18
18
  extend AutoCorrector
19
19
 
20
20
  MSG = 'Use `!` instead of `not`.'
21
+ RESTRICT_ON_SEND = %i[!].freeze
21
22
 
22
23
  OPPOSITE_METHODS = {
23
24
  :== => :!=,
@@ -54,11 +54,9 @@ module RuboCop
54
54
  'negative?' => '<'
55
55
  }.freeze
56
56
 
57
- COMPARISON_METHODS = %i[== > < positive? negative? zero?].to_set.freeze
57
+ RESTRICT_ON_SEND = %i[== > < positive? negative? zero?].freeze
58
58
 
59
59
  def on_send(node)
60
- return unless COMPARISON_METHODS.include?(node.method_name)
61
-
62
60
  numeric, replacement = check(node)
63
61
  return unless numeric
64
62
 
@@ -36,6 +36,8 @@ module RuboCop
36
36
  verbose: %i[key? value?]
37
37
  }.freeze
38
38
 
39
+ RESTRICT_ON_SEND = OFFENDING_SELECTORS.values.flatten.freeze
40
+
39
41
  def on_send(node)
40
42
  return unless node.arguments.one? && offending_selector?(node.method_name)
41
43
 
@@ -41,6 +41,8 @@ module RuboCop
41
41
  COMPACT_MSG = 'Provide an exception object ' \
42
42
  'as an argument to `%<method>s`.'
43
43
 
44
+ RESTRICT_ON_SEND = %i[raise fail].freeze
45
+
44
46
  def on_send(node)
45
47
  return unless node.command?(:raise) || node.command?(:fail)
46
48
 
@@ -28,6 +28,7 @@ module RuboCop
28
28
 
29
29
  MSG = 'Prefer ranges when generating random numbers instead of ' \
30
30
  'integers with offsets.'
31
+ RESTRICT_ON_SEND = %i[+ - succ pred next].freeze
31
32
 
32
33
  def_node_matcher :integer_op_rand?, <<~PATTERN
33
34
  (send
@@ -23,13 +23,11 @@ module RuboCop
23
23
  MSG_2 = 'Redundant `RuntimeError.new` call can be replaced with ' \
24
24
  'just the message.'
25
25
 
26
- RAISE_METHODS = %i[raise fail].freeze
26
+ RESTRICT_ON_SEND = %i[raise fail].freeze
27
27
 
28
28
  # Switch `raise RuntimeError, 'message'` to `raise 'message'`, and
29
29
  # `raise RuntimeError.new('message')` to `raise 'message'`.
30
30
  def on_send(node)
31
- return unless RAISE_METHODS.include?(node.method_name)
32
-
33
31
  fix_exploded(node) || fix_compact(node)
34
32
  end
35
33
 
@@ -28,6 +28,7 @@ module RuboCop
28
28
  extend AutoCorrector
29
29
 
30
30
  MSG = 'Redundant `.rb` file extension detected.'
31
+ RESTRICT_ON_SEND = %i[require require_relative].freeze
31
32
 
32
33
  def_node_matcher :require_call?, <<~PATTERN
33
34
  (send nil? {:require :require_relative} $str_type?)
@@ -17,9 +17,10 @@ module RuboCop
17
17
 
18
18
  MSG = 'Do not freeze immutable objects, as freezing them has no ' \
19
19
  'effect.'
20
+ RESTRICT_ON_SEND = %i[freeze].freeze
20
21
 
21
22
  def on_send(node)
22
- return unless node.receiver && node.method?(:freeze) &&
23
+ return unless node.receiver &&
23
24
  (immutable_literal?(node.receiver) ||
24
25
  operation_produces_immutable_object?(node.receiver))
25
26
 
@@ -26,9 +26,7 @@ module RuboCop
26
26
  '^^(block (send _ _ equal?(%0) ...) ...)'
27
27
 
28
28
  def on_begin(node)
29
- return if !parentheses?(node) || parens_allowed?(node)
30
- return if node.parent && (node.parent.while_post_type? ||
31
- node.parent.until_post_type?)
29
+ return if !parentheses?(node) || parens_allowed?(node) || ignore_syntax?(node)
32
30
 
33
31
  check(node)
34
32
  end
@@ -42,6 +40,13 @@ module RuboCop
42
40
  allowed_expression?(node)
43
41
  end
44
42
 
43
+ def ignore_syntax?(node)
44
+ return false unless (parent = node.parent)
45
+
46
+ parent.while_post_type? || parent.until_post_type? ||
47
+ like_method_argument_parentheses?(parent)
48
+ end
49
+
45
50
  def allowed_expression?(node)
46
51
  allowed_ancestor?(node) ||
47
52
  allowed_method_call?(node) ||
@@ -68,6 +73,10 @@ module RuboCop
68
73
  !ancestor.begin_type? && !ancestor.def_type? && !ancestor.block_type?
69
74
  end
70
75
 
76
+ def like_method_argument_parentheses?(node)
77
+ node.send_type? && node.arguments.size == 1 && !node.arithmetic_operation?
78
+ end
79
+
71
80
  def empty_parentheses?(node)
72
81
  # Don't flag `()`
73
82
  node.children.empty?
@@ -37,8 +37,8 @@ module RuboCop
37
37
  METHODS_RETURNING_SELF = %i[
38
38
  append clear collect! compare_by_identity concat delete_if
39
39
  fill initialize_copy insert keep_if map! merge! prepend push
40
- rehash replace reverse! rotate! shuffle! sort! sort_by! to_ary
41
- to_hash transform_keys! transform_values! unshift update
40
+ rehash replace reverse! rotate! shuffle! sort! sort_by!
41
+ transform_keys! transform_values! unshift update
42
42
  ].to_set.freeze
43
43
 
44
44
  ASSIGNMENT_TYPE_TO_RECEIVER_TYPE = {
@@ -55,7 +55,7 @@ module RuboCop
55
55
  MSG = 'Use `%<suggestion>s` instead of '\
56
56
  '`%<sorter>s...%<accessor_source>s`.'
57
57
 
58
- SORT_METHODS = %i[sort sort_by].freeze
58
+ RESTRICT_ON_SEND = %i[sort sort_by].freeze
59
59
 
60
60
  def_node_matcher :redundant_sort?, <<~MATCHER
61
61
  {
@@ -74,8 +74,6 @@ module RuboCop
74
74
  MATCHER
75
75
 
76
76
  def on_send(node)
77
- return unless sort_method?(node)
78
-
79
77
  if (sort_node, sorter, accessor = redundant_sort?(node.parent))
80
78
  ancestor = node.parent
81
79
  elsif (sort_node, sorter, accessor = redundant_sort?(node.parent&.parent))
@@ -113,10 +111,6 @@ module RuboCop
113
111
 
114
112
  private
115
113
 
116
- def sort_method?(node)
117
- SORT_METHODS.include?(node.method_name)
118
- end
119
-
120
114
  def offense_range(sort_node, ancestor)
121
115
  range_between(sort_node.loc.selector.begin_pos, ancestor.loc.expression.end_pos)
122
116
  end