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

Add new Style/StringChars cop #9615

Merged
merged 1 commit into from Mar 18, 2021
Merged

Conversation

@koic
Copy link
Member

@koic koic commented Mar 18, 2021

Summary

This PR adds new Style/StringChars cop. The cop checks for uses of String#split with empty string or regexp literal argument.

# bad
string.split(//)
string.split('')

# good
string.chars

Since Ruby 2.0, the behavior of String#chars and String#split(//) is the same.

Ruby 1.9 and lower

% ruby -ve "p 'foo'.split(//)"
ruby 1.9.3p551 (2014-11-13 revision 48407) [x86_64-darwin13.0.2]
["f", "o", "o"]
% ruby -ve "p 'foo'.chars"
ruby 1.9.3p551 (2014-11-13 revision 48407) [x86_64-darwin13.0.2]
#<Enumerator: "foo":chars>

Ruby 2.0 and higher

% ruby -ve "p 'foo'.split(//)"
ruby 2.0.0p648 (2015-12-16 revision 53162) [x86_64-darwin13.0.2]
["f", "o", "o"]
% ruby -ve "p 'foo'.chars"
ruby 2.0.0p648 (2015-12-16 revision 53162) [x86_64-darwin13.0.2]
["f", "o", "o"]

Writing String#chars would pretty express the intent of the code.

Additional Information

This cop is intended to use String#chars method that express readable code.
And it has a different purpose than Performance/RedundantSplitRegexpArgument cop.
https://docs.rubocop.org/rubocop-performance/cops_performance.html#performanceredundantsplitregexpargument

Therefore, it will be added as a new Style cop.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • 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.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
@koic koic force-pushed the koic:add_new_style_string_chars_cop branch from 8122e36 to c5fe028 Mar 18, 2021
@@ -4493,6 +4493,11 @@ Style/StderrPuts:
Enabled: true
VersionAdded: '0.51'

Style/StringChars:
Description: 'checks for uses of `String#split` with empty string or regexp literal argument.'

This comment has been minimized.

@bbatsov

bbatsov Mar 18, 2021
Collaborator

I guess you should mention that the cops is not safe and add some notes about the safety.

This comment has been minimized.

@bbatsov

bbatsov Mar 18, 2021
Collaborator

Actually, probably it's quite unlikely that some other class would define a split method that takes exactly the same args. :-)

This comment has been minimized.

@koic

koic Mar 18, 2021
Author Member

Thank you for the review. I marked the cop as unsafe and added a description to safety.
Initially, I didn't mark it as unsafe because I think it's less likely to be false positives 😅

@bbatsov
Copy link
Collaborator

@bbatsov bbatsov commented Mar 18, 2021

Great cop - simple and useful at the same time!

## Summary

This PR adds new `Style/StringChars` cop. The cop checks for uses of
`String#split` with empty string or regexp literal argument.

```ruby
# bad
string.split(//)
string.split('')

# good
string.chars
```

Since Ruby 2.0, the behavior of `String#chars` and `String#split(//)` is the same.

### Ruby 1.9 and lower

```console
% ruby -ve "p 'foo'.split(//)"
ruby 1.9.3p551 (2014-11-13 revision 48407) [x86_64-darwin13.0.2]
["f", "o", "o"]
% ruby -ve "p 'foo'.chars"
ruby 1.9.3p551 (2014-11-13 revision 48407) [x86_64-darwin13.0.2]
#<Enumerator: "foo":chars>
```

### Ruby 2.0 and higher

```console
% ruby -ve "p 'foo'.split(//)"
ruby 2.0.0p648 (2015-12-16 revision 53162) [x86_64-darwin13.0.2]
["f", "o", "o"]
% ruby -ve "p 'foo'.chars"
ruby 2.0.0p648 (2015-12-16 revision 53162) [x86_64-darwin13.0.2]
["f", "o", "o"]
```

Writing `String#chars` would pretty express the intent of the code.

## Additional Information

This cop is intended to use `String#chars` method that express readable code.
And it has a different purpose than `Performance/RedundantSplitRegexpArgument` cop.
https://docs.rubocop.org/rubocop-performance/cops_performance.html#performanceredundantsplitregexpargument

Therefore, it will be added as a new `Style` cop.
@koic koic force-pushed the koic:add_new_style_string_chars_cop branch from c5fe028 to 5fbfe36 Mar 18, 2021
@bbatsov bbatsov merged commit 5cd6116 into rubocop:master Mar 18, 2021
31 checks passed
31 checks passed
@github-actions
windows 2.4
Details
@github-actions
windows 2.5
Details
@github-actions
windows 2.6
Details
@github-actions
windows 2.7
Details
@github-actions
windows 3.0
Details
@github-actions
windows mingw
Details
@mergify
Summary 2 potential rules
Details
ci/circleci: cc-setup Your tests passed on CircleCI!
Details
ci/circleci: cc-upload-coverage Your tests passed on CircleCI!
Details
ci/circleci: documentation-checks Your tests passed on CircleCI!
Details
ci/circleci: jruby-9.2-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: jruby-9.2-rubocop Your tests passed on CircleCI!
Details
ci/circleci: jruby-9.2-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.4-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.4-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.4-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.5-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.5-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.5-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.6-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.6-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.6-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.7-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.7-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.7-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-3.0-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-3.0-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-3.0-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-spec Your tests passed on CircleCI!
Details
@koic koic deleted the koic:add_new_style_string_chars_cop branch Mar 18, 2021
koic added a commit to koic/ruby-style-guide that referenced this pull request Mar 19, 2021
Follow rubocop/rubocop#9615.

This PR adds "String#chars" rule.

```ruby
# bad
string.split(//)
string.split('')

# good
string.chars
```
koic added a commit to koic/ruby-style-guide that referenced this pull request Mar 19, 2021
Follow rubocop/rubocop#9615.

This PR adds "String#chars" rule.

```ruby
# bad
string.split(//)
string.split('')

# good
string.chars
```
bbatsov added a commit to rubocop/ruby-style-guide that referenced this pull request Mar 19, 2021
Follow rubocop/rubocop#9615.

This PR adds "String#chars" rule.

```ruby
# bad
string.split(//)
string.split('')

# good
string.chars
```
koic added a commit to koic/standard that referenced this pull request Apr 5, 2021
This PR updates RuboCop to 1.12.1.
rubocop/rubocop@v1.11.0...v1.12.1

It supports private API change from `RuboCop::DirectiveComment::DIRECTIVE_COMMENT_REGEXP`
to `RuboCop::DirectiveComment::COMMENT_DIRECTIVE_REGEXP`.

- rubocop/rubocop@7f1f75c
- rubocop/rubocop@0421710

And `Style/StringChars` cop is added in RuboCop 1.12. It is up to maintainers to
decide whether to enable it :-)
rubocop/rubocop#9615
jmkoni pushed a commit to testdouble/standard that referenced this pull request May 3, 2021
* Update rubocop from 1.12.1 to [1.13.0](https://github.com/rubocop-hq/rubocop/releases/tag/v1.13.0)
* Update rubocop-performance from 1.9.2 to [1.11.1](https://github.com/rubocop-hq/rubocop-performance/releases/tag/v1.11.1)
* Enabled the following rules:
  * [`Performance/RedundantSplitRegexpArgument`](rubocop/rubocop-performance#190)
  * [`Style/IfWithBooleanLiteralBranches`](rubocop/rubocop#9396)
  * [`Lint/TripleQuotes`](rubocop/rubocop#9402)
  * [`Lint/SymbolConversion`](rubocop/rubocop#9362)
  * [`Lint/OrAssignmentToConstant`](rubocop/rubocop#9363)
  * [`Lint/NumberedParameterAssignment`](rubocop/rubocop#9326)
  * [`Style/HashConversion`](rubocop/rubocop#9478)
  * [`Gemspec/DateAssignment`](rubocop/rubocop#9496)
  * [`Style/StringChars`](rubocop/rubocop#9615)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants