From f874d1dcb07e1989327c5d4b05a870e0726ae927 Mon Sep 17 00:00:00 2001 From: Piotr Szotkowski Date: Sat, 2 Nov 2024 12:37:43 +0100 Subject: [PATCH] Caller: use lonely operator to safely call `#first` --- ...ix_caller_use_lonely_operator_to_safely.md | 1 + lib/rubocop/cop/performance/caller.rb | 8 ++--- spec/rubocop/cop/performance/caller_spec.rb | 32 +++++++++---------- 3 files changed, 21 insertions(+), 20 deletions(-) create mode 100644 changelog/fix_caller_use_lonely_operator_to_safely.md diff --git a/changelog/fix_caller_use_lonely_operator_to_safely.md b/changelog/fix_caller_use_lonely_operator_to_safely.md new file mode 100644 index 0000000000..badebbb645 --- /dev/null +++ b/changelog/fix_caller_use_lonely_operator_to_safely.md @@ -0,0 +1 @@ +* [#476](https://github.com/rubocop/rubocop-performance/pull/476): Caller: use lonely operator to safely call `#first`. ([@chastell][]) diff --git a/lib/rubocop/cop/performance/caller.rb b/lib/rubocop/cop/performance/caller.rb index bba8758a6b..f996efa1f7 100644 --- a/lib/rubocop/cop/performance/caller.rb +++ b/lib/rubocop/cop/performance/caller.rb @@ -3,7 +3,7 @@ module RuboCop module Cop module Performance - # Identifies places where `caller[n]` can be replaced by `caller(n..n).first`. + # Identifies places where `caller[n]` can be replaced by `caller(n..n)&.first`. # # @example # # bad @@ -13,9 +13,9 @@ module Performance # caller_locations.first # # # good - # caller(2..2).first + # caller(2..2)&.first # caller(1..1).first - # caller_locations(2..2).first + # caller_locations(2..2)&.first # caller_locations(1..1).first class Caller < Base extend AutoCorrector @@ -48,7 +48,7 @@ def on_send(node) n += m end - preferred_method = "#{method_name}(#{n}..#{n}).first" + preferred_method = "#{method_name}(#{n}..#{n})&.first" message = format(MSG, preferred_method: preferred_method, current_method: node.source) add_offense(node, message: message) do |corrector| diff --git a/spec/rubocop/cop/performance/caller_spec.rb b/spec/rubocop/cop/performance/caller_spec.rb index 05578e30e8..cc270cd7f2 100644 --- a/spec/rubocop/cop/performance/caller_spec.rb +++ b/spec/rubocop/cop/performance/caller_spec.rb @@ -17,11 +17,11 @@ expect(caller.first).to eq(caller(1..1).first) expect_offense(<<~RUBY) caller.first - ^^^^^^^^^^^^ Use `caller(1..1).first` instead of `caller.first`. + ^^^^^^^^^^^^ Use `caller(1..1)&.first` instead of `caller.first`. RUBY expect_correction(<<~RUBY) - caller(1..1).first + caller(1..1)&.first RUBY end @@ -29,11 +29,11 @@ expect(caller(1).first).to eq(caller(1..1).first) expect_offense(<<~RUBY) caller(1).first - ^^^^^^^^^^^^^^^ Use `caller(1..1).first` instead of `caller(1).first`. + ^^^^^^^^^^^^^^^ Use `caller(1..1)&.first` instead of `caller(1).first`. RUBY expect_correction(<<~RUBY) - caller(1..1).first + caller(1..1)&.first RUBY end @@ -41,11 +41,11 @@ expect(caller(2).first).to eq(caller(2..2).first) expect_offense(<<~RUBY) caller(2).first - ^^^^^^^^^^^^^^^ Use `caller(2..2).first` instead of `caller(2).first`. + ^^^^^^^^^^^^^^^ Use `caller(2..2)&.first` instead of `caller(2).first`. RUBY expect_correction(<<~RUBY) - caller(2..2).first + caller(2..2)&.first RUBY end @@ -53,11 +53,11 @@ expect(caller[1]).to eq(caller(2..2).first) expect_offense(<<~RUBY) caller[1] - ^^^^^^^^^ Use `caller(2..2).first` instead of `caller[1]`. + ^^^^^^^^^ Use `caller(2..2)&.first` instead of `caller[1]`. RUBY expect_correction(<<~RUBY) - caller(2..2).first + caller(2..2)&.first RUBY end @@ -65,11 +65,11 @@ expect(caller(1)[1]).to eq(caller(2..2).first) expect_offense(<<~RUBY) caller(1)[1] - ^^^^^^^^^^^^ Use `caller(2..2).first` instead of `caller(1)[1]`. + ^^^^^^^^^^^^ Use `caller(2..2)&.first` instead of `caller(1)[1]`. RUBY expect_correction(<<~RUBY) - caller(2..2).first + caller(2..2)&.first RUBY end @@ -77,11 +77,11 @@ expect(caller(2)[1]).to eq(caller(3..3).first) expect_offense(<<~RUBY) caller(2)[1] - ^^^^^^^^^^^^ Use `caller(3..3).first` instead of `caller(2)[1]`. + ^^^^^^^^^^^^ Use `caller(3..3)&.first` instead of `caller(2)[1]`. RUBY expect_correction(<<~RUBY) - caller(3..3).first + caller(3..3)&.first RUBY end @@ -89,11 +89,11 @@ expect(caller_locations.first.to_s).to eq(caller_locations(1..1).first.to_s) expect_offense(<<~RUBY) caller_locations.first - ^^^^^^^^^^^^^^^^^^^^^^ Use `caller_locations(1..1).first` instead of `caller_locations.first`. + ^^^^^^^^^^^^^^^^^^^^^^ Use `caller_locations(1..1)&.first` instead of `caller_locations.first`. RUBY expect_correction(<<~RUBY) - caller_locations(1..1).first + caller_locations(1..1)&.first RUBY end @@ -101,11 +101,11 @@ expect(caller_locations[1].to_s).to eq(caller_locations(2..2).first.to_s) expect_offense(<<~RUBY) caller_locations[1] - ^^^^^^^^^^^^^^^^^^^ Use `caller_locations(2..2).first` instead of `caller_locations[1]`. + ^^^^^^^^^^^^^^^^^^^ Use `caller_locations(2..2)&.first` instead of `caller_locations[1]`. RUBY expect_correction(<<~RUBY) - caller_locations(2..2).first + caller_locations(2..2)&.first RUBY end end