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 #10581] Fix a false positive for Style/FetchEnvVar #10582

Merged

Conversation

koic
Copy link
Member

@koic koic commented Apr 26, 2022

Fixes #10581.

This PR fixes a false positive for Style/FetchEnvVar when comparing with ENV['TERM'].
ENV['DISABLE_DOTENV'] != '1' could be treated the same as the already allowed in used_as_flag? (that is, used as a flag).


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.

@koic koic force-pushed the fix_a_false_positive_for_style_fetch_env_var_cop branch from 167927e to d691c4d Compare April 26, 2022 02:58
Fixes rubocop#10581.

This PR fixes a false positive for `Style/FetchEnvVar` when comparing with `ENV['TERM']`.
`ENV['DISABLE_DOTENV'] != '1'` could be treated the same as the already allowed in `used_as_flag?`
(that is, used as a flag).
@koic koic force-pushed the fix_a_false_positive_for_style_fetch_env_var_cop branch from d691c4d to 0b350b1 Compare April 26, 2022 02:58
@j-miyake
Copy link
Contributor

j-miyake commented Apr 26, 2022

I think there can be a case like the below where it isn't a flag use. (Not sure if this is a good example, though).
What do you think?

if ENV['SERVER_ROLE'] == 'mailing'
  # Disable some features that must not be used in 'mailing' role.
else
  # Expect other environment
end

@koic
Copy link
Member Author

koic commented Apr 26, 2022

Hm, it may be possible to allow comparisons only to compare with commonly used values as flags. e.g. '1', 'true'. Thoughts?

@j-miyake
Copy link
Contributor

j-miyake commented Apr 26, 2022

Yes, that would be a possible option, or we could suggest these allowable uses.

if ENV['DISABLE_DOTENV'].nil?
  ...
end
unless ENV['DISABLE_DOTENV']
  ...
end

@j-miyake
Copy link
Contributor

j-miyake commented Apr 26, 2022

But it is obvious that the developer knows how the logic branches by the variable in the condition, so detecting the code like my example as an offense may not make sense. (I mean this PR looks good to me now)

@koic
Copy link
Member Author

koic commented Apr 29, 2022

it is obvious that the developer knows how the logic branches by the variable in the condition

Yeah, probably so. I'm going to merge this PR. Thank you for your feedback.

@koic koic merged commit 793f145 into rubocop:master Apr 29, 2022
@koic koic deleted the fix_a_false_positive_for_style_fetch_env_var_cop branch April 29, 2022 20:15
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.

Style/FetchEnvVar - false positive cases with (not) equal operators
2 participants