From fe478d7e13f3844375a52fc0f6e91b09660eda71 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 16 Sep 2020 20:29:34 +0900 Subject: [PATCH] [Fix #8730] Fix an error for `Lint/UselessTimes` Fixes #8730. This PR fixes an error for `Lint/UselessTimes` when there is a blank line in the method definition. --- CHANGELOG.md | 1 + lib/rubocop/cop/lint/useless_times.rb | 7 ++++++- spec/rubocop/cop/lint/useless_times_spec.rb | 21 +++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6930b0a6878..a385c95b24a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * [#8720](https://github.com/rubocop-hq/rubocop/issues/8720): Fix an error for `Lint/IdentityComparison` when calling `object_id` method without receiver in LHS or RHS. ([@koic][]) * [#8710](https://github.com/rubocop-hq/rubocop/issues/8710): Fix a false positive for `Layout/RescueEnsureAlignment` when `Layout/BeginEndAlignment` cop is not enabled status. ([@koic][]) * [#8726](https://github.com/rubocop-hq/rubocop/issues/8726): Fix a false positive for `Naming/VariableNumber` when naming multibyte character variable name. ([@koic][]) +* [#8730](https://github.com/rubocop-hq/rubocop/issues/8730): Fix an error for `Lint/UselessTimes` when there is a blank line in the method definition. ([@koic][]) ### Changes diff --git a/lib/rubocop/cop/lint/useless_times.rb b/lib/rubocop/cop/lint/useless_times.rb index 1ffe628103e..32c4703c172 100644 --- a/lib/rubocop/cop/lint/useless_times.rb +++ b/lib/rubocop/cop/lint/useless_times.rb @@ -82,7 +82,12 @@ def autocorrect_block(corrector, node) def fix_indentation(source, range) # Cleanup indentation in a multiline block source_lines = source.split("\n") - source_lines[1..-1].each { |line| line[range] = '' } + + source_lines[1..-1].each do |line| + next if line.empty? + + line[range] = '' + end source_lines.join("\n") end diff --git a/spec/rubocop/cop/lint/useless_times_spec.rb b/spec/rubocop/cop/lint/useless_times_spec.rb index 428247f80e2..b609dcba3eb 100644 --- a/spec/rubocop/cop/lint/useless_times_spec.rb +++ b/spec/rubocop/cop/lint/useless_times_spec.rb @@ -61,6 +61,27 @@ RUBY end + it 'registers an offense and corrects when there is a blank line in the method definition' do + expect_offense(<<~RUBY) + def foo + 1.times do + ^^^^^^^^^^ Useless call to `1.times` detected. + bar + + baz + end + end + RUBY + + expect_correction(<<~RUBY) + def foo + bar + + baz + end + RUBY + end + it 'does not register an offense for an integer > 1' do expect_no_offenses(<<~RUBY) 2.times { |i| puts i }