rubocop 1.8.0 → 1.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (246) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -13
  3. data/assets/output.html.erb +1 -1
  4. data/config/default.yml +89 -22
  5. data/config/obsoletion.yml +4 -0
  6. data/lib/rubocop.rb +9 -0
  7. data/lib/rubocop/cli/command/auto_genenerate_config.rb +5 -4
  8. data/lib/rubocop/cli/command/execute_runner.rb +1 -1
  9. data/lib/rubocop/cli/command/suggest_extensions.rb +1 -1
  10. data/lib/rubocop/config.rb +5 -2
  11. data/lib/rubocop/config_loader.rb +7 -14
  12. data/lib/rubocop/config_store.rb +12 -1
  13. data/lib/rubocop/cop/base.rb +2 -1
  14. data/lib/rubocop/cop/bundler/duplicated_gem.rb +2 -1
  15. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -0
  16. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -0
  17. data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -0
  18. data/lib/rubocop/cop/exclude_limit.rb +26 -0
  19. data/lib/rubocop/cop/gemspec/date_assignment.rb +57 -0
  20. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -0
  21. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -0
  22. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +2 -0
  23. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +2 -0
  24. data/lib/rubocop/cop/generator.rb +3 -5
  25. data/lib/rubocop/cop/internal_affairs.rb +6 -1
  26. data/lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb +68 -0
  27. data/lib/rubocop/cop/internal_affairs/example_description.rb +90 -0
  28. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +1 -0
  29. data/lib/rubocop/cop/internal_affairs/node_destructuring.rb +2 -0
  30. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +151 -0
  31. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -0
  32. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +2 -0
  33. data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +62 -0
  34. data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +65 -0
  35. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -0
  36. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +3 -0
  37. data/lib/rubocop/cop/internal_affairs/style_detected_api_use.rb +4 -0
  38. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -0
  39. data/lib/rubocop/cop/layout/block_alignment.rb +1 -0
  40. data/lib/rubocop/cop/layout/class_structure.rb +8 -2
  41. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +37 -17
  42. data/lib/rubocop/cop/layout/extra_spacing.rb +2 -2
  43. data/lib/rubocop/cop/layout/first_argument_indentation.rb +22 -3
  44. data/lib/rubocop/cop/layout/indentation_width.rb +1 -0
  45. data/lib/rubocop/cop/layout/line_length.rb +2 -1
  46. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +26 -0
  47. data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -1
  48. data/lib/rubocop/cop/layout/space_before_brackets.rb +9 -4
  49. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  50. data/lib/rubocop/cop/lint/big_decimal_new.rb +1 -0
  51. data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -0
  52. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +2 -0
  53. data/lib/rubocop/cop/lint/constant_resolution.rb +1 -0
  54. data/lib/rubocop/cop/lint/debugger.rb +60 -14
  55. data/lib/rubocop/cop/lint/deprecated_constants.rb +5 -0
  56. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +14 -4
  57. data/lib/rubocop/cop/lint/duplicate_branch.rb +1 -1
  58. data/lib/rubocop/cop/lint/duplicate_methods.rb +3 -0
  59. data/lib/rubocop/cop/lint/duplicate_require.rb +3 -2
  60. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -0
  61. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  62. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -0
  63. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -0
  64. data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +1 -0
  65. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -0
  66. data/lib/rubocop/cop/lint/inherit_exception.rb +1 -0
  67. data/lib/rubocop/cop/lint/multiple_comparison.rb +5 -4
  68. data/lib/rubocop/cop/lint/nested_method_definition.rb +3 -0
  69. data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -0
  70. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +7 -0
  71. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +3 -0
  72. data/lib/rubocop/cop/lint/number_conversion.rb +43 -6
  73. data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +47 -0
  74. data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +39 -0
  75. data/lib/rubocop/cop/lint/raise_exception.rb +2 -0
  76. data/lib/rubocop/cop/lint/rand_one.rb +1 -0
  77. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +5 -3
  78. data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -0
  79. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +1 -0
  80. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +7 -3
  81. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -0
  82. data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -0
  83. data/lib/rubocop/cop/lint/redundant_with_object.rb +1 -0
  84. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -0
  85. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -0
  86. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -0
  87. data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -0
  88. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -0
  89. data/lib/rubocop/cop/lint/struct_new_override.rb +1 -0
  90. data/lib/rubocop/cop/lint/symbol_conversion.rb +103 -0
  91. data/lib/rubocop/cop/lint/to_enum_arguments.rb +3 -0
  92. data/lib/rubocop/cop/lint/triple_quotes.rb +71 -0
  93. data/lib/rubocop/cop/lint/unified_integer.rb +1 -0
  94. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +5 -0
  95. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -0
  96. data/lib/rubocop/cop/lint/unreachable_loop.rb +1 -0
  97. data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -0
  98. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +1 -0
  99. data/lib/rubocop/cop/lint/useless_access_modifier.rb +4 -0
  100. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -0
  101. data/lib/rubocop/cop/lint/useless_times.rb +3 -0
  102. data/lib/rubocop/cop/message_annotator.rb +4 -1
  103. data/lib/rubocop/cop/metrics/block_nesting.rb +2 -2
  104. data/lib/rubocop/cop/metrics/module_length.rb +1 -0
  105. data/lib/rubocop/cop/metrics/parameter_lists.rb +6 -2
  106. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +6 -4
  107. data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +2 -0
  108. data/lib/rubocop/cop/mixin/check_line_breakable.rb +5 -0
  109. data/lib/rubocop/cop/mixin/code_length.rb +3 -1
  110. data/lib/rubocop/cop/mixin/comments_help.rb +0 -1
  111. data/lib/rubocop/cop/mixin/configurable_max.rb +1 -0
  112. data/lib/rubocop/cop/mixin/def_node.rb +1 -0
  113. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +3 -0
  114. data/lib/rubocop/cop/mixin/empty_parameter.rb +1 -0
  115. data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -0
  116. data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -0
  117. data/lib/rubocop/cop/mixin/method_complexity.rb +4 -1
  118. data/lib/rubocop/cop/mixin/negative_conditional.rb +3 -0
  119. data/lib/rubocop/cop/mixin/preferred_delimiters.rb +3 -3
  120. data/lib/rubocop/cop/mixin/rational_literal.rb +1 -0
  121. data/lib/rubocop/cop/mixin/safe_assignment.rb +5 -0
  122. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +5 -1
  123. data/lib/rubocop/cop/mixin/visibility_help.rb +1 -0
  124. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -0
  125. data/lib/rubocop/cop/naming/constant_name.rb +2 -0
  126. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +2 -0
  127. data/lib/rubocop/cop/naming/method_name.rb +3 -0
  128. data/lib/rubocop/cop/naming/predicate_name.rb +1 -0
  129. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +38 -5
  130. data/lib/rubocop/cop/naming/variable_number.rb +1 -1
  131. data/lib/rubocop/cop/registry.rb +1 -1
  132. data/lib/rubocop/cop/security/eval.rb +1 -0
  133. data/lib/rubocop/cop/security/json_load.rb +1 -0
  134. data/lib/rubocop/cop/security/marshal_load.rb +1 -0
  135. data/lib/rubocop/cop/security/open.rb +1 -0
  136. data/lib/rubocop/cop/security/yaml_load.rb +1 -0
  137. data/lib/rubocop/cop/severity.rb +3 -3
  138. data/lib/rubocop/cop/style/access_modifier_declarations.rb +1 -0
  139. data/lib/rubocop/cop/style/alias.rb +1 -0
  140. data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -0
  141. data/lib/rubocop/cop/style/array_coercion.rb +2 -0
  142. data/lib/rubocop/cop/style/array_join.rb +1 -0
  143. data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
  144. data/lib/rubocop/cop/style/attr.rb +1 -0
  145. data/lib/rubocop/cop/style/case_equality.rb +2 -1
  146. data/lib/rubocop/cop/style/class_equality_comparison.rb +1 -0
  147. data/lib/rubocop/cop/style/collection_compact.rb +2 -0
  148. data/lib/rubocop/cop/style/colon_method_call.rb +1 -0
  149. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  150. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -0
  151. data/lib/rubocop/cop/style/constant_visibility.rb +28 -0
  152. data/lib/rubocop/cop/style/date_time.rb +3 -0
  153. data/lib/rubocop/cop/style/dir.rb +1 -0
  154. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +49 -9
  155. data/lib/rubocop/cop/style/documentation.rb +5 -0
  156. data/lib/rubocop/cop/style/documentation_method.rb +1 -0
  157. data/lib/rubocop/cop/style/double_negation.rb +3 -2
  158. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -0
  159. data/lib/rubocop/cop/style/each_with_object.rb +1 -0
  160. data/lib/rubocop/cop/style/empty_literal.rb +9 -0
  161. data/lib/rubocop/cop/style/endless_method.rb +1 -0
  162. data/lib/rubocop/cop/style/eval_with_location.rb +140 -49
  163. data/lib/rubocop/cop/style/even_odd.rb +1 -0
  164. data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -0
  165. data/lib/rubocop/cop/style/explicit_block_argument.rb +12 -1
  166. data/lib/rubocop/cop/style/exponential_notation.rb +6 -7
  167. data/lib/rubocop/cop/style/float_division.rb +7 -0
  168. data/lib/rubocop/cop/style/format_string.rb +2 -0
  169. data/lib/rubocop/cop/style/format_string_token.rb +19 -2
  170. data/lib/rubocop/cop/style/global_std_stream.rb +1 -0
  171. data/lib/rubocop/cop/style/hash_conversion.rb +105 -0
  172. data/lib/rubocop/cop/style/hash_each_methods.rb +1 -0
  173. data/lib/rubocop/cop/style/hash_except.rb +1 -0
  174. data/lib/rubocop/cop/style/hash_like_case.rb +1 -0
  175. data/lib/rubocop/cop/style/hash_transform_keys.rb +4 -0
  176. data/lib/rubocop/cop/style/hash_transform_values.rb +4 -0
  177. data/lib/rubocop/cop/style/if_inside_else.rb +14 -7
  178. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +122 -0
  179. data/lib/rubocop/cop/style/implicit_runtime_error.rb +1 -0
  180. data/lib/rubocop/cop/style/inverse_methods.rb +2 -0
  181. data/lib/rubocop/cop/style/min_max.rb +1 -0
  182. data/lib/rubocop/cop/style/mixin_usage.rb +2 -0
  183. data/lib/rubocop/cop/style/module_function.rb +5 -0
  184. data/lib/rubocop/cop/style/multiple_comparison.rb +21 -2
  185. data/lib/rubocop/cop/style/mutable_constant.rb +3 -0
  186. data/lib/rubocop/cop/style/negated_if_else_condition.rb +1 -0
  187. data/lib/rubocop/cop/style/nil_comparison.rb +6 -0
  188. data/lib/rubocop/cop/style/nil_lambda.rb +1 -0
  189. data/lib/rubocop/cop/style/non_nil_check.rb +30 -13
  190. data/lib/rubocop/cop/style/numeric_literals.rb +6 -9
  191. data/lib/rubocop/cop/style/numeric_predicate.rb +4 -1
  192. data/lib/rubocop/cop/style/option_hash.rb +1 -0
  193. data/lib/rubocop/cop/style/or_assignment.rb +2 -0
  194. data/lib/rubocop/cop/style/parallel_assignment.rb +6 -0
  195. data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -0
  196. data/lib/rubocop/cop/style/proc.rb +1 -0
  197. data/lib/rubocop/cop/style/random_with_offset.rb +5 -0
  198. data/lib/rubocop/cop/style/redundant_assignment.rb +1 -0
  199. data/lib/rubocop/cop/style/redundant_begin.rb +7 -1
  200. data/lib/rubocop/cop/style/redundant_conditional.rb +2 -0
  201. data/lib/rubocop/cop/style/redundant_exception.rb +2 -0
  202. data/lib/rubocop/cop/style/redundant_fetch_block.rb +2 -0
  203. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -0
  204. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -0
  205. data/lib/rubocop/cop/style/redundant_parentheses.rb +13 -0
  206. data/lib/rubocop/cop/style/redundant_self_assignment.rb +2 -0
  207. data/lib/rubocop/cop/style/redundant_sort.rb +1 -0
  208. data/lib/rubocop/cop/style/redundant_sort_by.rb +1 -0
  209. data/lib/rubocop/cop/style/regexp_literal.rb +1 -1
  210. data/lib/rubocop/cop/style/rescue_standard_error.rb +2 -0
  211. data/lib/rubocop/cop/style/return_nil.rb +6 -0
  212. data/lib/rubocop/cop/style/safe_navigation.rb +2 -0
  213. data/lib/rubocop/cop/style/sample.rb +1 -0
  214. data/lib/rubocop/cop/style/signal_exception.rb +3 -0
  215. data/lib/rubocop/cop/style/single_argument_dig.rb +1 -0
  216. data/lib/rubocop/cop/style/single_line_methods.rb +5 -2
  217. data/lib/rubocop/cop/style/slicing_with_range.rb +1 -0
  218. data/lib/rubocop/cop/style/sole_nested_conditional.rb +28 -4
  219. data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
  220. data/lib/rubocop/cop/style/stderr_puts.rb +1 -0
  221. data/lib/rubocop/cop/style/string_concatenation.rb +2 -1
  222. data/lib/rubocop/cop/style/string_hash_keys.rb +2 -0
  223. data/lib/rubocop/cop/style/strip.rb +1 -0
  224. data/lib/rubocop/cop/style/struct_inheritance.rb +1 -0
  225. data/lib/rubocop/cop/style/symbol_proc.rb +25 -1
  226. data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -1
  227. data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +1 -0
  228. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -1
  229. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -0
  230. data/lib/rubocop/cop/style/unless_logical_operators.rb +99 -0
  231. data/lib/rubocop/cop/style/unpack_first.rb +1 -0
  232. data/lib/rubocop/cop/style/while_until_modifier.rb +2 -4
  233. data/lib/rubocop/cop/style/yoda_condition.rb +1 -0
  234. data/lib/rubocop/cop/style/zero_length_predicate.rb +5 -0
  235. data/lib/rubocop/formatter/git_hub_actions_formatter.rb +1 -0
  236. data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
  237. data/lib/rubocop/formatter/simple_text_formatter.rb +2 -1
  238. data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
  239. data/lib/rubocop/magic_comment.rb +30 -1
  240. data/lib/rubocop/name_similarity.rb +1 -1
  241. data/lib/rubocop/options.rb +1 -1
  242. data/lib/rubocop/rspec/expect_offense.rb +5 -2
  243. data/lib/rubocop/runner.rb +1 -0
  244. data/lib/rubocop/target_ruby.rb +21 -13
  245. data/lib/rubocop/version.rb +2 -2
  246. metadata +21 -7
@@ -19,6 +19,7 @@ module RuboCop
19
19
  ' rather than just a message.'
20
20
  RESTRICT_ON_SEND = %i[raise fail].freeze
21
21
 
22
+ # @!method implicit_runtime_error_raise_or_fail(node)
22
23
  def_node_matcher :implicit_runtime_error_raise_or_fail,
23
24
  '(send nil? ${:raise :fail} {str dstr})'
24
25
 
@@ -48,6 +48,7 @@ module RuboCop
48
48
  [Style::Not, Style::SymbolProc]
49
49
  end
50
50
 
51
+ # @!method inverse_candidate?(node)
51
52
  def_node_matcher :inverse_candidate?, <<~PATTERN
52
53
  {
53
54
  (send $(send $(...) $_ $...) :!)
@@ -56,6 +57,7 @@ module RuboCop
56
57
  }
57
58
  PATTERN
58
59
 
60
+ # @!method inverse_block?(node)
59
61
  def_node_matcher :inverse_block?, <<~PATTERN
60
62
  (block $(send (...) $_) ... { $(send ... :!)
61
63
  $(send (...) {:!= :!~} ...)
@@ -34,6 +34,7 @@ module RuboCop
34
34
 
35
35
  private
36
36
 
37
+ # @!method min_max_candidate(node)
37
38
  def_node_matcher :min_max_candidate, <<~PATTERN
38
39
  ({array return} (send [$_receiver !nil?] :min) (send [$_receiver !nil?] :max))
39
40
  PATTERN
@@ -45,11 +45,13 @@ module RuboCop
45
45
  'or `module`.'
46
46
  RESTRICT_ON_SEND = %i[include extend prepend].freeze
47
47
 
48
+ # @!method include_statement(node)
48
49
  def_node_matcher :include_statement, <<~PATTERN
49
50
  (send nil? ${:include :extend :prepend}
50
51
  const)
51
52
  PATTERN
52
53
 
54
+ # @!method in_top_level_scope?(node)
53
55
  def_node_matcher :in_top_level_scope?, <<~PATTERN
54
56
  {
55
57
  root? # either at the top level
@@ -82,8 +82,13 @@ module RuboCop
82
82
  FORBIDDEN_MSG =
83
83
  'Do not use `module_function` or `extend self`.'
84
84
 
85
+ # @!method module_function_node?(node)
85
86
  def_node_matcher :module_function_node?, '(send nil? :module_function)'
87
+
88
+ # @!method extend_self_node?(node)
86
89
  def_node_matcher :extend_self_node?, '(send nil? :extend self)'
90
+
91
+ # @!method private_directive?(node)
87
92
  def_node_matcher :private_directive?, '(send nil? :private ...)'
88
93
 
89
94
  def on_module(node)
@@ -47,11 +47,12 @@ module RuboCop
47
47
  'in a conditional, use `Array#include?` instead.'
48
48
 
49
49
  def on_new_investigation
50
- @compared_elements = []
51
- @allowed_method_comparison = false
50
+ @last_comparison = nil
52
51
  end
53
52
 
54
53
  def on_or(node)
54
+ reset_comparison if switch_comparison?(node)
55
+
55
56
  root_of_or_node = root_of_or_node(node)
56
57
 
57
58
  return unless node == root_of_or_node
@@ -64,14 +65,21 @@ module RuboCop
64
65
 
65
66
  corrector.replace(node, prefer_method)
66
67
  end
68
+
69
+ @last_comparison = node
67
70
  end
68
71
 
69
72
  private
70
73
 
74
+ # @!method simple_double_comparison?(node)
71
75
  def_node_matcher :simple_double_comparison?, '(send $lvar :== $lvar)'
76
+
77
+ # @!method simple_comparison_lhs?(node)
72
78
  def_node_matcher :simple_comparison_lhs?, <<~PATTERN
73
79
  (send $lvar :== $_)
74
80
  PATTERN
81
+
82
+ # @!method simple_comparison_rhs?(node)
75
83
  def_node_matcher :simple_comparison_rhs?, <<~PATTERN
76
84
  (send $_ :== $lvar)
77
85
  PATTERN
@@ -131,6 +139,17 @@ module RuboCop
131
139
  end
132
140
  end
133
141
 
142
+ def switch_comparison?(node)
143
+ return true if @last_comparison.nil?
144
+
145
+ @last_comparison.descendants.none? { |descendant| descendant == node }
146
+ end
147
+
148
+ def reset_comparison
149
+ @compared_elements = []
150
+ @allowed_method_comparison = false
151
+ end
152
+
134
153
  def allow_method_comparison?
135
154
  cop_config.fetch('AllowMethodComparison', true)
136
155
  end
@@ -154,12 +154,14 @@ module RuboCop
154
154
  end
155
155
  end
156
156
 
157
+ # @!method splat_value(node)
157
158
  def_node_matcher :splat_value, <<~PATTERN
158
159
  (array (splat $_))
159
160
  PATTERN
160
161
 
161
162
  # Some of these patterns may not actually return an immutable object,
162
163
  # but we want to consider them immutable for this cop.
164
+ # @!method operation_produces_immutable_object?(node)
163
165
  def_node_matcher :operation_produces_immutable_object?, <<~PATTERN
164
166
  {
165
167
  (const _ _)
@@ -176,6 +178,7 @@ module RuboCop
176
178
  }
177
179
  PATTERN
178
180
 
181
+ # @!method range_enclosed_in_parentheses?(node)
179
182
  def_node_matcher :range_enclosed_in_parentheses?, <<~PATTERN
180
183
  (begin ({irange erange} _ _))
181
184
  PATTERN
@@ -35,6 +35,7 @@ module RuboCop
35
35
 
36
36
  NEGATED_EQUALITY_METHODS = %i[!= !~].freeze
37
37
 
38
+ # @!method double_negation?(node)
38
39
  def_node_matcher :double_negation?, '(send (send _ :!) :!)'
39
40
 
40
41
  def self.autocorrect_incompatible_with
@@ -37,7 +37,10 @@ module RuboCop
37
37
 
38
38
  RESTRICT_ON_SEND = %i[== === nil?].freeze
39
39
 
40
+ # @!method nil_comparison?(node)
40
41
  def_node_matcher :nil_comparison?, '(send _ {:== :===} nil)'
42
+
43
+ # @!method nil_check?(node)
41
44
  def_node_matcher :nil_check?, '(send _ :nil?)'
42
45
 
43
46
  def on_send(node)
@@ -50,6 +53,9 @@ module RuboCop
50
53
  end
51
54
 
52
55
  corrector.replace(node, new_code)
56
+
57
+ parent = node.parent
58
+ corrector.wrap(node, '(', ')') if parent.respond_to?(:method?) && parent.method?(:!)
53
59
  end
54
60
  end
55
61
  end
@@ -28,6 +28,7 @@ module RuboCop
28
28
 
29
29
  MSG = 'Use an empty lambda instead of always returning nil.'
30
30
 
31
+ # @!method nil_return?(node)
31
32
  def_node_matcher :nil_return?, <<~PATTERN
32
33
  { ({return next break} nil) (nil) }
33
34
  PATTERN
@@ -8,6 +8,9 @@ module RuboCop
8
8
  # With `IncludeSemanticChanges` set to `false` by default, this cop
9
9
  # does not report offenses for `!x.nil?` and does no changes that might
10
10
  # change behavior.
11
+ # Also `IncludeSemanticChanges` set to `false` with `EnforcedStyle: comparison` of
12
+ # `Style/NilComparison` cop, this cop does not report offenses for `x != nil` and
13
+ # does no changes to `!x.nil?` style.
11
14
  #
12
15
  # With `IncludeSemanticChanges` set to `true`, this cop reports offenses
13
16
  # for `!x.nil?` and autocorrects that and `x != nil` to solely `x`, which
@@ -41,19 +44,30 @@ module RuboCop
41
44
  class NonNilCheck < Base
42
45
  extend AutoCorrector
43
46
 
47
+ MSG_FOR_REPLACEMENT = 'Prefer `%<prefer>s` over `%<current>s`.'
48
+ MSG_FOR_REDUNDANCY = 'Explicit non-nil checks are usually redundant.'
49
+
44
50
  RESTRICT_ON_SEND = %i[!= nil? !].freeze
45
51
 
52
+ # @!method not_equal_to_nil?(node)
46
53
  def_node_matcher :not_equal_to_nil?, '(send _ :!= nil)'
54
+
55
+ # @!method unless_check?(node)
47
56
  def_node_matcher :unless_check?, '(if (send _ :nil?) ...)'
57
+
58
+ # @!method nil_check?(node)
48
59
  def_node_matcher :nil_check?, '(send _ :nil?)'
60
+
61
+ # @!method not_and_nil_check?(node)
49
62
  def_node_matcher :not_and_nil_check?, '(send (send _ :nil?) :!)'
50
63
 
51
64
  def on_send(node)
52
- return if ignored_node?(node)
53
- return unless (offense_node = find_offense_node(node))
65
+ return if ignored_node?(node) ||
66
+ !include_semantic_changes? && nil_comparison_style == 'comparison'
67
+ return unless register_offense?(node)
54
68
 
55
69
  message = message(node)
56
- add_offense(offense_node, message: message) do |corrector|
70
+ add_offense(node, message: message) do |corrector|
57
71
  autocorrect(corrector, node)
58
72
  end
59
73
  end
@@ -73,13 +87,9 @@ module RuboCop
73
87
 
74
88
  private
75
89
 
76
- def find_offense_node(node)
77
- if not_equal_to_nil?(node)
78
- node.loc.selector
79
- elsif include_semantic_changes? &&
80
- (not_and_nil_check?(node) || unless_and_nil_check?(node))
81
- node
82
- end
90
+ def register_offense?(node)
91
+ not_equal_to_nil?(node) ||
92
+ include_semantic_changes? && (not_and_nil_check?(node) || unless_and_nil_check?(node))
83
93
  end
84
94
 
85
95
  def autocorrect(corrector, node)
@@ -101,10 +111,11 @@ module RuboCop
101
111
  end
102
112
 
103
113
  def message(node)
104
- if node.method?(:!=)
105
- 'Prefer `!expression.nil?` over `expression != nil`.'
114
+ if node.method?(:!=) && !include_semantic_changes?
115
+ prefer = "!#{node.receiver.source}.nil?"
116
+ format(MSG_FOR_REPLACEMENT, prefer: prefer, current: node.source)
106
117
  else
107
- 'Explicit non-nil checks are usually redundant.'
118
+ MSG_FOR_REDUNDANCY
108
119
  end
109
120
  end
110
121
 
@@ -138,6 +149,12 @@ module RuboCop
138
149
  corrector.replace(node.parent.loc.keyword, 'if')
139
150
  corrector.replace(node, receiver.source)
140
151
  end
152
+
153
+ def nil_comparison_style
154
+ nil_comparison_conf = config.for_cop('Style/NilComparison')
155
+
156
+ nil_comparison_conf['Enabled'] && nil_comparison_conf['EnforcedStyle']
157
+ end
141
158
  end
142
159
  end
143
160
  end
@@ -28,10 +28,6 @@ module RuboCop
28
28
  # 10_000_00 # typical representation of $10,000 in cents
29
29
  #
30
30
  class NumericLiterals < Base
31
- # The parameter is called MinDigits (meaning the minimum number of
32
- # digits for which an offense can be registered), but essentially it's
33
- # a Max parameter (the maximum number of something that's allowed).
34
- include ConfigurableMax
35
31
  include IntegerNode
36
32
  extend AutoCorrector
37
33
 
@@ -39,6 +35,11 @@ module RuboCop
39
35
  'separate every 3 digits with them.'
40
36
  DELIMITER_REGEXP = /[eE.]/.freeze
41
37
 
38
+ # The parameter is called MinDigits (meaning the minimum number of
39
+ # digits for which an offense can be registered), but essentially it's
40
+ # a Max parameter (the maximum number of something that's allowed).
41
+ exclude_limit 'MinDigits'
42
+
42
43
  def on_int(node)
43
44
  check(node)
44
45
  end
@@ -49,10 +50,6 @@ module RuboCop
49
50
 
50
51
  private
51
52
 
52
- def max_parameter_name
53
- 'MinDigits'
54
- end
55
-
56
53
  def check(node)
57
54
  int = integer_part(node)
58
55
 
@@ -62,7 +59,7 @@ module RuboCop
62
59
 
63
60
  case int
64
61
  when /^\d+$/
65
- return unless (self.max = int.size + 1)
62
+ return unless (self.min_digits = int.size + 1)
66
63
 
67
64
  register_offense(node)
68
65
  when /\d{4}/, short_group_regex
@@ -8,7 +8,7 @@ module RuboCop
8
8
  # These can be replaced by their respective predicate methods.
9
9
  # The cop can also be configured to do the reverse.
10
10
  #
11
- # The cop disregards `#nonzero?` as it its value is truthy or falsey,
11
+ # The cop disregards `#nonzero?` as its value is truthy or falsey,
12
12
  # but not `true` and `false`, and thus not always interchangeable with
13
13
  # `!= 0`.
14
14
  #
@@ -115,14 +115,17 @@ module RuboCop
115
115
  end
116
116
  end
117
117
 
118
+ # @!method predicate(node)
118
119
  def_node_matcher :predicate, <<~PATTERN
119
120
  (send $(...) ${:zero? :positive? :negative?})
120
121
  PATTERN
121
122
 
123
+ # @!method comparison(node)
122
124
  def_node_matcher :comparison, <<~PATTERN
123
125
  (send [$(...) !gvar_type?] ${:== :> :<} (int 0))
124
126
  PATTERN
125
127
 
128
+ # @!method inverted_comparison(node)
126
129
  def_node_matcher :inverted_comparison, <<~PATTERN
127
130
  (send (int 0) ${:== :> :<} [$(...) !gvar_type?])
128
131
  PATTERN
@@ -22,6 +22,7 @@ module RuboCop
22
22
  class OptionHash < Base
23
23
  MSG = 'Prefer keyword arguments to options hashes.'
24
24
 
25
+ # @!method option_hash(node)
25
26
  def_node_matcher :option_hash, <<~PATTERN
26
27
  (args ... $(optarg [#suspicious_name? _] (hash)))
27
28
  PATTERN
@@ -31,6 +31,7 @@ module RuboCop
31
31
 
32
32
  MSG = 'Use the double pipe equals operator `||=` instead.'
33
33
 
34
+ # @!method ternary_assignment?(node)
34
35
  def_node_matcher :ternary_assignment?, <<~PATTERN
35
36
  ({lvasgn ivasgn cvasgn gvasgn} _var
36
37
  (if
@@ -39,6 +40,7 @@ module RuboCop
39
40
  $_))
40
41
  PATTERN
41
42
 
43
+ # @!method unless_assignment?(node)
42
44
  def_node_matcher :unless_assignment?, <<~PATTERN
43
45
  (if
44
46
  ({lvar ivar cvar gvar} _var) nil?
@@ -115,6 +115,7 @@ module RuboCop
115
115
  end
116
116
  end
117
117
 
118
+ # @!method implicit_self_getter?(node)
118
119
  def_node_matcher :implicit_self_getter?, '(send nil? $_)'
119
120
 
120
121
  # Helper class necessitated by silly design of TSort prior to Ruby 2.1
@@ -123,8 +124,13 @@ module RuboCop
123
124
  include TSort
124
125
  extend RuboCop::NodePattern::Macros
125
126
 
127
+ # @!method var_name(node)
126
128
  def_node_matcher :var_name, '{(casgn _ $_) (_ $_)}'
129
+
130
+ # @!method uses_var?(node)
127
131
  def_node_search :uses_var?, '{({lvar ivar cvar gvar} %) (const _ %)}'
132
+
133
+ # @!method matching_calls(node, receiver, method_name)
128
134
  def_node_search :matching_calls, '(send %1 %2 $...)'
129
135
 
130
136
  def initialize(assignments)
@@ -71,6 +71,7 @@ module RuboCop
71
71
 
72
72
  private
73
73
 
74
+ # @!method control_op_condition(node)
74
75
  def_node_matcher :control_op_condition, <<~PATTERN
75
76
  (begin $_ ...)
76
77
  PATTERN
@@ -18,6 +18,7 @@ module RuboCop
18
18
 
19
19
  MSG = 'Use `proc` instead of `Proc.new`.'
20
20
 
21
+ # @!method proc_new?(node)
21
22
  def_node_matcher :proc_new?,
22
23
  '(block $(send (const {nil? cbase} :Proc) :new) ...)'
23
24
 
@@ -30,6 +30,7 @@ module RuboCop
30
30
  'integers with offsets.'
31
31
  RESTRICT_ON_SEND = %i[+ - succ pred next].freeze
32
32
 
33
+ # @!method integer_op_rand?(node)
33
34
  def_node_matcher :integer_op_rand?, <<~PATTERN
34
35
  (send
35
36
  int {:+ :-}
@@ -39,6 +40,7 @@ module RuboCop
39
40
  {int (irange int int) (erange int int)}))
40
41
  PATTERN
41
42
 
43
+ # @!method rand_op_integer?(node)
42
44
  def_node_matcher :rand_op_integer?, <<~PATTERN
43
45
  (send
44
46
  (send
@@ -49,6 +51,7 @@ module RuboCop
49
51
  int)
50
52
  PATTERN
51
53
 
54
+ # @!method rand_modified?(node)
52
55
  def_node_matcher :rand_modified?, <<~PATTERN
53
56
  (send
54
57
  (send
@@ -71,6 +74,7 @@ module RuboCop
71
74
 
72
75
  private
73
76
 
77
+ # @!method random_call(node)
74
78
  def_node_matcher :random_call, <<~PATTERN
75
79
  {(send (send $_ _ $_) ...)
76
80
  (send _ _ (send $_ _ $_))}
@@ -144,6 +148,7 @@ module RuboCop
144
148
  end
145
149
  end
146
150
 
151
+ # @!method to_int(node)
147
152
  def_node_matcher :to_int, <<~PATTERN
148
153
  (int $_)
149
154
  PATTERN