Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #8730] Fix an error for Lint/UselessTimes #8733

Merged
merged 1 commit into from Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion lib/rubocop/cop/lint/useless_times.rb
Expand Up @@ -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

Expand Down
21 changes: 21 additions & 0 deletions spec/rubocop/cop/lint/useless_times_spec.rb
Expand Up @@ -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 }
Expand Down