From b2fe007101dab711dd75ad4927bb5813e4411cf5 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 2 Nov 2021 22:25:51 +0900 Subject: [PATCH] Fix a false positive for `Style/StringLiterals` Follow up to https://github.com/rubocop/rubocop/pull/10166#issuecomment-956190960. 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 #10152 to `Style/QuotedSymbols` to prevent impact on `Style/StringLiterals`'s false positives. --- ...fix_a_false_positive_for_style_string_literals.md | 1 + lib/rubocop/cop/mixin/string_literals_help.rb | 6 +----- lib/rubocop/cop/style/quoted_symbols.rb | 12 +++++++++++- spec/rubocop/cop/style/string_literals_spec.rb | 8 ++++++++ 4 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 changelog/fix_a_false_positive_for_style_string_literals.md diff --git a/changelog/fix_a_false_positive_for_style_string_literals.md b/changelog/fix_a_false_positive_for_style_string_literals.md new file mode 100644 index 00000000000..5d634e80531 --- /dev/null +++ b/changelog/fix_a_false_positive_for_style_string_literals.md @@ -0,0 +1 @@ +* [#10229](https://github.com/rubocop/rubocop/pull/10229): Fix a false positive for `Style/StringLiterals` when `EnforcedStyle: double_quotes` and using single quoted string with backslash. ([@koic][]) diff --git a/lib/rubocop/cop/mixin/string_literals_help.rb b/lib/rubocop/cop/mixin/string_literals_help.rb index fba7dceaad0..c43c08dc176 100644 --- a/lib/rubocop/cop/mixin/string_literals_help.rb +++ b/lib/rubocop/cop/mixin/string_literals_help.rb @@ -13,11 +13,7 @@ def wrong_quotes?(src_or_node) if style == :single_quotes !double_quotes_required?(src) else - # The string needs single quotes if: - # 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 - !/" | (?