diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a911ef288..d6da4afebc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,11 +2,15 @@ ## master (unreleased) +### Bug fixes + +* [#50](https://github.com/rubocop-hq/rubocop-performance/issues/50): Make `Performance/EndWith` and `Performance/StartWith` autocorrects nil-safe. ([@dduugg][]) + ## 1.3.0 (2019-05-13) ### Bug fixes -* [#48](https://github.com/rubocop-hq/rubocop-performance/issues/48): Reduce `Performance/RegexpMatch` false positive by only flagging `match` used with Regexp/String/Symbol literals. ([@dduugg][]) +* [#48](https://github.com/rubocop-hq/rubocop-performance/issues/48): Reduce `Performance/RegexpMatch` false positives by only flagging `match` used with Regexp/String/Symbol literals. ([@dduugg][]) ### Changes diff --git a/config/default.yml b/config/default.yml index cc896dd05d..b6cd288700 100644 --- a/config/default.yml +++ b/config/default.yml @@ -82,7 +82,6 @@ Performance/EndWith: # object. Switching these methods has to be done with knowledge of the types # of the variables which rubocop doesn't have. SafeAutoCorrect: false - AutoCorrect: false Enabled: true VersionAdded: '0.36' VersionChanged: '0.44' @@ -176,7 +175,6 @@ Performance/StartWith: # object. Switching these methods has to be done with knowledge of the types # of the variables which rubocop doesn't have. SafeAutoCorrect: false - AutoCorrect: false Enabled: true VersionAdded: '0.36' VersionChanged: '0.44' diff --git a/lib/rubocop/cop/performance/end_with.rb b/lib/rubocop/cop/performance/end_with.rb index b4d8fa6d04..4026d1d2a5 100644 --- a/lib/rubocop/cop/performance/end_with.rb +++ b/lib/rubocop/cop/performance/end_with.rb @@ -44,7 +44,7 @@ def autocorrect(node) regex_str = interpret_string_escapes(regex_str) lambda do |corrector| - new_source = receiver.source + '.end_with?(' + + new_source = receiver.source + '&.end_with?(' + to_string_literal(regex_str) + ')' corrector.replace(node.source_range, new_source) end diff --git a/lib/rubocop/cop/performance/start_with.rb b/lib/rubocop/cop/performance/start_with.rb index 50ba3d4844..8a9660e523 100644 --- a/lib/rubocop/cop/performance/start_with.rb +++ b/lib/rubocop/cop/performance/start_with.rb @@ -47,7 +47,7 @@ def autocorrect(node) regex_str = interpret_string_escapes(regex_str) lambda do |corrector| - new_source = receiver.source + '.start_with?(' + + new_source = receiver.source + '&.start_with?(' + to_string_literal(regex_str) + ')' corrector.replace(node.source_range, new_source) end diff --git a/manual/cops_performance.md b/manual/cops_performance.md index d76f1e1002..e49dcdc4cf 100644 --- a/manual/cops_performance.md +++ b/manual/cops_performance.md @@ -324,7 +324,7 @@ would suffice. Name | Default value | Configurable values --- | --- | --- -AutoCorrect | `false` | Boolean +AutoCorrect | `true` | Boolean ### References @@ -775,7 +775,7 @@ This cop identifies unnecessary use of a regex where Name | Default value | Configurable values --- | --- | --- -AutoCorrect | `false` | Boolean +AutoCorrect | `true` | Boolean ### References diff --git a/spec/rubocop/cop/performance/end_with_spec.rb b/spec/rubocop/cop/performance/end_with_spec.rb index b2dd45e6f0..c9b5446024 100644 --- a/spec/rubocop/cop/performance/end_with_spec.rb +++ b/spec/rubocop/cop/performance/end_with_spec.rb @@ -6,23 +6,23 @@ shared_examples 'different match methods' do |method| it "autocorrects #{method} /abc\\z/" do new_source = autocorrect_source("str#{method} /abc\\z/") - expect(new_source).to eq "str.end_with?('abc')" + expect(new_source).to eq "str&.end_with?('abc')" end it "autocorrects #{method} /\\n\\z/" do new_source = autocorrect_source("str#{method} /\\n\\z/") - expect(new_source).to eq 'str.end_with?("\n")' + expect(new_source).to eq 'str&.end_with?("\n")' end it "autocorrects #{method} /\\t\\z/" do new_source = autocorrect_source("str#{method} /\\t\\z/") - expect(new_source).to eq 'str.end_with?("\t")' + expect(new_source).to eq 'str&.end_with?("\t")' end %w[. $ ^ |].each do |str| it "autocorrects #{method} /\\#{str}\\z/" do new_source = autocorrect_source("str#{method} /\\#{str}\\z/") - expect(new_source).to eq "str.end_with?('#{str}')" + expect(new_source).to eq "str&.end_with?('#{str}')" end it "doesn't register an error for #{method} /#{str}\\z/" do @@ -36,7 +36,7 @@ %w[a e f r t v].each do |str| it "autocorrects #{method} /\\#{str}\\z/" do new_source = autocorrect_source("str#{method} /\\#{str}\\z/") - expect(new_source).to eq %{str.end_with?("\\#{str}")} + expect(new_source).to eq %{str&.end_with?("\\#{str}")} end end @@ -51,7 +51,7 @@ %w[i j l m o q y].each do |str| it "autocorrects #{method} /\\#{str}\\z/" do new_source = autocorrect_source("str#{method} /\\#{str}\\z/") - expect(new_source).to eq "str.end_with?('#{str}')" + expect(new_source).to eq "str&.end_with?('#{str}')" end end @@ -64,7 +64,7 @@ it "autocorrects #{method} /\\\\\\z/" do new_source = autocorrect_source("str#{method} /\\\\\\z/") - expect(new_source).to eq("str.end_with?('\\\\')") + expect(new_source).to eq("str&.end_with?('\\\\')") end end diff --git a/spec/rubocop/cop/performance/start_with_spec.rb b/spec/rubocop/cop/performance/start_with_spec.rb index 8e125b4ff8..0b1f56735d 100644 --- a/spec/rubocop/cop/performance/start_with_spec.rb +++ b/spec/rubocop/cop/performance/start_with_spec.rb @@ -6,7 +6,7 @@ shared_examples 'different match methods' do |method| it "autocorrects #{method} /\\Aabc/" do new_source = autocorrect_source("str#{method} /\\Aabc/") - expect(new_source).to eq "str.start_with?('abc')" + expect(new_source).to eq "str&.start_with?('abc')" end # escapes like "\n" @@ -15,7 +15,7 @@ %w[a e f r t v].each do |str| it "autocorrects #{method} /\\A\\#{str}/" do new_source = autocorrect_source("str#{method} /\\A\\#{str}/") - expect(new_source).to eq %{str.start_with?("\\#{str}")} + expect(new_source).to eq %{str&.start_with?("\\#{str}")} end end @@ -23,7 +23,7 @@ %w[. * ? $ ^ |].each do |str| it "autocorrects #{method} /\\A\\#{str}/" do new_source = autocorrect_source("str#{method} /\\A\\#{str}/") - expect(new_source).to eq "str.start_with?('#{str}')" + expect(new_source).to eq "str&.start_with?('#{str}')" end it "doesn't register an error for #{method} /\\A#{str}/" do @@ -42,7 +42,7 @@ %w[i j l m o q y].each do |str| it "autocorrects #{method} /\\A\\#{str}/" do new_source = autocorrect_source("str#{method} /\\A\\#{str}/") - expect(new_source).to eq "str.start_with?('#{str}')" + expect(new_source).to eq "str&.start_with?('#{str}')" end end @@ -55,7 +55,7 @@ it "autocorrects #{method} /\\A\\\\/" do new_source = autocorrect_source("str#{method} /\\A\\\\/") - expect(new_source).to eq("str.start_with?('\\\\')") + expect(new_source).to eq("str&.start_with?('\\\\')") end end