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 a suggestions for safer conversions for Lint/NonAtomicFileOperation #10781

Merged
merged 1 commit into from Jul 7, 2022

Conversation

ydah
Copy link
Member

@ydah ydah commented Jul 3, 2022

This PR is fix a suggestions for safer conversions for Lint/NonAtomicFileOperation.

Change the uniform conversion to rm_rf or mkdir_p to a safe change proposal as follows.

For non-recurrent file deletions

# bad
if File.exist?(path)
  File.remove(path)
end

# good
FileUtils.rm_f(path)

For cases that do not require replacement

# bad
if File.exist?(path)
  FileUtils.makedirs(path)
end

# good
FileUtils.makedirs(path)

In addition, when replacing a non-atomic file operation with a forced file creation or deletion, an offense message has been added as follows.

if FileTest.exist?(path)
   ^^^^^^^^^^^^^^^^^^^^^^^^ Remove an unnecessary existence checks `FileTest.exist?`.
  FileUtils.remove(path)
  ^^^^^^^^^^^^^^^^ Use atomic file operation method `FileUtils.rm_f`.
end

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • [-] Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@ydah ydah force-pushed the fix_non_atomic_file_operation branch 2 times, most recently from fd4cb08 to 818944a Compare July 3, 2022 03:30
@ydah ydah changed the title Change suggestions for safer conversions for Lint/NonAtomicFileOperation Fix a suggestions for safer conversions for Lint/NonAtomicFileOperation Jul 3, 2022
@ydah ydah force-pushed the fix_non_atomic_file_operation branch 3 times, most recently from fca0be1 to fd5e30d Compare July 5, 2022 13:38
REMOVE_METHODS = %i[remove remove_dir remove_entry remove_entry_secure delete unlink
remove_file rm rm_f rm_r rm_rf rmdir rmtree safe_unlink].freeze
RESTRICT_ON_SEND = (MAKE_METHODS + REMOVE_METHODS).freeze
MSG_REMOVE_FILE_EXIST_CHECK = 'Remove an unnecessary existence checks ' \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Remove unnecessary existence check"

Copy link
Member Author

@ydah ydah Jul 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much. I fixed it.

RESTRICT_ON_SEND = (MAKE_METHODS + REMOVE_METHODS).freeze
MSG_REMOVE_FILE_EXIST_CHECK = 'Remove an unnecessary existence checks ' \
'`%<receiver>s.%<method_name>s`.'
MSG_CHANGE_FORCE_METHOD = 'Change to a forced file operation method ' \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if most people would understand this. :D I get what you mean, but I had to think for a moment what's "forced file operation" in this context.

Copy link
Member Author

@ydah ydah Jul 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's for sure. I was wondering what to do with this sentence.
How about "Use atomic file operation methods" ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would be better.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much. I changed to "Use atomic file operation methods".

@ydah ydah force-pushed the fix_non_atomic_file_operation branch 2 times, most recently from be6b0c6 to 2db1687 Compare July 7, 2022 06:44
RESTRICT_ON_SEND = (MAKE_METHODS + REMOVE_METHODS).freeze
MSG_REMOVE_FILE_EXIST_CHECK = 'Remove unnecessary existence check ' \
'`%<receiver>s.%<method_name>s`.'
MSG_CHANGE_FORCE_METHOD = 'Use atomic file operation methods `FileUtils.%<method_name>s`.'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

methods -> method

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I updated this PR.

…ion`

This PR is fix a suggestions for safer conversions for `Lint/NonAtomicFileOperation`.

Change the uniform conversion to `rm_rf` or `mkdir_p` to a safe change proposal as follows.

For non-recurrent file deletions
```ruby
# bad
if File.exist?(path)
  File.remove(path)
end

# good
FileUtils.rm_f(path)
```

For cases that do not require replacement
```ruby
# bad
if File.exist?(path)
  FileUtils.makedirs(path)
end

# good
FileUtils.makedirs(path)
```

In addition, when replacing a non-atomic file operation with a forced file creation or deletion, an offense message has been added as follows.

```ruby
if FileTest.exist?(path)
   ^^^^^^^^^^^^^^^^^^^^^^^^ Remove an unnecessary existence checks `FileTest.exist?`.
  FileUtils.remove(path)
  ^^^^^^^^^^^^^^^^ Use atomic file operation method `FileUtils.rm_f`.
end
```
@ydah ydah force-pushed the fix_non_atomic_file_operation branch from 2db1687 to c95db16 Compare July 7, 2022 07:09
@bbatsov bbatsov merged commit 4464853 into rubocop:master Jul 7, 2022
@bbatsov
Copy link
Collaborator

bbatsov commented Jul 7, 2022

Thanks!

@ydah ydah deleted the fix_non_atomic_file_operation branch July 7, 2022 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants