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
Cop idea: Set the default value of ENV.fetch
by autocorrect
#10578
Comments
Style/FetchEnvVar
- Set the default value of ENV.fetch
ENV.fetch
by autocorrection
Seems like a good idea to me. As for the auto-correct - I guess if the default is some literal or a local value we can use the first form and if it's a method call we can go for the block style, although I don't have a strong preference. The performance gains would be notable only if the computation of the default value is expensive. |
ENV.fetch
by autocorrectionENV.fetch
by autocorrect
Please note that if you got code like this: return ENV['foo'] if ENV.key?('foo')
File.read('file_if_foo_not_exists') Code like this will work fine if But if you by similarity with proposal from this issue change it to ENV.fetch('foo', File.read('file_if_foo_not_exists')) If will fail in any case if |
Great point. That would be one of the cases we should use the block style. ENV.fetch('foo') { File.read('file_if_foo_not_exists') } In my opinion, in most cases always using the block style is safer and reduces the rules. Any thoughts? |
I created a PR for this. |
Is your feature request related to a problem? Please describe.
From: #10502 (comment)
With
Style/FetchEnvVar
, code likegets autocorrected to
Autocorrecting like below helps people reduce their manual changings like above.
Describe the solution you'd like
ENV[]
is the left-hand of||
, the autocorrect can move the right-hand of||
to the default value ofENV.fetch
.||
can't be the default value (e.g.,next
), so we need to carefully handle them.The text was updated successfully, but these errors were encountered: