diff --git a/CHANGELOG.md b/CHANGELOG.md index ffef8d4d66..f1f7b1c526 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ ## master (unreleased) +## 1.19.0 (2023-08-13) + ### New features * [#364](https://github.com/rubocop/rubocop-performance/pull/364): Add new `Performance/MapMethodChain` cop. ([@koic][]) diff --git a/config/default.yml b/config/default.yml index 68c498dc15..e2395d9045 100644 --- a/config/default.yml +++ b/config/default.yml @@ -197,7 +197,7 @@ Performance/MapMethodChain: Description: 'Checks if the `map` method is used in a chain.' Enabled: pending Safe: false - VersionAdded: '<>' + VersionAdded: '1.19' Performance/MethodObjectAsBlock: Description: 'Use block explicitly instead of block-passing a method object.' diff --git a/docs/antora.yml b/docs/antora.yml index 21b1815a34..19b1b92720 100644 --- a/docs/antora.yml +++ b/docs/antora.yml @@ -2,6 +2,6 @@ name: rubocop-performance title: RuboCop Performance # We always provide version without patch here (e.g. 1.1), # as patch versions should not appear in the docs. -version: ~ +version: '1.19' nav: - modules/ROOT/nav.adoc diff --git a/docs/modules/ROOT/pages/cops.adoc b/docs/modules/ROOT/pages/cops.adoc index 9bee60fa19..7c131dfc0d 100644 --- a/docs/modules/ROOT/pages/cops.adoc +++ b/docs/modules/ROOT/pages/cops.adoc @@ -36,6 +36,7 @@ Performance cops optimization analysis for your projects. * xref:cops_performance.adoc#performanceinefficienthashsearch[Performance/InefficientHashSearch] * xref:cops_performance.adoc#performanceioreadlines[Performance/IoReadlines] * xref:cops_performance.adoc#performancemapcompact[Performance/MapCompact] +* xref:cops_performance.adoc#performancemapmethodchain[Performance/MapMethodChain] * xref:cops_performance.adoc#performancemethodobjectasblock[Performance/MethodObjectAsBlock] * xref:cops_performance.adoc#performanceopenstruct[Performance/OpenStruct] * xref:cops_performance.adoc#performancerangeinclude[Performance/RangeInclude] diff --git a/docs/modules/ROOT/pages/cops_performance.adoc b/docs/modules/ROOT/pages/cops_performance.adoc index 420870f337..6da0f42d81 100644 --- a/docs/modules/ROOT/pages/cops_performance.adoc +++ b/docs/modules/ROOT/pages/cops_performance.adoc @@ -1175,6 +1175,59 @@ ary.map(&:foo).compact! ary.compact.map(&:foo) ---- +== Performance/MapMethodChain + +|=== +| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed + +| Pending +| No +| No +| 1.19 +| - +|=== + +Checks if the map method is used in a chain. + +Autocorrection is not supported because an appropriate block variable name cannot be determined automatically. + +[source,ruby] +---- +class X + def initialize + @@num = 0 + end + + def foo + @@num += 1 + self + end + + def bar + @@num * 2 + end +end + +[X.new, X.new].map(&:foo).map(&:bar) # => [4, 4] +[X.new, X.new].map { |x| x.foo.bar } # => [2, 4] +---- + +=== Safety + +This cop is unsafe because false positives occur if the number of times the first method is executed +affects the return value of subsequent methods. + +=== Examples + +[source,ruby] +---- +# bad +array.map(&:foo).map(&:bar) + +# good +array.map { |item| item.foo.bar } +---- + == Performance/MethodObjectAsBlock |=== diff --git a/lib/rubocop/performance/version.rb b/lib/rubocop/performance/version.rb index 698ade3cfa..c3e87793f4 100644 --- a/lib/rubocop/performance/version.rb +++ b/lib/rubocop/performance/version.rb @@ -4,7 +4,7 @@ module RuboCop module Performance # This module holds the RuboCop Performance version information. module Version - STRING = '1.18.0' + STRING = '1.19.0' def self.document_version STRING.match('\d+\.\d+').to_s diff --git a/relnotes/v1.19.0.md b/relnotes/v1.19.0.md new file mode 100644 index 0000000000..dc6a67a920 --- /dev/null +++ b/relnotes/v1.19.0.md @@ -0,0 +1,6 @@ +### New features + +* [#364](https://github.com/rubocop/rubocop-performance/pull/364): Add new `Performance/MapMethodChain` cop. ([@koic][]) +* [#363](https://github.com/rubocop/rubocop-performance/pull/363): Support safe navigation operator for `Performance/ArraySemiInfiniteRangeSlice`, `Performance/DeletePrefix`, `Performance/DeleteSuffix`, `Performance/Detect`, `Performance/EndWith`, `Performance/InefficientHashSearch`, `Performance/MapCompact`, `Performance/RedundantSplitRegexpArgument`, `Performance/ReverseEach`, `Performance/ReverseFirst`, `Performance/SelectMap`, `Performance/Squeeze`, `Performance/StartWith`, `Performance/StringInclude`, and `Performance/StringReplacement` cops. ([@koic][]) + +[@koic]: https://github.com/koic