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 false positives about double quotes #4112

Merged

Conversation

pocke
Copy link
Collaborator

@pocke pocke commented Mar 13, 2017

StringLiterals, UnneededCapitalW and UnneededPercentQ cops have
false positives about double quotes.
If escaped normal character that is like \! exists in a literal, those
cops make false positives.

For examples

UnneededPercentQ

%Q{"foo\!"}

The above code can't be replaced with '"foo\!"'.

%Q{"foo\!"} == '"foo\!"'  # => false

But the cop adds an offense for the code.

$ rubocop --only UnneededPercentQ
Inspecting 1 file
C

Offenses:

test.rb:1:1: C: Use %Q only for strings that contain both single quotes and double quotes, or for dynamic strings that contain double quotes.
%Q{"foo\!"}
^^^^^^^^^^^

1 file inspected, 1 offense detected

UnneededCapitalW

%W(a \! b)

The above code can't be replaced with %w(a \! b)(small w).

%W(a \! b) == %w(a \! b)  # => false

But the cop adds an offense for the code.

$ rubocop --only UnneededCapitalW
Inspecting 1 file
C

Offenses:

test.rb:1:1: C: Do not use %W unless interpolation is needed. If not, use %w.
%W(a \! b)
^^^^^^^^^^

1 file inspected, 1 offense detected

StringLiterals

 # .rubocop.yml
StringLiterals:
  ConsistentQuotesInMultiline: true
"foo \!" \
  "\!"

The double quotes in the above code can't be replaced with single
quotes.
But the cop adds an offense for the code.

$ rubocop --only StringLiterals
Inspecting 1 file
C

Offenses:

test.rb:1:1: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
"foo \!" \ ...
^^^^^^^^^^

1 file inspected, 1 offense detected

The cause about this false positives is double_quotes_acceptable? method.
The method receives a string value. For example, when a literal is "\!", the method receives "!". So, the presence or absence of escape will be lost.

This change fixed the problems.

Replace this text with a summary of the changes in your PR.
The more detailed you are, the better.


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 are passing.
  • The new code doesn't generate RuboCop offenses.
  • 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 good-bye-double_quotes_acceptable-method branch from 77d0486 to e5ec6e4 Compare March 13, 2017 10:49
pocke and others added 2 commits March 13, 2017 19:50
`StringLiterals`, `UnneededCapitalW` and `UnneededPercentQ` cops have
false positives about double quotes.
If escaped normal character that is like `\!` exists in a literal, those
cops make false positives.

For examples
======

`UnneededPercentQ`
-------

```ruby
%Q{"foo\!"}
```

The above code can't be replaced with `'"foo\!"'`.

```ruby
%Q{"foo\!"} == '"foo\!"'  # => false
```

But the cop adds an offense for the code.

```sh
$ rubocop --only UnneededPercentQ
Inspecting 1 file
C

Offenses:

test.rb:1:1: C: Use %Q only for strings that contain both single quotes and double quotes, or for dynamic strings that contain double quotes.
%Q{"foo\!"}
^^^^^^^^^^^

1 file inspected, 1 offense detected
```

`UnneededCapitalW`
-----------

```ruby
%W(a \! b)
```

The above code can't be replaced with `%w(a \! b)`(small `w`).

```ruby
%W(a \! b) == %w(a \! b)  # => false
```

But the cop adds an offense for the code.

```sh
$ rubocop --only UnneededCapitalW
Inspecting 1 file
C

Offenses:

test.rb:1:1: C: Do not use %W unless interpolation is needed. If not, use %w.
%W(a \! b)
^^^^^^^^^^

1 file inspected, 1 offense detected
```

`StringLiterals`
----------

```yaml
 # .rubocop.yml
StringLiterals:
  ConsistentQuotesInMultiline: true
```

```ruby
"foo \!" \
  "\!"
```

The double quotes in the above code can't be replaced with single
quotes.
But the cop adds an offense for the code.

```sh
$ rubocop --only StringLiterals
Inspecting 1 file
C

Offenses:

test.rb:1:1: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
"foo \!" \ ...
^^^^^^^^^^

1 file inspected, 1 offense detected
```

------

The cause about this false positives is `double_quotes_acceptable?` method.
The method receives a string value. For example, when a literal is `"\!"`, the method receives `"!"`. So, the presence or absence of escape will be lost.

This change fixed the problems.
@bbatsov bbatsov merged commit e857280 into rubocop:master Mar 13, 2017
@pocke pocke deleted the good-bye-double_quotes_acceptable-method branch March 18, 2017 13:59
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.

2 participants