-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Add new Style/FileTouch
cop
#13484
Conversation
|
||
module RuboCop | ||
module Cop | ||
module Style |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
65ea6a1
to
54a2603
Compare
54a2603
to
1effbaf
Compare
Looks good to me. Thanks! |
Closes #13482.
Adds new
Style/FileTouch
cop to detect usage ofFile.open
in append mode with empty block and autocorrect it toFileUtils.touch
.Before:
After:
Recognized access modes are
a
,a+
,ab
,a+b
,at
,a+t
.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.