Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Make a safe detection for Performance/StringInclude #143

Merged
merged 1 commit into from
Jul 3, 2020

Conversation

koic
Copy link
Member

@koic koic commented Jul 2, 2020

This PR makes a safe detection for Performance/StringInclude cop when match? argument is a variable.
Symbol object does not have an include? method.

The following is an auto-corrected example code.

% ruby -e ':symbol.include?(/pattern/)'
Traceback (most recent call last):
-e:1:in `<main>': undefined method `include?' for :symbol:Symbol (NoMethodError)

A variable possible to be a symbol object, so if match? argument is a variable, accept it.

As an unsafe option it might be possible to provide detection when argument is a variable.
First of all, this PR makes detection safe.

This PR haven't added a changelog entry as it is an unreleased feature.


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.

This PR makes a safe detection for `Performance/StringInclude` cop
when `match?` argument is a variable.
Symbol object does not have an `include?` method.

The following is an auto-corrected example code.

```console
% ruby -e ':symbol.include?(/pattern/)'
Traceback (most recent call last):
-e:1:in `<main>': undefined method `include?' for :symbol:Symbol (NoMethodError)
````

A variable possible to be a symbol object, so if `match?` argument is
a variable, accept it.

As an unsafe option it might be possible to provide detection when
argument is a variable.
First of all, this PR makes detection safe.

This PR haven't added a changelog entry as it is an unreleased feature.
@koic koic merged commit 7885e72 into rubocop:master Jul 3, 2020
@koic koic deleted the fix_false_positive_for_string_include branch July 3, 2020 15:26
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant