rubocop 1.77.0 → 1.79.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +36 -20
  4. data/lib/rubocop/cli.rb +12 -1
  5. data/lib/rubocop/config_loader.rb +1 -38
  6. data/lib/rubocop/cop/internal_affairs/example_description.rb +1 -1
  7. data/lib/rubocop/cop/internal_affairs/node_type_group.rb +3 -2
  8. data/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb +1 -1
  9. data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +99 -0
  10. data/lib/rubocop/cop/layout/space_around_keyword.rb +6 -1
  11. data/lib/rubocop/cop/layout/space_around_operators.rb +8 -0
  12. data/lib/rubocop/cop/lint/duplicate_methods.rb +25 -4
  13. data/lib/rubocop/cop/lint/literal_as_condition.rb +3 -1
  14. data/lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb +1 -0
  15. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +101 -2
  16. data/lib/rubocop/cop/lint/redundant_type_conversion.rb +4 -4
  17. data/lib/rubocop/cop/lint/require_range_parentheses.rb +1 -1
  18. data/lib/rubocop/cop/lint/rescue_type.rb +1 -1
  19. data/lib/rubocop/cop/lint/useless_numeric_operation.rb +1 -0
  20. data/lib/rubocop/cop/lint/utils/nil_receiver_checker.rb +121 -0
  21. data/lib/rubocop/cop/naming/method_name.rb +102 -13
  22. data/lib/rubocop/cop/naming/predicate_method.rb +27 -2
  23. data/lib/rubocop/cop/security/eval.rb +2 -1
  24. data/lib/rubocop/cop/security/open.rb +1 -0
  25. data/lib/rubocop/cop/style/access_modifier_declarations.rb +1 -1
  26. data/lib/rubocop/cop/style/accessor_grouping.rb +13 -1
  27. data/lib/rubocop/cop/style/array_intersect.rb +51 -23
  28. data/lib/rubocop/cop/style/block_delimiters.rb +1 -1
  29. data/lib/rubocop/cop/style/conditional_assignment.rb +1 -1
  30. data/lib/rubocop/cop/style/dig_chain.rb +1 -1
  31. data/lib/rubocop/cop/style/exponential_notation.rb +1 -0
  32. data/lib/rubocop/cop/style/hash_conversion.rb +8 -9
  33. data/lib/rubocop/cop/style/inverse_methods.rb +1 -1
  34. data/lib/rubocop/cop/style/it_assignment.rb +69 -12
  35. data/lib/rubocop/cop/style/it_block_parameter.rb +3 -1
  36. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +2 -4
  37. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +16 -0
  38. data/lib/rubocop/cop/style/parallel_assignment.rb +3 -0
  39. data/lib/rubocop/cop/style/redundant_fetch_block.rb +1 -9
  40. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
  41. data/lib/rubocop/cop/style/redundant_line_continuation.rb +1 -1
  42. data/lib/rubocop/cop/style/redundant_parentheses.rb +1 -0
  43. data/lib/rubocop/cop/style/single_line_methods.rb +7 -4
  44. data/lib/rubocop/cop/style/sole_nested_conditional.rb +30 -1
  45. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +1 -1
  46. data/lib/rubocop/cop/variable_force.rb +16 -7
  47. data/lib/rubocop/cops_documentation_generator.rb +1 -0
  48. data/lib/rubocop/formatter/markdown_formatter.rb +1 -0
  49. data/lib/rubocop/formatter/pacman_formatter.rb +1 -0
  50. data/lib/rubocop/pending_cops_reporter.rb +56 -0
  51. data/lib/rubocop/server/cache.rb +4 -2
  52. data/lib/rubocop/server/client_command/base.rb +10 -0
  53. data/lib/rubocop/server/client_command/exec.rb +2 -1
  54. data/lib/rubocop/server/client_command/start.rb +11 -1
  55. data/lib/rubocop/version.rb +1 -1
  56. data/lib/rubocop.rb +3 -0
  57. metadata +23 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 871a632c96e6a5c92e57b651037eaf7efd7412e64ac77440d5275027ef791d4e
4
- data.tar.gz: f375ec6479b1f90193cd39e863411707d60cd071e170888005339d89a19ea65b
3
+ metadata.gz: 5919779b3b5bcc30656adbfd28f00fc821327d6ecf4abff825eea32150319aef
4
+ data.tar.gz: d94231324f779e1f99469dc032b762825a761bc0a3ed85e44f34ae3fdc4ea78a
5
5
  SHA512:
6
- metadata.gz: f8dac3eb5cb12741c110c8726ec64595cefb89700527144cc590d7febb676164e3902dff171594278f7895d1c3df336dea3e14f869d285a0c7080b1467bd6362
7
- data.tar.gz: 4aadc7a712774810e1815f4f31bea43fc6d9e121a547621f57894c7df848a6459dfc962af0fb725f4fef6e2017b6a499c203cc05a4d2879d0e50aceaee156b95
6
+ metadata.gz: fff3036cd812063f449fc41c2e1b536c6735fd1e85742692a5f710769a619b2eab1c5800180744b748ab3ba37fe1f4e6c64dd8edf9d4a9abc2f426bf89b47091
7
+ data.tar.gz: d174e5c8d47b012a591bd59292411c2e23927d378c88362bf1f77427b6bf92230f1f95417e7f2ec44454d78bbfe41bf40fd7e7c07add9047beda6655d307e7ed
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.77', require: false
56
+ gem 'rubocop', '~> 1.79', 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
@@ -378,7 +378,7 @@ Gemspec/RubyVersionGlobalsUsage:
378
378
  #################### Layout ###########################
379
379
 
380
380
  Layout/AccessModifierIndentation:
381
- Description: Check indentation of private/protected visibility modifiers.
381
+ Description: Checks indentation of private/protected visibility modifiers.
382
382
  StyleGuide: '#indent-public-private-protected'
383
383
  Enabled: true
384
384
  VersionAdded: '0.49'
@@ -638,6 +638,12 @@ Layout/EmptyLines:
638
638
  Enabled: true
639
639
  VersionAdded: '0.49'
640
640
 
641
+ Layout/EmptyLinesAfterModuleInclusion:
642
+ Description: 'Keeps track of empty lines after module inclusion methods.'
643
+ StyleGuide: '#empty-lines-after-module-inclusion'
644
+ Enabled: pending
645
+ VersionAdded: '1.79'
646
+
641
647
  Layout/EmptyLinesAroundAccessModifier:
642
648
  Description: "Keep blank lines around access modifiers."
643
649
  StyleGuide: '#empty-lines-around-access-modifier'
@@ -1051,7 +1057,7 @@ Layout/LeadingCommentSpace:
1051
1057
  AllowSteepAnnotation: false
1052
1058
 
1053
1059
  Layout/LeadingEmptyLines:
1054
- Description: Check for unnecessary blank lines at the beginning of a file.
1060
+ Description: Checks for unnecessary blank lines at the beginning of a file.
1055
1061
  Enabled: true
1056
1062
  VersionAdded: '0.57'
1057
1063
  VersionChanged: '0.77'
@@ -1141,7 +1147,7 @@ Layout/MultilineArrayLineBreaks:
1141
1147
  AllowMultilineFinalElement: false
1142
1148
 
1143
1149
  Layout/MultilineAssignmentLayout:
1144
- Description: 'Check for a newline after the assignment operator in multi-line assignments.'
1150
+ Description: 'Checks for a newline after the assignment operator in multi-line assignments.'
1145
1151
  StyleGuide: '#indent-conditional-assignment'
1146
1152
  Enabled: false
1147
1153
  VersionAdded: '0.49'
@@ -1655,7 +1661,7 @@ Lint/BinaryOperatorWithIdenticalOperands:
1655
1661
  VersionChanged: '1.69'
1656
1662
 
1657
1663
  Lint/BooleanSymbol:
1658
- Description: 'Check for `:true` and `:false` symbols.'
1664
+ Description: 'Checks for `:true` and `:false` symbols.'
1659
1665
  Enabled: true
1660
1666
  SafeAutoCorrect: false
1661
1667
  VersionAdded: '0.50'
@@ -1688,7 +1694,7 @@ Lint/ConstantReassignment:
1688
1694
  VersionAdded: '1.70'
1689
1695
 
1690
1696
  Lint/ConstantResolution:
1691
- Description: 'Check that constants are fully qualified with `::`.'
1697
+ Description: 'Checks that constants are fully qualified with `::`.'
1692
1698
  Enabled: false
1693
1699
  VersionAdded: '0.86'
1694
1700
  # Restrict this cop to only looking at certain names
@@ -1702,7 +1708,7 @@ Lint/CopDirectiveSyntax:
1702
1708
  VersionAdded: '1.72'
1703
1709
 
1704
1710
  Lint/Debugger:
1705
- Description: 'Check for debugger calls.'
1711
+ Description: 'Checks for debugger calls.'
1706
1712
  Enabled: true
1707
1713
  VersionAdded: '0.14'
1708
1714
  VersionChanged: '1.63'
@@ -1753,7 +1759,7 @@ Lint/Debugger:
1753
1759
  - debug/start
1754
1760
 
1755
1761
  Lint/DeprecatedClassMethods:
1756
- Description: 'Check for deprecated class method calls.'
1762
+ Description: 'Checks for deprecated class method calls.'
1757
1763
  Enabled: true
1758
1764
  VersionAdded: '0.19'
1759
1765
 
@@ -1827,13 +1833,13 @@ Lint/DuplicateElsifCondition:
1827
1833
  VersionAdded: '0.88'
1828
1834
 
1829
1835
  Lint/DuplicateHashKey:
1830
- Description: 'Check for duplicate keys in hash literals.'
1836
+ Description: 'Checks for duplicate keys in hash literals.'
1831
1837
  Enabled: true
1832
1838
  VersionAdded: '0.34'
1833
1839
  VersionChanged: '0.77'
1834
1840
 
1835
1841
  Lint/DuplicateMagicComment:
1836
- Description: 'Check for duplicated magic comments.'
1842
+ Description: 'Checks for duplicated magic comments.'
1837
1843
  Enabled: pending
1838
1844
  VersionAdded: '1.37'
1839
1845
 
@@ -1843,7 +1849,7 @@ Lint/DuplicateMatchPattern:
1843
1849
  VersionAdded: '1.50'
1844
1850
 
1845
1851
  Lint/DuplicateMethods:
1846
- Description: 'Check for duplicate method definitions.'
1852
+ Description: 'Checks for duplicate method definitions.'
1847
1853
  Enabled: true
1848
1854
  VersionAdded: '0.29'
1849
1855
 
@@ -1853,7 +1859,7 @@ Lint/DuplicateRegexpCharacterClassElement:
1853
1859
  VersionAdded: '1.1'
1854
1860
 
1855
1861
  Lint/DuplicateRequire:
1856
- Description: 'Check for duplicate `require`s and `require_relative`s.'
1862
+ Description: 'Checks for duplicate `require`s and `require_relative`s.'
1857
1863
  Enabled: true
1858
1864
  SafeAutoCorrect: false
1859
1865
  VersionAdded: '0.90'
@@ -1870,12 +1876,12 @@ Lint/DuplicateSetElement:
1870
1876
  VersionAdded: '1.67'
1871
1877
 
1872
1878
  Lint/EachWithObjectArgument:
1873
- Description: 'Check for immutable argument given to each_with_object.'
1879
+ Description: 'Checks for immutable argument given to each_with_object.'
1874
1880
  Enabled: true
1875
1881
  VersionAdded: '0.31'
1876
1882
 
1877
1883
  Lint/ElseLayout:
1878
- Description: 'Check for odd code arrangement in an else block.'
1884
+ Description: 'Checks for odd code arrangement in an else block.'
1879
1885
  Enabled: true
1880
1886
  VersionAdded: '0.17'
1881
1887
  VersionChanged: '1.2'
@@ -2278,6 +2284,7 @@ Lint/RedundantSafeNavigation:
2278
2284
  Description: 'Checks for redundant safe navigation calls.'
2279
2285
  Enabled: true
2280
2286
  VersionAdded: '0.93'
2287
+ VersionChanged: '1.79'
2281
2288
  AllowedMethods:
2282
2289
  - instance_of?
2283
2290
  - kind_of?
@@ -2285,6 +2292,12 @@ Lint/RedundantSafeNavigation:
2285
2292
  - eql?
2286
2293
  - respond_to?
2287
2294
  - equal?
2295
+ InferNonNilReceiver: false
2296
+ AdditionalNilMethods:
2297
+ - present?
2298
+ - blank?
2299
+ - try
2300
+ - try!
2288
2301
  Safe: false
2289
2302
 
2290
2303
  Lint/RedundantSplatExpansion:
@@ -2817,7 +2830,7 @@ Migration/DepartmentName:
2817
2830
  #################### Naming ##############################
2818
2831
 
2819
2832
  Naming/AccessorMethodName:
2820
- Description: Check the naming of accessor methods for get_/set_.
2833
+ Description: Checks the naming of accessor methods for get_/set_.
2821
2834
  StyleGuide: '#accessor_mutator_method_names'
2822
2835
  Enabled: true
2823
2836
  VersionAdded: '0.50'
@@ -3072,7 +3085,7 @@ Naming/PredicateMethod:
3072
3085
  Description: 'Checks that predicate methods end with `?` and non-predicate methods do not.'
3073
3086
  Enabled: pending
3074
3087
  VersionAdded: '1.76'
3075
- VersionChanged: '1.76'
3088
+ VersionChanged: '1.78'
3076
3089
  # In `aggressive` mode, the cop will register an offense for predicate methods that
3077
3090
  # may return a non-boolean value.
3078
3091
  # In `conservative` mode, the cop will *not* register an offense for predicate methods
@@ -3082,6 +3095,9 @@ Naming/PredicateMethod:
3082
3095
  - call
3083
3096
  AllowedPatterns: []
3084
3097
  AllowBangMethods: false
3098
+ # Methods that are known to not return a boolean value, despite ending in `?`.
3099
+ WaywardPredicates:
3100
+ - nonzero?
3085
3101
 
3086
3102
  Naming/PredicatePrefix:
3087
3103
  Description: 'Predicate method names should not be prefixed and end with a `?`.'
@@ -4221,7 +4237,7 @@ Style/GlobalVars:
4221
4237
  AllowedVariables: []
4222
4238
 
4223
4239
  Style/GuardClause:
4224
- Description: 'Check for conditionals that can be replaced with guard clauses.'
4240
+ Description: 'Checks for conditionals that can be replaced with guard clauses.'
4225
4241
  StyleGuide: '#no-nested-conditionals'
4226
4242
  Enabled: true
4227
4243
  VersionAdded: '0.20'
@@ -4483,7 +4499,7 @@ Style/IpAddresses:
4483
4499
  - '**/*.gemspec'
4484
4500
 
4485
4501
  Style/ItAssignment:
4486
- Description: 'Checks for assignment to `it` inside a block.'
4502
+ Description: 'Checks for local variables and method parameters named `it`.'
4487
4503
  Enabled: pending
4488
4504
  VersionAdded: '1.70'
4489
4505
 
@@ -5184,7 +5200,7 @@ Style/RandomWithOffset:
5184
5200
  VersionAdded: '0.52'
5185
5201
 
5186
5202
  Style/RedundantArgument:
5187
- Description: 'Check for a redundant argument passed to certain methods.'
5203
+ Description: 'Checks for a redundant argument passed to certain methods.'
5188
5204
  Enabled: pending
5189
5205
  Safe: false
5190
5206
  VersionAdded: '1.4'
@@ -5345,7 +5361,7 @@ Style/RedundantInterpolationUnfreeze:
5345
5361
  VersionAdded: '1.66'
5346
5362
 
5347
5363
  Style/RedundantLineContinuation:
5348
- Description: 'Check for redundant line continuation.'
5364
+ Description: 'Checks for redundant line continuation.'
5349
5365
  Enabled: pending
5350
5366
  VersionAdded: '1.49'
5351
5367
 
@@ -5639,7 +5655,7 @@ Style/SpecialGlobalVars:
5639
5655
  - use_builtin_english_names
5640
5656
 
5641
5657
  Style/StabbyLambdaParentheses:
5642
- Description: 'Check for the usage of parentheses around stabby lambda arguments.'
5658
+ Description: 'Checks for the usage of parentheses around stabby lambda arguments.'
5643
5659
  StyleGuide: '#stabby-lambda-with-args'
5644
5660
  Enabled: true
5645
5661
  VersionAdded: '0.35'
data/lib/rubocop/cli.rb CHANGED
@@ -12,7 +12,7 @@ module RuboCop
12
12
  STATUS_INTERRUPTED = Signal.list['INT'] + 128
13
13
  DEFAULT_PARALLEL_OPTIONS = %i[
14
14
  color config debug display_style_guide display_time display_only_fail_level_offenses
15
- display_only_failed editor_mode except extra_details fail_level fix_layout format
15
+ display_only_failed editor_mode except extra_details fail_level fix_layout format formatters
16
16
  ignore_disable_comments lint only only_guide_cops require safe
17
17
  autocorrect safe_autocorrect autocorrect_all
18
18
  ].freeze
@@ -48,6 +48,7 @@ module RuboCop
48
48
  validate_options_vs_config
49
49
  parallel_by_default!
50
50
  apply_default_formatter
51
+ report_pending_cops
51
52
  execute_runners
52
53
  end
53
54
  end
@@ -155,6 +156,7 @@ module RuboCop
155
156
 
156
157
  def act_on_options
157
158
  set_options_to_config_loader
159
+ set_options_to_pending_cops_reporter
158
160
  handle_editor_mode
159
161
 
160
162
  @config_store.options_config = @options[:config] if @options[:config]
@@ -179,6 +181,11 @@ module RuboCop
179
181
  ConfigLoader.ignore_unrecognized_cops = @options[:ignore_unrecognized_cops]
180
182
  end
181
183
 
184
+ def set_options_to_pending_cops_reporter
185
+ PendingCopsReporter.disable_pending_cops = @options[:disable_pending_cops]
186
+ PendingCopsReporter.enable_pending_cops = @options[:enable_pending_cops]
187
+ end
188
+
182
189
  def handle_editor_mode
183
190
  RuboCop::LSP.enable if @options[:editor_mode]
184
191
  end
@@ -208,5 +215,9 @@ module RuboCop
208
215
  [[formatter, @options[:output_path]]]
209
216
  end
210
217
  end
218
+
219
+ def report_pending_cops
220
+ PendingCopsReporter.warn_if_needed(@config_store.for_pwd)
221
+ end
211
222
  end
212
223
  end
@@ -22,14 +22,6 @@ module RuboCop
22
22
  class << self
23
23
  include FileFinder
24
24
 
25
- PENDING_BANNER = <<~BANNER
26
- The following cops were added to RuboCop, but are not configured. Please set Enabled to either `true` or `false` in your `.rubocop.yml` file.
27
-
28
- Please also note that you can opt-in to new cops by default by adding this to your config:
29
- AllCops:
30
- NewCops: enable
31
- BANNER
32
-
33
25
  attr_accessor :debug, :ignore_parent_exclusion, :disable_pending_cops, :enable_pending_cops,
34
26
  :ignore_unrecognized_cops
35
27
  attr_writer :default_configuration
@@ -132,21 +124,7 @@ module RuboCop
132
124
  add_excludes_from_files(config, config_file)
133
125
  end
134
126
 
135
- merge_with_default(config, config_file).tap do |merged_config|
136
- unless possible_new_cops?(merged_config)
137
- pending_cops = pending_cops_only_qualified(merged_config.pending_cops)
138
- warn_on_pending_cops(pending_cops) unless pending_cops.empty?
139
- end
140
- end
141
- end
142
-
143
- def pending_cops_only_qualified(pending_cops)
144
- pending_cops.select { |cop| Cop::Registry.qualified_cop?(cop.name) }
145
- end
146
-
147
- def possible_new_cops?(config)
148
- disable_pending_cops || enable_pending_cops ||
149
- config.disabled_new_cops? || config.enabled_new_cops?
127
+ merge_with_default(config, config_file)
150
128
  end
151
129
 
152
130
  def add_excludes_from_files(config, config_file)
@@ -208,21 +186,6 @@ module RuboCop
208
186
  ConfigFinder.project_root
209
187
  end
210
188
 
211
- def warn_on_pending_cops(pending_cops)
212
- warn Rainbow(PENDING_BANNER).yellow
213
-
214
- pending_cops.each { |cop| warn_pending_cop cop }
215
-
216
- warn Rainbow('For more information: https://docs.rubocop.org/rubocop/versioning.html').yellow
217
- end
218
-
219
- def warn_pending_cop(cop)
220
- version = cop.metadata['VersionAdded'] || 'N/A'
221
-
222
- warn Rainbow("#{cop.name}: # new in #{version}").yellow
223
- warn Rainbow(' Enabled: true').yellow
224
- end
225
-
226
189
  # Merges the given configuration with the default one.
227
190
  def merge_with_default(config, config_file, unset_nil: true)
228
191
  resolver.merge_with_default(config, config_file, unset_nil: unset_nil)
@@ -46,7 +46,7 @@ module RuboCop
46
46
  /\A(does not|doesn't) (register|find|flag|report)/ => 'registers',
47
47
  /\A(does not|doesn't) add (a|an|any )?offense/ => 'registers an offense',
48
48
  /\Aregisters no offense/ => 'registers an offense',
49
- /\A(accepts|register)\b/ => 'registers'
49
+ /\A(accepts|allows|register)\b/ => 'registers'
50
50
  }.freeze
51
51
 
52
52
  EXPECT_NO_CORRECTIONS_DESCRIPTION_MAPPING = {
@@ -66,8 +66,9 @@ module RuboCop
66
66
  end
67
67
 
68
68
  def autocorrect_to_explicit_predicate(corrector, node, group_name)
69
- corrector.replace(node.selector, "#{group_name}_type?")
70
- corrector.remove(arguments_range(node))
69
+ range = node.loc.selector.begin.join(node.source_range.end)
70
+
71
+ corrector.replace(range, "#{group_name}_type?")
71
72
  end
72
73
 
73
74
  def autocorrect_keep_method(corrector, symbol_args, group_name, group_types)
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module InternalAffairs
6
- # Check for useless `RESTRICT_ON_SEND`.
6
+ # Checks for useless `RESTRICT_ON_SEND`.
7
7
  #
8
8
  # @example
9
9
  # # bad
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Layout
6
+ # Checks for an empty line after a module inclusion method (`extend`,
7
+ # `include` and `prepend`), or a group of them.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # class Foo
12
+ # include Bar
13
+ # attr_reader :baz
14
+ # end
15
+ #
16
+ # # good
17
+ # class Foo
18
+ # include Bar
19
+ #
20
+ # attr_reader :baz
21
+ # end
22
+ #
23
+ # # also good - multiple module inclusions grouped together
24
+ # class Foo
25
+ # extend Bar
26
+ # include Baz
27
+ # prepend Qux
28
+ # end
29
+ #
30
+ class EmptyLinesAfterModuleInclusion < Base
31
+ include RangeHelp
32
+ extend AutoCorrector
33
+
34
+ MSG = 'Add an empty line after module inclusion.'
35
+
36
+ MODULE_INCLUSION_METHODS = %i[include extend prepend].freeze
37
+
38
+ RESTRICT_ON_SEND = MODULE_INCLUSION_METHODS
39
+
40
+ def on_send(node)
41
+ return if node.receiver
42
+ return if node.parent&.send_type?
43
+
44
+ return if next_line_empty_or_enable_directive_comment?(node.last_line)
45
+
46
+ next_line_node = next_line_node(node)
47
+ return unless require_empty_line?(next_line_node)
48
+
49
+ add_offense(node) { |corrector| autocorrect(corrector, node) }
50
+ end
51
+
52
+ private
53
+
54
+ def autocorrect(corrector, node)
55
+ node_range = range_by_whole_lines(node.source_range)
56
+
57
+ next_line = node_range.last_line + 1
58
+ if enable_directive_comment?(next_line)
59
+ node_range = processed_source.comment_at_line(next_line)
60
+ end
61
+
62
+ corrector.insert_after(node_range, "\n")
63
+ end
64
+
65
+ def next_line_empty_or_enable_directive_comment?(line)
66
+ line_empty?(line) || (enable_directive_comment?(line + 1) && line_empty?(line + 1))
67
+ end
68
+
69
+ def enable_directive_comment?(line)
70
+ return false unless (comment = processed_source.comment_at_line(line))
71
+
72
+ DirectiveComment.new(comment).enabled?
73
+ end
74
+
75
+ def line_empty?(line)
76
+ processed_source[line].nil? || processed_source[line].blank?
77
+ end
78
+
79
+ def require_empty_line?(node)
80
+ return false unless node
81
+
82
+ !allowed_method?(node)
83
+ end
84
+
85
+ def allowed_method?(node)
86
+ return false unless node.send_type?
87
+
88
+ MODULE_INCLUSION_METHODS.include?(node.method_name)
89
+ end
90
+
91
+ def next_line_node(node)
92
+ return if node.parent.if_type?
93
+
94
+ node.right_sibling
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -16,6 +16,8 @@ module RuboCop
16
16
  #
17
17
  # something = 123if test
18
18
  #
19
+ # return(foo + bar)
20
+ #
19
21
  # # good
20
22
  # something 'test' do |x|
21
23
  # end
@@ -24,6 +26,9 @@ module RuboCop
24
26
  # end
25
27
  #
26
28
  # something = 123 if test
29
+ #
30
+ # return (foo + bar)
31
+ #
27
32
  class SpaceAroundKeyword < Base
28
33
  extend AutoCorrector
29
34
 
@@ -33,7 +38,7 @@ module RuboCop
33
38
  DO = 'do'
34
39
  SAFE_NAVIGATION = '&.'
35
40
  NAMESPACE_OPERATOR = '::'
36
- ACCEPT_LEFT_PAREN = %w[break defined? next not rescue return super yield].freeze
41
+ ACCEPT_LEFT_PAREN = %w[break defined? next not rescue super yield].freeze
37
42
  ACCEPT_LEFT_SQUARE_BRACKET = %w[super yield].freeze
38
43
  ACCEPT_NAMESPACE_OPERATOR = 'super'
39
44
  RESTRICT_ON_SEND = %i[!].freeze
@@ -151,6 +151,14 @@ module RuboCop
151
151
  check_operator(:match_pattern, node.loc.operator, node)
152
152
  end
153
153
 
154
+ def on_match_alt(node)
155
+ check_operator(:match_alt, node.loc.operator, node)
156
+ end
157
+
158
+ def on_match_as(node)
159
+ check_operator(:match_as, node.loc.operator, node)
160
+ end
161
+
154
162
  alias on_or on_binary
155
163
  alias on_and on_binary
156
164
  alias on_lvasgn on_assignment
@@ -6,6 +6,10 @@ module RuboCop
6
6
  # Checks for duplicated instance (or singleton) method
7
7
  # definitions.
8
8
  #
9
+ # NOTE: Aliasing a method to itself is allowed, as it indicates that
10
+ # the developer intends to suppress Ruby's method redefinition warnings.
11
+ # See https://bugs.ruby-lang.org/issues/13574.
12
+ #
9
13
  # @example
10
14
  #
11
15
  # # bad
@@ -40,6 +44,18 @@ module RuboCop
40
44
  #
41
45
  # alias bar foo
42
46
  #
47
+ # # good
48
+ # alias foo foo
49
+ # def foo
50
+ # 1
51
+ # end
52
+ #
53
+ # # good
54
+ # alias_method :foo, :foo
55
+ # def foo
56
+ # 1
57
+ # end
58
+ #
43
59
  # @example AllCops:ActiveSupportExtensionsEnabled: false (default)
44
60
  #
45
61
  # # good
@@ -113,11 +129,13 @@ module RuboCop
113
129
 
114
130
  # @!method method_alias?(node)
115
131
  def_node_matcher :method_alias?, <<~PATTERN
116
- (alias (sym $_name) sym)
132
+ (alias (sym $_name) (sym $_original_name))
117
133
  PATTERN
118
134
 
119
135
  def on_alias(node)
120
- return unless (name = method_alias?(node))
136
+ name, original_name = method_alias?(node)
137
+ return unless name && original_name
138
+ return if name == original_name
121
139
  return if node.ancestors.any?(&:if_type?)
122
140
 
123
141
  found_instance_method(node, name)
@@ -125,7 +143,7 @@ module RuboCop
125
143
 
126
144
  # @!method alias_method?(node)
127
145
  def_node_matcher :alias_method?, <<~PATTERN
128
- (send nil? :alias_method (sym $_name) _)
146
+ (send nil? :alias_method (sym $_name) (sym $_original_name))
129
147
  PATTERN
130
148
 
131
149
  # @!method delegate_method?(node)
@@ -140,7 +158,10 @@ module RuboCop
140
158
  def_node_matcher :sym_name, '(sym $_name)'
141
159
 
142
160
  def on_send(node) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
143
- if (name = alias_method?(node))
161
+ name, original_name = alias_method?(node)
162
+
163
+ if name && original_name
164
+ return if name == original_name
144
165
  return if node.ancestors.any?(&:if_type?)
145
166
 
146
167
  found_instance_method(node, name)
@@ -123,7 +123,9 @@ module RuboCop
123
123
  # rubocop:enable Metrics/AbcSize
124
124
 
125
125
  def on_case(case_node)
126
- if case_node.condition
126
+ if (cond = case_node.condition)
127
+ return if !cond.falsey_literal? && !cond.truthy_literal?
128
+
127
129
  check_case(case_node)
128
130
  else
129
131
  case_node.when_branches.each do |when_node|
@@ -45,6 +45,7 @@ module RuboCop
45
45
  #
46
46
  class NumericOperationWithConstantResult < Base
47
47
  extend AutoCorrector
48
+
48
49
  MSG = 'Numeric operation with a constant result detected.'
49
50
  RESTRICT_ON_SEND = %i[* / **].freeze
50
51