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 a false positive for Style/StringLiterals #10166

Merged

Conversation

koic
Copy link
Member

@koic koic commented Oct 5, 2021

Follow up to a7ef72a#r57503462.

This PR fixes a false positive for Style/StringLiterals when using '\s' with EnforcedStyle: double_quotes


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 referenced this pull request Oct 5, 2021
…correctly.

Two cases are improved here:
1. Escaped double quotes (`\"`) within a double-quoted string are handled correctly (replaced with an unescaped double quote) when converting to a single-quoted string.
2. Escaped backslashes (`\\`) are treated properly and not doubled for both single- and double-quoted strings.
@Bo98
Copy link

Bo98 commented Oct 5, 2021

Makes sense.

I take the intention here is to limit this to recognised control character sequences and quote escaping?

Because it's worth noting even things like '\z', for example, is not the same as "\z" (which equates to "z") - you still need "\\z".

@nvasilevski
Copy link

Hey folks, I believe there are more examples of false-positives apart from the '\s' like '\S' or '\d' or '\p'
The autocorrector turns these into "\\S" and "\\d" and satisfies rubocop by keeping the behaviour, however, from my point of view, in large expressions it makes the code harder to read just to follow the rule which seems a bit unreasonable
Should we consider ignoring such cases in general and avoid overcomplicating expressions from readability point of view?
Please feel free to let me know if I'm missing something, thanks!

@@ -17,7 +17,7 @@ def wrong_quotes?(src_or_node)
# 1. It contains a double quote
# 2. It contains text that would become an escape sequence with double quotes
# 3. It contains text that would become an interpolation with double quotes
!/" | (?<!\\)\\[abcefMnrtuUx0-7] | \#[@{$]/x.match?(src)
!/" | (?<!\\)\\[abcefMnrstuUx0-7] | \#[@{$]/x.match?(src)
Copy link
Contributor

Choose a reason for hiding this comment

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

\k, \A, \z, \Z are also missing

@koic koic force-pushed the fix_false_positive_for_style_string_literals branch from 86b37c0 to d06047a Compare October 20, 2021 03:09
Follow up to rubocop@a7ef72a#r57503462.

This PR fixes a false positive for `Style/StringLiterals` when using
some meta characters (e.g. `'\s'`, `'\z'`) with `EnforcedStyle: double_quotes`.
@koic koic force-pushed the fix_false_positive_for_style_string_literals branch from d06047a to 0039834 Compare October 20, 2021 03:10
@Bo98
Copy link

Bo98 commented Oct 20, 2021

Because it's worth noting even things like '\z', for example, is not the same as "\z" (which equates to "z") - you still need "\\z".

To clarify my point here: this applies to any character. Or at least I can't find any that behave differently. Am I missing something?

"\y" == '\y'
=> false
"\~" == '\~'
=> false
"\9" == '\9'
=> false

@bbatsov bbatsov merged commit 565a77d into rubocop:master Oct 27, 2021
@mvz
Copy link
Contributor

mvz commented Nov 1, 2021

With this change, 'No mutations found \o/ \o/' is now corrected to "No mutations found \\o/ \\o/". I'm not sure if that was the intention @koic?

@jdelStrother
Copy link

Yeah, I'm seeing a lot of similar problems across our codebase. I'm not entirely convinced about the rationale behind this change - we use double quoted strings everywhere, unless the string needs literal backslashes in it (where we use single-quotes to avoid the double-backslash).

To clarify my point here: this applies to any character. Or at least I can't find any that behave differently. Am I missing something?

Correct - in all your examples, none of the double-quoted strings have a backslash in their 'output', and all the single-quoted ones do.

@koic koic deleted the fix_false_positive_for_style_string_literals branch November 2, 2021 14:18
koic added a commit to koic/rubocop that referenced this pull request Nov 2, 2021
Follow up to rubocop#10166 (comment).

This PR fixes a false positive for `Style/StringLiterals` when `EnforcedStyle: double_quotes`
and using single quoted string with backslash.
It has moved a separate implementation for `Style/QuotedSymbols` to resolve rubocop#10152 to
`Style/QuotedSymbols` to prevent impact on `Style/StringLiterals`'s false positives.
@koic
Copy link
Member Author

koic commented Nov 2, 2021

Yup. I've opened #10229 to prevent the false positives.

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.

None yet

8 participants