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

Style/RedundantStringEscape cop is crashing on some rare inputs #11089

Closed
denis-kos opened this issue Oct 20, 2022 · 1 comment · Fixed by #11090
Closed

Style/RedundantStringEscape cop is crashing on some rare inputs #11089

denis-kos opened this issue Oct 20, 2022 · 1 comment · Fixed by #11090
Labels

Comments

@denis-kos
Copy link

The code as follows cause cop to crash:

["one", "two"] * ?.
# => "one.two"

Expected behavior

The cop should not crash. Rubocop probably can suggest to rewrite the code like ["one", "two"].join("."), but not crash.

Actual behavior

An error occurred while Style/RedundantStringEscape cop was inspecting ...

RuboCop version

$ [bundle exec] rubocop -V
1.37.0 (using Parser 3.1.2.1, rubocop-ast 1.22.0, running on ruby 3.1.1) [x86_64-linux]
  - rubocop-rails 2.15.2
  - rubocop-rspec 2.12.1
@bbatsov
Copy link
Collaborator

bbatsov commented Oct 20, 2022

@owst Please, take a look.

@bbatsov bbatsov added the bug label Oct 20, 2022
ydah added a commit to ydah/rubocop that referenced this issue Oct 21, 2022
koic added a commit that referenced this issue Oct 21, 2022
[Fix #11089] Fix an error for `Style/RedundantStringEscape` when using character literals (e.g. `?a`)
koic added a commit to koic/rubocop that referenced this issue Oct 21, 2022
This PR fixes the following error for `Style/RedundantStringEscape` cop
when using `?\n` string character literal.

```console
% cat example.rb
?\n

% bundle exec rubocop --only Style/RedundantStringEscape -d
(snip)

An error occurred while Style/RedundantStringEscape cop was inspecting /Users/koic/src/github.com/koic/rubocop-issues/11090/example.rb:1:0.
undefined method `source' for nil:NilClass

          delimiters = [node.loc.begin.source[-1], node.loc.end.source[0]]
                                                               ^^^^^^^
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/style/redundant_string_escape.rb:162:in `delimiter?'
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/style/redundant_string_escape.rb:110:in `single_quoted?'
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/style/redundant_string_escape.rb:103:in `interpolation_not_enabled?'
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/style/redundant_string_escape.rb:85:in `allowed_escape?'
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/style/redundant_string_escape.rb:51:in `block in on_str'
```

Similar case to rubocop#11089, but not resolved by rubocop#11090.
It also removes a bit of unnecessary implementation logic I found.
bbatsov pushed a commit that referenced this issue Oct 21, 2022
This PR fixes the following error for `Style/RedundantStringEscape` cop
when using `?\n` string character literal.

```console
% cat example.rb
?\n

% bundle exec rubocop --only Style/RedundantStringEscape -d
(snip)

An error occurred while Style/RedundantStringEscape cop was inspecting /Users/koic/src/github.com/koic/rubocop-issues/11090/example.rb:1:0.
undefined method `source' for nil:NilClass

          delimiters = [node.loc.begin.source[-1], node.loc.end.source[0]]
                                                               ^^^^^^^
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/style/redundant_string_escape.rb:162:in `delimiter?'
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/style/redundant_string_escape.rb:110:in `single_quoted?'
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/style/redundant_string_escape.rb:103:in `interpolation_not_enabled?'
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/style/redundant_string_escape.rb:85:in `allowed_escape?'
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/style/redundant_string_escape.rb:51:in `block in on_str'
```

Similar case to #11089, but not resolved by #11090.
It also removes a bit of unnecessary implementation logic I found.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants