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 Lint/RegexpInCondition cop #4854

Merged
merged 1 commit into from
Oct 14, 2017

Conversation

pocke
Copy link
Collaborator

@pocke pocke commented Oct 11, 2017

Problem

If a regexp literal is in condition, the regexp matches $_ implicitly.
For example:

# test.rb
$_ = 'foo'
if /foo/
  p 1
end

$_ = 'bar'
if /foo/
  p 2
end
# It outputs 1, but it does not output 2.
$ ruby test.rb
1

The behaviour is confusing. I guess the feature is designed for one-liner. So, we should not use the feature in production code.

Solution

Add this cop 😄

Lint/LiteralInCondition?

This cop is very similar to Lint/LiteralInCondition cop. But this should be a different cop from Lint/LiteralInCondition.
There are two reasons.

First, these cops target different issues. Lint/LiteralInCondition targets the issue that a condition is evaluated as true. But Lint/RegexpInCondition targets the issue that a regexp literal matches $_ implicitly.

Second, these cops have different implementation.

Note

Currently, this cop does not work on ! and not.
For example:

# This code has the problem, but the cop does not add offence for the code.
!/re/

It is a parser gem's bug, and the bug is already fixed in master.
whitequark/parser#376


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).
  • Used the same coding conventions as the rest of the project.
  • 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.
  • All tests(rake spec) are passing.
  • The new code doesn't generate RuboCop offenses that are checked by rake internal_investigation.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

@pocke pocke force-pushed the Lint/MatchCurrentLine branch from fd20aa8 to 25ed928 Compare October 11, 2017 10:07
@@ -1338,6 +1338,12 @@ Lint/RedundantWithObject:
Description: 'Checks for redundant `with_object`.'
Enabled: true

Lint/RegexpInCondition:
Description: >-
Do not use regexp literal in condition.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the message should be more specific, since e.g. if /foo/ =~ str is also a regexp literal in a condition. Perhaps the cop name should even be RegexpCondition? 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess @pocke just followed the (bad) naming of the other cop. Maybe it should have been named LiteralAsCondition or LiteralCondition.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. yes.

@bbatsov bbatsov merged commit b2c6da6 into rubocop:master Oct 14, 2017
@bbatsov
Copy link
Collaborator

bbatsov commented Oct 14, 2017

Let's improve the cop name and their messages subsequently.

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.

4 participants