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

[New Cop Idea] File.empty? #11629

Closed
bbatsov opened this issue Feb 26, 2023 · 0 comments · Fixed by #11645
Closed

[New Cop Idea] File.empty? #11629

bbatsov opened this issue Feb 26, 2023 · 0 comments · Fixed by #11645
Labels
feature request good first issue Easy task, suitable for newcomers to the project

Comments

@bbatsov
Copy link
Collaborator

bbatsov commented Feb 26, 2023

Checking whether a file is empty in Ruby was always a bit weird:

  • You can use File.zero? (class method of File)
  • You can use file.size == 0 or file.size.zero?

For some reason Ruby has always shied away from adding an empty? method to file objects (you can't do file.empty?, but at least Ruby 2.4 added an File.empty? method that is an alias of File.zero? and reads better. It'd be nice (and easy) to have some cop promoting the use of File.empty?.

@bbatsov bbatsov added feature request good first issue Easy task, suitable for newcomers to the project labels Feb 26, 2023
nitishparkar added a commit to nitishparkar/rubocop that referenced this issue Mar 1, 2023
ydah added a commit to ydah/rubocop that referenced this issue Mar 6, 2023
Fix rubocop#11629

```ruby
# bad
File.zero?('path/to/file')
File.size('path/to/file') == 0
File.size('path/to/file').zero?
File.read('path/to/file').empty?
File.binread('path/to/file').empty?
FileTest.zero?('path/to/file')

# good
File.empty?('path/to/file')
FileTest.empty?('path/to/file')
```
bbatsov pushed a commit that referenced this issue Mar 6, 2023
Fix #11629

```ruby
# bad
File.zero?('path/to/file')
File.size('path/to/file') == 0
File.size('path/to/file').zero?
File.read('path/to/file').empty?
File.binread('path/to/file').empty?
FileTest.zero?('path/to/file')

# good
File.empty?('path/to/file')
FileTest.empty?('path/to/file')
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request good first issue Easy task, suitable for newcomers to the project
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant