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

Lint/MultipleComparison should not be marked safe #9349

Closed
dgollahon opened this issue Jan 7, 2021 · 6 comments · Fixed by #9352
Closed

Lint/MultipleComparison should not be marked safe #9349

dgollahon opened this issue Jan 7, 2021 · 6 comments · Fixed by #9352
Labels

Comments

@dgollahon
Copy link
Contributor

dgollahon commented Jan 7, 2021

I am using a SQL query dsl with code like this:

query.where { date >= range.begin & date < range.end }

rubocop autocorrects this to:

query.where { date >= range.begin & date && range.begin & date < range.end }

which has different semantics.

(see follow-up comment for my original error here^^ 🤦)


Expected behavior

This cop seems to make wrong assumptions about the safety of operating on relational operators. Whatever the case may be, this should not change my code to something that could be semantically different (by default).

Actual behavior

It autocorrects to semantically different code as outlined above.

Steps to reproduce the problem

Run rubocop -a on source files with the snippets above.

RuboCop version

1.7.0 (using Parser 3.0.0.0, rubocop-ast 1.3.0, running on ruby 2.6.6 x86_64-darwin19)
  - rubocop-performance 1.9.2
  - rubocop-rspec 2.1.0
  - rubocop-rspec 2.1.0

Side note: that duplicate rubocop-rspec is not a typo, that's what the command outputs. Not sure if there's something wrong there as well or some quirk to my config^

@dgollahon
Copy link
Contributor Author

dgollahon commented Jan 7, 2021

Arg, I have screwed up my example 🤦 . It was originally meant to be parenthesized but I screwed up somewhere in working with the DSL and then got very confused by the autocorrect output and opened this issue. Due to operator precedence it should be:

(date >= range.begin) & (date < range.end)

so the lint did highlight something noteworthy but imo it still should not autocorrect like that. a < b < c form of code will generally not work but if it does, it is probably like that for a reason (< does not always mean "less than"). Transforming in the way that this cop does is still not safe despite my confusing/errant code example I chose above.

@koic
Copy link
Member

koic commented Jan 8, 2021

Thank you for your feedback. I've opened #9352.

Side note: that duplicate rubocop-rspec is not a typo, that's what the command outputs. Not sure if there's something wrong there as well or some quirk to my config^

That's interesting. Is it possible that .rubocop.yml contains require: rubocop-rspec twice?

@koic koic added the bug label Jan 8, 2021
@dgollahon
Copy link
Contributor Author

Thank you for your feedback. I've opened #9352.

That looks like a good improvement for reducing false positives! It does not appear to mark autocorrection as unsafe, however. It still seems to me like this should not be marked as a "safe" cop. As a very simple example if I write:

1 < 2 < 3 # => NoMethodError: undefined method `<' for true:TrueClass

I (rightly) get an error. If I let rubocop autocorrect it, it outputs the item below which returns true.

1 < 2 && 2 < 3 # => true

Unless "safe" means something different than I think it should mean there are pretty clear changes in semantics here. WDYT?

@dgollahon
Copy link
Contributor Author

That's interesting. Is it possible that .rubocop.yml contains require: rubocop-rspec twice?

It appears to happen because my rubocop config requires rubocop-rspec but it also uses inherit_gem to use a company-wide config which additionally requires rubocop-rspec. I assume that's not a problem and I suppose the output there was reasonable (?) but it was just something kind of funny I noticed.

@koic
Copy link
Member

koic commented Jan 8, 2021

Unless "safe" means something different than I think it should mean there are pretty clear changes in semantics here. WDYT?

Sure! I update the proposal #9352.

I will also investigate rubocop -V separately. Thank you for the additional information.

@dgollahon
Copy link
Contributor Author

Thanks @koic that's great. :)

koic added a commit to koic/rubocop that referenced this issue Jan 8, 2021
Follow rubocop#9349 (comment).

This PR fixes duplicate extension cop versions when `rubocop -V`.

The following is an example:

```yaml
# ext.yml
require:
  - rubocop-rspec

# .rubocop.yml
inherit_from: ext.yml
require:
  - rubocop-performance
  - rubocop-rspec

## Before

If there are duplicate requires, extension cop display will also be duplicated.
Duplicate requires will be displayed in duplicate.

```console
% rubocop -V
1.8.0 (using Parser 3.0.0.0, rubocop-ast 1.4.0, running on ruby 2.7.2 x86_64-darwin19)
  - rubocop-performance 1.9.2
  - rubocop-rspec 2.0.0
  - rubocop-rspec 2.0.0
```

## After

Even if there are duplicate requires, they will only be displayed once per extension cop.

```console
% rubocop -V
1.8.0 (using Parser 3.0.0.0, rubocop-ast 1.4.0, running on ruby 2.7.2 x86_64-darwin19)
  - rubocop-performance 1.9.2
  - rubocop-rspec 2.0.0
```
bbatsov pushed a commit that referenced this issue Jan 9, 2021
Follow #9349 (comment).

This PR fixes duplicate extension cop versions when `rubocop -V`.

The following is an example:

```yaml
# ext.yml
require:
  - rubocop-rspec

# .rubocop.yml
inherit_from: ext.yml
require:
  - rubocop-performance
  - rubocop-rspec

## Before

If there are duplicate requires, extension cop display will also be duplicated.
Duplicate requires will be displayed in duplicate.

```console
% rubocop -V
1.8.0 (using Parser 3.0.0.0, rubocop-ast 1.4.0, running on ruby 2.7.2 x86_64-darwin19)
  - rubocop-performance 1.9.2
  - rubocop-rspec 2.0.0
  - rubocop-rspec 2.0.0
```

## After

Even if there are duplicate requires, they will only be displayed once per extension cop.

```console
% rubocop -V
1.8.0 (using Parser 3.0.0.0, rubocop-ast 1.4.0, running on ruby 2.7.2 x86_64-darwin19)
  - rubocop-performance 1.9.2
  - rubocop-rspec 2.0.0
```
koic added a commit to koic/rubocop that referenced this issue Feb 3, 2021
Fixes rubocop#9349.

This PR fixes a false positive for `Lint/MultipleComparison`
when using `&`, `|`, and `^` set operation operators in multiple comparison.
e.g. `x < y & y < z`

I think that the cop can probably allow using these operators by default.
bbatsov pushed a commit that referenced this issue Feb 13, 2021
Fixes #9349.

This PR fixes a false positive for `Lint/MultipleComparison`
when using `&`, `|`, and `^` set operation operators in multiple comparison.
e.g. `x < y & y < z`

I think that the cop can probably allow using these operators by default.
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