Rubocop: Style/RegexpLiteral complains about its EnforcedStyle instead of AllowInnerSlashes

Created on 12 Jan 2017  路  4Comments  路  Source: rubocop-hq/rubocop

If you write a regexp containing inner slashes, RuboCop will keeps complaining about using %r{} even if you asks for slashes as enforced style in your .rubocop.yml file.


Expected behavior

With EnforcedStyle: slashes or EnforcedStyle: mixed RuboCop should not display a warning urging you to use %r{} without more explanation. The message should be more explicit like: 芦 you should use %r{} when your regexp contains inner slashes (preference AllowInnerSlashes) [Style/RegexpLitteral] 禄

Actual behavior

RuboCop warns me with a weird 芦 Use %r{} around regular expression [Style/RegexpLitteral] 禄

Steps to reproduce the problem

Write the following lines in a new files :

tutu = 'To/to lapin'

tutu.gsub(/o\/t?/, 'a')

puts tutu

In the same folder, create a .rubocop.yml file containing:

Style/RegexpLiteral:
  EnforcedStyle: slashes

RuboCop version

$ rubocop -V
0.46.0 (using Parser 2.3.2.0, running on ruby 2.1.2 x86_64-linux)
bug

Most helpful comment

@milouse Actually, you need this configuration:

Style/RegexpLiteral:
  EnforcedStyle: slashes
  AllowInnerSlashes: true

because with the default AllowInnerSlashes: false, the meaning of EnforcedStyle: slashes is that slashes are only enforced as long as they don't make it necessary to escape inner slashes.

See the last example in the relevant section of the manual.

All 4 comments

Agreed. This should be improved.

If we agree on the message to be displayed, I can try to make a PR to help you :)

I'll have to review the behavior of the cop first. Honestly I don't remember its different behaviors.

@milouse Actually, you need this configuration:

Style/RegexpLiteral:
  EnforcedStyle: slashes
  AllowInnerSlashes: true

because with the default AllowInnerSlashes: false, the meaning of EnforcedStyle: slashes is that slashes are only enforced as long as they don't make it necessary to escape inner slashes.

See the last example in the relevant section of the manual.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

david942j picture david942j  路  3Comments

mikegee picture mikegee  路  3Comments

bbugh picture bbugh  路  3Comments

millisami picture millisami  路  3Comments

herwinw picture herwinw  路  3Comments