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

0.41.1: Style/EachForSimpleLoop autocorrection calculates wrong number of repetitions #3271

Closed
dabroz opened this issue Jul 1, 2016 · 2 comments

Comments

@dabroz
Copy link
Contributor

dabroz commented Jul 1, 2016

Style/EachForSimpleLoop autocorrection calculates wrong number of repetitions.

Test case

(1..5).each { print '+' } # result: +++++

Expected behavior

5.times { print '+' } # result: +++++

Actual behavior

4.times { print '+' } # result: ++++

Steps to reproduce the problem

  1. Put test case in a test.rb file
  2. Run rubocop -a

RuboCop version

0.41.1 (using Parser 2.3.1.2, running on ruby 2.3.1 x86_64-darwin14)
@dabroz dabroz changed the title [0.41.1] Style/EachForSimpleLoop autocorrection calculates wrong number of repetitions 0.41.1: Style/EachForSimpleLoop autocorrection calculates wrong number of repetitions Jul 1, 2016
@Aqualon
Copy link
Contributor

Aqualon commented Jul 2, 2016

This was not completely fixed by #3221, the code https://github.com/bbatsov/rubocop/blob/e2af464b809180ae2de1785c362c25291995e586/lib/rubocop/cop/style/each_for_simple_loop.rb#L38 finds irange and erange type of ranges, but the max - min computation only works for erange.

@Drenmi
Copy link
Collaborator

Drenmi commented Jul 7, 2016

This is pretty bad actually. All the unit tests are incorrect. Example:

corrected = autocorrect_source(cop, '(0..10).each {}')
expect(corrected).to eq '10.times {}'

But (0..10) is an inclusive range of 0 through 10, i.e. 11 iterations. This is repeated throughout the suite. There are no test cases for exclusive ranges.

Drenmi added a commit to Drenmi/rubocop that referenced this issue Jul 7, 2016
… cop

There is a bug in `Style/EachForSimpleLoop`, where it auto-corrects
with the wrong number of iterations. Consider:

```
(0..10).each { ... }
```

This is an inclusive range of 0 through 10, i.e. 11 iterations.
This incorrectly gets auto-corrected to:

```
10.times { ... }
```

It turns out the tests for this auto-correct are incorrect, and
missing test cases for the exclusive range cases.

This fix adds test coverage, and makes the auto-correct behave
well for inclusive ranges.
@bbatsov bbatsov closed this as completed in afb4c4f Jul 8, 2016
Neodelf pushed a commit to Neodelf/rubocop that referenced this issue Oct 15, 2016
… cop (rubocop#3297)

There is a bug in `Style/EachForSimpleLoop`, where it auto-corrects
with the wrong number of iterations. Consider:

```
(0..10).each { ... }
```

This is an inclusive range of 0 through 10, i.e. 11 iterations.
This incorrectly gets auto-corrected to:

```
10.times { ... }
```

It turns out the tests for this auto-correct are incorrect, and
missing test cases for the exclusive range cases.

This fix adds test coverage, and makes the auto-correct behave
well for inclusive ranges.
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

No branches or pull requests

3 participants