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 #171] Extend auto-correction support for Performance/Sum #188

Merged

Conversation

koic
Copy link
Member

@koic koic commented Nov 5, 2020

Fixes #171.

This PR makes Performance/Sum cop can be changed auto-correction scope depending on the value of SafeAutoCorrect in .rubocop.yml.

Its auto-correction is marked as safe by default (SafeAutoCorrect: true) to prevent TypeError in auto-correced code when initial value is not specified as shown below:

['a', 'b'].sum # => (String can't be coerced into Integer)

Therefore if initial value is not specify as in that code, it will not be auto-corrected.

If user always want to enable auto-correction, user can set SafeAutoCorrect: false.

Performance/Sum:
  SafeAutoCorrect: false

SafeAutoCorrect remains true by default, so the behavior does not change.


Before submitting the PR make sure the following are checked:

  • 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.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@koic
Copy link
Member Author

koic commented Nov 5, 2020

@rubocop-hq/rubocop-core User can change auto-correction behaviour by specifying from SafeAutoCorrect: true (default) to SafeAutoCorrect: false in .rubocop.yml. That may be an unprecedented (or unexpected) usage. If you have any concerns about using the SafeAutoCorrect option, please comment your feedback.

@marcandre
Copy link
Contributor

marcandre commented Nov 7, 2020

I'd like to know why you are proposing this instead of marking this cop as SafeAutoCorrect: false?

@marcandre
Copy link
Contributor

Oh, sorry, I understand that you want the safe autocorrection to be always done.

It would be better if add_offense accepted a parameter to say "this one is safe to auto-correct"

@koic
Copy link
Member Author

koic commented Nov 9, 2020

Thank you for your feedback. I can probably add to the doc about always safe auto-correction cases.
Is there anything I need to update about this PR's implementation? I'm sorry for my lack of understanding 💦

@marcandre
Copy link
Contributor

Actually, I like your implementation.

I think it we should have better support for this kind of cop. Maybe this cop could specify: SafeAutoCorrect: both?

@koic
Copy link
Member Author

koic commented Nov 12, 2020

Thank you for supplementing. I only saw it from one perspective, you've made me realize there's other perspectives to this 😅
Currently only SafeAutoCorrect: true and SafeAutoCorrect: false are supported by core, so I'd like to start with this boolean toggle first.

Fixes rubocop#171.

This PR makes `Performance/Sum` cop can be changed auto-correction scope
depending on the value of `SafeAutoCorrect` in .rubocop.yml.

Its auto-correction is marked as safe by default (`SafeAutoCorrect: true`)
to prevent `TypeError` in auto-correced code when initial value is not
specified as shown below:

```ruby
['a', 'b'].sum # => (String can't be coerced into Integer)
```

Therefore if initial value is not specify as in that code, it will not be auto-corrected.

If user always want to enable auto-correction, user can set `SafeAutoCorrect: false`.

```yaml
Performance/Sum:
  SafeAutoCorrect: false
```

`SafeAutoCorrect` remains `true` by default, so the behavior does not change.
@koic koic force-pushed the extend_support_autocorrect_for_performance_sum branch from 2da0d9e to 591c711 Compare November 12, 2020 07:48
@koic koic merged commit 2fe6f67 into rubocop:master Nov 14, 2020
@koic koic deleted the extend_support_autocorrect_for_performance_sum branch November 14, 2020 18:11
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.

Performance/Sum not autocorrecting injects or reduces with blocks
2 participants