Skip to content

Commit

Permalink
[Fix #4444] Fix Style/AutoResourceCleanup shouldn't flag `File.open…
Browse files Browse the repository at this point in the history
…(...).close (#5186)
  • Loading branch information
dpostorivo authored and bbatsov committed Dec 21, 2017
1 parent b6d3cfe commit 78d70ca
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@
* [#5226](https://github.com/bbatsov/rubocop/issues/5226): Suppress false positives for `Rails/RedundantReceiverInWithOptions` when including another receiver in `with_options`. ([@wata727][])
* [#5238](https://github.com/bbatsov/rubocop/pull/5238): Fix error when #present? or #blank? is used in if or unless modifier. ([@eitoball][])
* [#5261](https://github.com/bbatsov/rubocop/issues/5261): Fix a false positive for `Style/MixinUsage` when using inside class or module. ([@koic][])
* [#4444](https://github.com/bbatsov/rubocop/issues/4444): Fix `Style/AutoResourceCleanup` shouldn't flag `File.open(...).close. ([@dpostorivo][])
* [#5278](https://github.com/bbatsov/rubocop/pull/5278): Fix deprecation check to use `loaded_path` in warning. ([@chrishulton][])

### Changes
Expand Down
11 changes: 9 additions & 2 deletions lib/rubocop/cop/style/auto_resource_cleanup.rb
Expand Up @@ -29,15 +29,22 @@ def on_send(node)

next if node.receiver != target_receiver
next if node.method_name != target_method
next if node.parent && node.parent.block_type?
next if node.block_argument?
next if cleanup?(node)

add_offense(node,
message: format(MSG,
class: target_class,
method: target_method))
end
end

private

def cleanup?(node)
parent = node.parent
node.block_argument? ||
(parent && (parent.block_type? || !parent.lvasgn_type?))
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/rubocop/cop/style/auto_resource_cleanup_spec.rb
Expand Up @@ -17,4 +17,8 @@
it 'does not register an offense for File.open with block-pass' do
expect_no_offenses('File.open("file", &:read)')
end

it 'does not register an offense for File.open with immediate close' do
expect_no_offenses('File.open("file", "w", 0o777).close')
end
end

0 comments on commit 78d70ca

Please sign in to comment.