豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit e2f2d5c

Browse files
committed
Cut 1.70
1 parent 3994f94 commit e2f2d5c

File tree

14 files changed

+380
-28
lines changed

14 files changed

+380
-28
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ output by `rubocop -V`, include them as well. Here's an example:
3838

3939
```
4040
$ [bundle exec] rubocop -V
41-
1.69.2 (using Parser 3.3.5.0, rubocop-ast 1.32.3, analyzing as Ruby 3.3, running on ruby 3.3.5) [x86_64-linux]
41+
1.70.0 (using Parser 3.3.5.0, rubocop-ast 1.32.3, analyzing as Ruby 3.3, running on ruby 3.3.5) [x86_64-linux]
4242
- rubocop-performance 1.22.1
4343
- rubocop-rspec 3.1.0
4444
```

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
## master (unreleased)
1313

14+
## 1.70.0 (2025-01-10)
15+
1416
### New features
1517

1618
* [#13474](https://github.com/rubocop/rubocop/pull/13474): Add new `Style/ItAssignment` cop to detect local assignments to `it` inside blocks. ([@dvandersluis][])

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ do so.
1717

1818
```console
1919
$ rubocop -V
20-
1.69.2 (using Parser 3.3.5.0, rubocop-ast 1.32.3, analyzing as Ruby 3.3, running on ruby 3.3.5) [x86_64-linux]
20+
1.70.0 (using Parser 3.3.5.0, rubocop-ast 1.32.3, analyzing as Ruby 3.3, running on ruby 3.3.5) [x86_64-linux]
2121
- rubocop-performance 1.22.1
2222
- rubocop-rspec 3.1.0
2323
```

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ To prevent an unwanted RuboCop update you might want to use a conservative versi
5252
in your `Gemfile`:
5353

5454
```rb
55-
gem 'rubocop', '~> 1.69', require: false
55+
gem 'rubocop', '~> 1.70', require: false
5656
```
5757

5858
See [our versioning policy](https://docs.rubocop.org/rubocop/versioning.html) for further details.

config/default.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,7 @@ Lint/ConstantOverwrittenInRescue:
16681668
Lint/ConstantReassignment:
16691669
Description: 'Checks for constant reassignments.'
16701670
Enabled: pending
1671-
VersionAdded: '<<next>>'
1671+
VersionAdded: '1.70'
16721672

16731673
Lint/ConstantResolution:
16741674
Description: 'Check that constants are fully qualified with `::`.'
@@ -2411,7 +2411,7 @@ Lint/SharedMutableDefault:
24112411
Description: 'Checks for mutable literals used as default arguments during Hash initialization.'
24122412
StyleGuide: '#no-mutable-defaults'
24132413
Enabled: pending
2414-
VersionAdded: '<<next>>'
2414+
VersionAdded: '1.70'
24152415

24162416
Lint/StructNewOverride:
24172417
Description: 'Disallow overriding the `Struct` built-in methods via `Struct.new`.'
@@ -3147,7 +3147,7 @@ Style/AccessModifierDeclarations:
31473147
Description: 'Checks style of how access modifiers are used.'
31483148
Enabled: true
31493149
VersionAdded: '0.57'
3150-
VersionChanged: '<<next>>'
3150+
VersionChanged: '1.70'
31513151
EnforcedStyle: group
31523152
SupportedStyles:
31533153
- inline
@@ -4331,7 +4331,7 @@ Style/IpAddresses:
43314331
Style/ItAssignment:
43324332
Description: 'Checks for assignment to `it` inside a block.'
43334333
Enabled: pending
4334-
VersionAdded: '<<next>>'
4334+
VersionAdded: '1.70'
43354335

43364336
Style/KeywordArgumentsMerging:
43374337
Description: >-

docs/antora.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ name: rubocop
22
title: RuboCop
33
# We always provide version without patch here (e.g. 1.1),
44
# as patch versions should not appear in the docs.
5-
version: ~
5+
version: '1.70'
66
nav:
77
- modules/ROOT/nav.adoc

docs/modules/ROOT/pages/cops.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ In the following section you find all available cops:
213213
* xref:cops_lint.adoc#lintcircularargumentreference[Lint/CircularArgumentReference]
214214
* xref:cops_lint.adoc#lintconstantdefinitioninblock[Lint/ConstantDefinitionInBlock]
215215
* xref:cops_lint.adoc#lintconstantoverwritteninrescue[Lint/ConstantOverwrittenInRescue]
216+
* xref:cops_lint.adoc#lintconstantreassignment[Lint/ConstantReassignment]
216217
* xref:cops_lint.adoc#lintconstantresolution[Lint/ConstantResolution]
217218
* xref:cops_lint.adoc#lintdebugger[Lint/Debugger]
218219
* xref:cops_lint.adoc#lintdeprecatedclassmethods[Lint/DeprecatedClassMethods]
@@ -312,6 +313,7 @@ In the following section you find all available cops:
312313
* xref:cops_lint.adoc#lintshadowedargument[Lint/ShadowedArgument]
313314
* xref:cops_lint.adoc#lintshadowedexception[Lint/ShadowedException]
314315
* xref:cops_lint.adoc#lintshadowingouterlocalvariable[Lint/ShadowingOuterLocalVariable]
316+
* xref:cops_lint.adoc#lintsharedmutabledefault[Lint/SharedMutableDefault]
315317
* xref:cops_lint.adoc#lintstructnewoverride[Lint/StructNewOverride]
316318
* xref:cops_lint.adoc#lintsuppressedexception[Lint/SuppressedException]
317319
* xref:cops_lint.adoc#lintsymbolconversion[Lint/SymbolConversion]
@@ -502,6 +504,7 @@ In the following section you find all available cops:
502504
* xref:cops_style.adoc#styleinversemethods[Style/InverseMethods]
503505
* xref:cops_style.adoc#styleinvertibleunlesscondition[Style/InvertibleUnlessCondition]
504506
* xref:cops_style.adoc#styleipaddresses[Style/IpAddresses]
507+
* xref:cops_style.adoc#styleitassignment[Style/ItAssignment]
505508
* xref:cops_style.adoc#stylekeywordargumentsmerging[Style/KeywordArgumentsMerging]
506509
* xref:cops_style.adoc#stylekeywordparametersorder[Style/KeywordParametersOrder]
507510
* xref:cops_style.adoc#stylelambda[Style/Lambda]

docs/modules/ROOT/pages/cops_layout.adoc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5535,6 +5535,18 @@ foo(a, b, c)
55355535
55365536
[source,ruby]
55375537
----
5538+
# bad
5539+
foo(a, b,
5540+
c
5541+
)
5542+
5543+
# bad
5544+
foo(
5545+
a, b, {
5546+
foo: "bar",
5547+
}
5548+
)
5549+
55385550
# good
55395551
foo(
55405552
a,
@@ -5550,6 +5562,18 @@ foo(
55505562
55515563
[source,ruby]
55525564
----
5565+
# bad
5566+
foo(a, b,
5567+
c
5568+
)
5569+
5570+
# good
5571+
foo(
5572+
a, b, {
5573+
foo: "bar",
5574+
}
5575+
)
5576+
55535577
# good
55545578
foo(
55555579
a,

docs/modules/ROOT/pages/cops_lint.adoc

Lines changed: 160 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,83 @@ rescue StandardError
741741
end
742742
----
743743
744+
[#lintconstantreassignment]
745+
== Lint/ConstantReassignment
746+
747+
|===
748+
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
749+
750+
| Pending
751+
| Yes
752+
| No
753+
| 1.70
754+
| -
755+
|===
756+
757+
Checks for constant reassignments.
758+
759+
Emulates Ruby's runtime warning "already initialized constant X"
760+
when a constant is reassigned in the same file and namespace using the
761+
`NAME = value` syntax.
762+
763+
The cop cannot catch all offenses, like, for example, when a constant
764+
is reassigned in another file, or when using metaprogramming (`Module#const_set`).
765+
766+
The cop only takes into account constants assigned in a "simple" way: directly
767+
inside class/module definition, or within another constant. Other type of assignments
768+
(e.g., inside a conditional) are disregarded.
769+
770+
The cop also tracks constant removal using `Module#remove_const` with symbol
771+
or string argument.
772+
773+
[#examples-lintconstantreassignment]
774+
=== Examples
775+
776+
[source,ruby]
777+
----
778+
# bad
779+
X = :foo
780+
X = :bar
781+
782+
# bad
783+
class A
784+
X = :foo
785+
X = :bar
786+
end
787+
788+
# bad
789+
module A
790+
X = :foo
791+
X = :bar
792+
end
793+
794+
# good - keep only one assignment
795+
X = :bar
796+
797+
class A
798+
X = :bar
799+
end
800+
801+
module A
802+
X = :bar
803+
end
804+
805+
# good - use OR assignment
806+
X = :foo
807+
X ||= :bar
808+
809+
# good - use conditional assignment
810+
X = :foo
811+
X = :bar unless defined?(X)
812+
813+
# good - remove the assigned constant first
814+
class A
815+
X = :foo
816+
remove_const :X
817+
X = :bar
818+
end
819+
----
820+
744821
[#lintconstantresolution]
745822
== Lint/ConstantResolution
746823
@@ -1730,7 +1807,7 @@ end
17301807
| -
17311808
|===
17321809
1733-
Checks for duplicate literal, constant, or variable elements in Set.
1810+
Checks for duplicate literal, constant, or variable elements in Set and SortedSet.
17341811
17351812
[#examples-lintduplicatesetelement]
17361813
=== Examples
@@ -1754,6 +1831,18 @@ Set.new([:foo, :bar])
17541831
17551832
# good
17561833
[:foo, :bar].to_set
1834+
1835+
# bad
1836+
SortedSet[:foo, :bar, :foo]
1837+
1838+
# good
1839+
SortedSet[:foo, :bar]
1840+
1841+
# bad
1842+
SortedSet.new([:foo, :bar, :foo])
1843+
1844+
# good
1845+
SortedSet.new([:foo, :bar])
17571846
----
17581847
17591848
[#linteachwithobjectargument]
@@ -4386,33 +4475,30 @@ non_numbered_parameter_name = :value
43864475
|===
43874476
43884477
Certain numeric operations have a constant result, usually 0 or 1.
4389-
Subtracting a number from itself or multiplying it by 0 will always return 0.
4390-
Additionally, a variable modulo 0 or itself will always return 0.
4478+
Multiplying a number by 0 will always return 0.
43914479
Dividing a number by itself or raising it to the power of 0 will always return 1.
43924480
As such, they can be replaced with that result.
43934481
These are probably leftover from debugging, or are mistakes.
43944482
Other numeric operations that are similarly leftover from debugging or mistakes
43954483
are handled by Lint/UselessNumericOperation.
43964484
4485+
NOTE: This cop doesn't detect offenses for the `-` and `%` operator because it
4486+
can't determine the type of `x`. If `x` is an Array or String, it doesn't perform
4487+
a numeric operation.
4488+
43974489
[#examples-lintnumericoperationwithconstantresult]
43984490
=== Examples
43994491
44004492
[source,ruby]
44014493
----
44024494
# bad
4403-
x - x
44044495
x * 0
4405-
x % 1
4406-
x % x
44074496
44084497
# good
44094498
0
44104499
44114500
# bad
4412-
x -= x
44134501
x *= 0
4414-
x %= 1
4415-
x %= x
44164502
44174503
# good
44184504
x = 0
@@ -6172,6 +6258,71 @@ def some_method
61726258
end
61736259
----
61746260
6261+
[#lintsharedmutabledefault]
6262+
== Lint/SharedMutableDefault
6263+
6264+
|===
6265+
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
6266+
6267+
| Pending
6268+
| Yes
6269+
| No
6270+
| 1.70
6271+
| -
6272+
|===
6273+
6274+
Checks for Hash creation with a mutable default value.
6275+
Creating a Hash in such a way will share the default value
6276+
across all keys, causing unexpected behavior when modifying it.
6277+
6278+
For example, when the Hash was created with an Array as the argument,
6279+
calling `hash[:foo] << 'bar'` will also change the value of all
6280+
other keys that have not been explicitly assigned to.
6281+
6282+
[#examples-lintsharedmutabledefault]
6283+
=== Examples
6284+
6285+
[source,ruby]
6286+
----
6287+
# bad
6288+
Hash.new([])
6289+
Hash.new({})
6290+
Hash.new(Array.new)
6291+
Hash.new(Hash.new)
6292+
6293+
# okay -- In rare cases that intentionally have this behavior,
6294+
# without disabling the cop, you can set the default explicitly.
6295+
h = Hash.new
6296+
h.default = []
6297+
h[:a] << 1
6298+
h[:b] << 2
6299+
h # => {:a => [1, 2], :b => [1, 2]}
6300+
6301+
# okay -- beware this will discard mutations and only remember assignments
6302+
Hash.new { Array.new }
6303+
Hash.new { Hash.new }
6304+
Hash.new { {} }
6305+
Hash.new { [] }
6306+
6307+
# good - frozen solution will raise an error when mutation attempted
6308+
Hash.new([].freeze)
6309+
Hash.new({}.freeze)
6310+
6311+
# good - using a proc will create a new object for each key
6312+
h = Hash.new
6313+
h.default_proc = ->(h, k) { [] }
6314+
h.default_proc = ->(h, k) { {} }
6315+
6316+
# good - using a block will create a new object for each key
6317+
Hash.new { |h, k| h[k] = [] }
6318+
Hash.new { |h, k| h[k] = {} }
6319+
----
6320+
6321+
[#references-lintsharedmutabledefault]
6322+
=== References
6323+
6324+
* https://rubystyle.guide#no-mutable-defaults
6325+
61756326
[#lintstructnewoverride]
61766327
== Lint/StructNewOverride
61776328

0 commit comments

Comments
 (0)