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

Style/InvertibleUnlessCondition autocorrections may break application logic #11800

Closed
indiebrain opened this issue Apr 14, 2023 · 0 comments · Fixed by #11801
Closed

Style/InvertibleUnlessCondition autocorrections may break application logic #11800

indiebrain opened this issue Apr 14, 2023 · 0 comments · Fixed by #11801
Labels

Comments

@indiebrain
Copy link

Given an specific style of comparison expression where the arguments are Modules or Classes Style/InvertibleUnlessCondition's autocorrection can break application logic.

Expected behavior

rubocop --autocorrect --only Style/InvertibleUnlessCondition

Either this cop should not run when running "safe" autocorrections - via the -a or --autocorrect flags - or, the Cop should ignore expressions comparing Module / Class hierarchies.

Actual behavior

Comparisons using a Module's Class's hierarchy break.

Steps to reproduce the problem

$ ruby --version
ruby 3.1.2p20
  1. Define a method under test which violates this Cop - example_test.rb
require "minitest/autorun"

class ExampleTest < Minitest::Test
  def test_is_not_ancestor_of_standard_error
    my_mod = Class.new(Object)

    not_an_error_class = true unless my_mod <= StandardError

    assert not_an_error_class
  end

  def test_is_ancestor_of_standard_error
    my_mod = Class.new(StandardError)

    not_an_error_class = true unless my_mod <= StandardError

    refute not_an_error_class
  end
end
  1. Run the test and see it pass:
$ ruby example_test.rb
Run options: --seed 58076

# Running:

..

Finished in 0.000446s, 4484.3049 runs/s, 4484.3049 assertions/s.

2 runs, 2 assertions, 0 failures, 0 errors, 0 skips
  1. Run the rubocop with the Style/InvertibleUnlessCondition Cop in "safe autocorrect" mode:
$ rubocop --autocorrect --only Style/InvertibleUnlessCondition example_test.rb
warning: parser/current is loading parser/ruby31, which recognizes 3.1.4-compliant syntax, but you are running 3.1.2.
Please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
Inspecting 1 file
C

Offenses:

example_test.rb:7:5: C: [Corrected] Style/InvertibleUnlessCondition: Favor if with inverted condition over unless.
    not_an_error_class = true unless my_mod <= StandardError
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
example_test.rb:15:5: C: [Corrected] Style/InvertibleUnlessCondition: Favor if with inverted condition over unless.
    not_an_error_class = true unless my_mod <= StandardError
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 2 offenses detected, 2 offenses corrected

  1. The test will now fail
$ ruby example_test.rb
Run options: --seed 28429

# Running:

F.

Finished in 0.000665s, 3007.5188 runs/s, 3007.5188 assertions/s.

  1) Failure:
ExampleTest#test_is_not_ancestor_of_standard_error [example_test.rb:9]:
Expected nil to be truthy.

2 runs, 2 assertions, 1 failures, 0 errors, 0 skips

RuboCop version

Include the output of rubocop -V or bundle exec rubocop -V if using Bundler.
If you see extension cop versions (e.g. rubocop-performance, rubocop-rspec, and others)
output by rubocop -V, include them as well. Here's an example:

$ rubocop -V
1.50.1 (using Parser 3.2.2.0, rubocop-ast 1.28.0, running on ruby 3.1.2) [arm64-darwin21]
  - rubocop-graphql 1.0.1
  - rubocop-performance 1.16.0
  - rubocop-rails 2.19.0
  - rubocop-rspec 2.17.1
  - rubocop-sorbet 0.7.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants