-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Description
Is your feature request related to a problem? Please describe.
I'm frustrated with how the Style/FetchEnvVar
cop works. It currently encourages replacing ENV["FOO"]
with ENV.fetch("FOO", nil)
, but this replacement doesn’t offer any real advantage. Using fetch
with nil
as the fallback essentially mimics the behavior of ENV["FOO"]
, while losing readability and not leveraging the main benefit of fetch
, which is to explicitly handle missing keys.
Describe the solution you'd like
I’d like to propose that the cop be smarter about when it suggests using ENV.fetch
. Specifically, it should avoid autocorrecting or flagging ENV["FOO"]
usages unless a meaningful default value is being provided (i.e., something other than nil
). If the intention is to allow for a fallback value, fetch
makes sense; otherwise, it does not add value and may even reduce code clarity.
Describe alternatives you've considered
One alternative is to disable the cop entirely, but that removes any enforcement for meaningful uses of ENV.fetch
. Another idea is to allow configuration of acceptable default values or provide an option to ignore nil
as a second argument.
Additional context
The current autocorrect suggestion creates code like ENV.fetch("FOO", nil)
which is functionally identical to ENV["FOO"]
, but more verbose and arguably harder to read. This reduces developer confidence in the value provided by the cop.