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

Add new Style/FileTouch cop #13484

Merged
merged 1 commit into from
Nov 26, 2024

Conversation

lovro-bikic
Copy link
Contributor

@lovro-bikic lovro-bikic commented Nov 21, 2024

Closes #13482.

Adds new Style/FileTouch cop to detect usage of File.open in append mode with empty block and autocorrect it to FileUtils.touch.

Before:

File.open(filename, 'a') {}

After:

FileUtils.touch(filename)

Recognized access modes are a, a+, ab, a+b, at, a+t.


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.


module RuboCop
module Cop
module Style
Copy link
Contributor Author

@lovro-bikic lovro-bikic Nov 21, 2024

Choose a reason for hiding this comment

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

I'm wondering if this should be a Lint cop instead. In the wild, I've seen File.open used to make sure a file exists or to try update its timestamps (much like touch utility), the latter of which actually doesn't happen:

path = 'foo.tmp'
FileUtils.touch(path) # create a file

atime_1, ctime_1, mtime_1 = File.atime(path), File.ctime(path), File.mtime(path)
File.open(path, 'a') {}
atime_2, ctime_2, mtime_2 = File.atime(path), File.ctime(path), File.mtime(path)

atime_1 == atime_2 && ctime_1 == ctime_2 && mtime_1 == mtime_2
# => true

so this could be considered erroneous code.

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 on the fence about changing the department, but I think it'd be good to add this to the cop's rationale and examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I've updated the docs, let me know if I made it clear enough.

@lovro-bikic lovro-bikic force-pushed the feature/style-file-touch-cop branch from 65ea6a1 to 54a2603 Compare November 26, 2024 11:28
@lovro-bikic lovro-bikic force-pushed the feature/style-file-touch-cop branch from 54a2603 to 1effbaf Compare November 26, 2024 11:32
@bbatsov bbatsov merged commit 77f3e6f into rubocop:master Nov 26, 2024
22 checks passed
@bbatsov
Copy link
Collaborator

bbatsov commented Nov 26, 2024

Looks good to me. Thanks!

@lovro-bikic lovro-bikic deleted the feature/style-file-touch-cop branch November 26, 2024 12:21
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.

New cop to detect usage of File.open with empty block
3 participants