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

Lint/UnneededDisable of Metrics/LineLength that isn't unneeded #4001

Closed
jfelchner opened this issue Feb 2, 2017 · 3 comments
Closed

Lint/UnneededDisable of Metrics/LineLength that isn't unneeded #4001

jfelchner opened this issue Feb 2, 2017 · 3 comments
Labels

Comments

@jfelchner
Copy link
Contributor

This line (among many others) is throwing an error for me:

  LARGE_DATA_STRING_PATTERN = %r{\A([A-Za-z0-9\+\/#]*\={0,2})#([A-Za-z0-9\+\/#]*\={0,2})#([A-Za-z0-9\+\/#]*\={0,2})\z} # rubocop:disable Metrics/LineLength

My full LineLength config is as follows:

Metrics/LineLength:
  Enabled:                       true
  Max:                           90
  AllowHeredoc:                  true
  AllowURI:                      true
  IgnoreCopDirectives:           true
  URISchemes:
    - 'http'
    - 'https'

Expected behavior

No error for Lint/UnneededDisable

Actual behavior

An error for Lint/UnneededDisable

RuboCop version

$ rubocop -V
0.47.1 (using Parser 2.3.3.1, running on ruby 2.4.0 x86_64-darwin16)
@mikegee
Copy link
Contributor

mikegee commented Feb 2, 2017

I confirmed the report. Also, changing IgnoreCopDirectives to false gets the expected behavior (no offense from Lint/UnneededDisable). I suspect there's an unexplored edge case.

Sidenote: It would be nice if IgnoreCopDirectives were flipped to positive phrasing like IncludeCopDirectives, to make this easier to reason about.

@bbatsov bbatsov added the bug label Feb 3, 2017
@wkurniawan07
Copy link
Contributor

As it turns out, it's really easy to reproduce this. Any line containing both non-directive # and directive # has the potential to run into this bug.

I'd be happy to submit a fix for this.

@wkurniawan07
Copy link
Contributor

@bbatsov

The following line:

    COMMENT_DIRECTIVE_REGEXP = Regexp.new(
      ('\A# rubocop : ((?:dis|en)able)\b ' + COPS_PATTERN).gsub(' ', '\s*')
    )

Is it necessary for the regexp to have the \A assertion? It is one of the culprits behind this bug.

wkurniawan07 added a commit to wkurniawan07/rubocop that referenced this issue Mar 2, 2017
Metrics/LineLength had a bug when configured with “ignoreCopDirectives” set to true AND checking a line with both directive # and non-directive # present.

For example:
```rb
aaa … aaa # bbb … bbb # rubocop:disable CopName
```
where (aaa … aaa # bbb … bbb) exceeds the specified line length.
The cop would correctly detect it as a violation, but would return the extraneous characters AND the directive in the error message.

```rb
LARGE_DATA_STRING_PATTERN = %r{\A([A-Za-z0-9\+\/#]…)} # rubocop:disable CopName
```
The above line has # as part of regexp and the line is longer than the permissible line length before the directive.
The cop did not detect it as a violation.
@bbatsov bbatsov closed this as completed in 670bc91 Mar 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants