Skip to content

Rails/EnvironmentComparison does not recognize case statements #1395

Open
@maxjacobson

Description

@maxjacobson

I've gotten accustomed to preferring if Rails.env.production? over if Rails.env == 'production' as a result of following the Rails/EnvironmentComparison rule. I noticed recently that code like this is still permissible:

case Rails.env
when "production"
  foo
else
  bar
end

This seems to violate the spirit of the rule, and also like it could be detected with static analysis.

Similarly, case statements that use pattern matching are also permitted:

case Rails.env
in "production"
  foo
in "test" | "development"
  bar
end

Perhaps this should also trigger an offense. What do you think?

Expected behavior

The examples above ought to produce Rails/EnvironmentComparison offenses.

Perhaps they could also be corrected to the equivalent if ... else ... end statements, but if they produced offenses that would be a good start.

Actual behavior

The examples above do not produce offenses.

Steps to reproduce the problem

Write code such as the above and run rubocop.

RuboCop version

1.69.1 (using Parser 3.3.6.0, rubocop-ast 1.36.2, analyzing as Ruby 3.3, running on ruby 3.3.6) [arm64-darwin24]
  - rubocop-capybara 2.21.0
  - rubocop-factory_bot 2.26.1
  - rubocop-graphql 1.5.4
  - rubocop-rails 2.27.0
  - rubocop-rspec 3.2.0
  - rubocop-rspec_rails 2.30.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions