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 #12873] Fix an error for Metrics/BlockLength #12874

Merged
merged 1 commit into from Apr 29, 2024

Conversation

koic
Copy link
Member

@koic koic commented Apr 29, 2024

Fixes #12873.

This PR fixes an error for Metrics/BlockLength when the CountAsOne config is invalid.

Before

Without the -d option, the cause is unclear:

$ bundle exec rubocop --only Metrics/BlockLength
Inspecting 1 file
An error occurred while Metrics/BlockLength cop was inspecting /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb:2:2.
To see the complete backtrace run rubocop -d.
.

1 file inspected, no offenses detected

1 error occurred:
An error occurred while Metrics/BlockLength cop was inspecting /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb:2:2.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop/rubocop/issues

Mention the following information in the issue report:
1.63.4 (using Parser 3.3.0.5, rubocop-ast 1.31.2, running on ruby 3.3.0) [x86_64-darwin22]

When the -d option is used, hint appears hidden within the backtrace:

$ bundle exec rubocop --only Metrics/BlockLength -d
For /Users/koic/src/github.com/koic/rubocop-issues/12873: configuration from /Users/koic/src/github.com/koic/rubocop-issues/12873/.rubocop.yml
Default configuration from /Users/koic/src/github.com/rubocop/rubocop/config/default.yml
AllCops/Exclude configuration from /Users/koic/src/github.com/koic/rubocop-issues/.rubocop.yml
configuration from /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/rubocop-minitest-0.35.0/config/default.yml
configuration from /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/rubocop-minitest-0.35.0/config/default.yml
configuration from /Users/koic/src/github.com/rubocop/rubocop-performance/config/default.yml
configuration from /Users/koic/src/github.com/rubocop/rubocop-performance/config/default.yml
configuration from /Users/koic/src/github.com/rubocop/rubocop-rails/config/default.yml
configuration from /Users/koic/src/github.com/rubocop/rubocop-rails/config/default.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb
An error occurred while Metrics/BlockLength cop was inspecting /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb:2:2.
Unknown foldable type: :config. Valid foldable types are: array, hash, heredoc, send, csend.
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/metrics/utils/code_length_calculator.rb:54:in `block in build_foldable_checks'

After

Even without the -d option, only the hint is displayed:

$ bundle exec rubocop --only Metrics/BlockLength
Inspecting 1 file
Unknown foldable type: :config. Valid foldable types are: array, hash, heredoc, send, csend.
(from file: /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb:2:2)
.

1 file inspected, no offenses detected

This behavior is the same as when the Notice config in Style/Copyright cop is not valid. This clarifies what needs to be addressed first to achieve the expected behavior.


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.

Fixes rubocop#12873.

This PR fixes an error for `Metrics/BlockLength` when the `CountAsOne` config is invalid.

## Before

Without the `-d` option, the cause is unclear:

```console
$ bundle exec rubocop --only Metrics/BlockLength
Inspecting 1 file
An error occurred while Metrics/BlockLength cop was inspecting /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb:2:2.
To see the complete backtrace run rubocop -d.
.

1 file inspected, no offenses detected

1 error occurred:
An error occurred while Metrics/BlockLength cop was inspecting /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb:2:2.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop/rubocop/issues

Mention the following information in the issue report:
1.63.4 (using Parser 3.3.0.5, rubocop-ast 1.31.2, running on ruby 3.3.0) [x86_64-darwin22]
```

When the `-d` option is used, hint appears hidden within the backtrace:

```console
$ bundle exec rubocop --only Metrics/BlockLength -d
For /Users/koic/src/github.com/koic/rubocop-issues/12873: configuration from /Users/koic/src/github.com/koic/rubocop-issues/12873/.rubocop.yml
Default configuration from /Users/koic/src/github.com/rubocop/rubocop/config/default.yml
AllCops/Exclude configuration from /Users/koic/src/github.com/koic/rubocop-issues/.rubocop.yml
configuration from /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/rubocop-minitest-0.35.0/config/default.yml
configuration from /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/rubocop-minitest-0.35.0/config/default.yml
configuration from /Users/koic/src/github.com/rubocop/rubocop-performance/config/default.yml
configuration from /Users/koic/src/github.com/rubocop/rubocop-performance/config/default.yml
configuration from /Users/koic/src/github.com/rubocop/rubocop-rails/config/default.yml
configuration from /Users/koic/src/github.com/rubocop/rubocop-rails/config/default.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb
An error occurred while Metrics/BlockLength cop was inspecting /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb:2:2.
Unknown foldable type: :config. Valid foldable types are: array, hash, heredoc, send, csend.
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/metrics/utils/code_length_calculator.rb:54:in `block in build_foldable_checks'
```

## After

Even without the `-d` option, only the hint is displayed:

```console
$ bundle exec rubocop --only Metrics/BlockLength
Inspecting 1 file
Unknown foldable type: :config. Valid foldable types are: array, hash, heredoc, send, csend.
(from file: /Users/koic/src/github.com/koic/rubocop-issues/12873/example.rb:2:2)
.

1 file inspected, no offenses detected
```

This behavior is the same as when the `Notice` config in `Style/Copyright` cop is not valid.
This clarifies what needs to be addressed first to achieve the expected behavior.
@koic koic force-pushed the fix_an_error_for_metrics_block_length branch from 3b4f259 to eb26e9d Compare April 29, 2024 09:32
@koic koic merged commit 3b3a0cd into rubocop:master Apr 29, 2024
33 checks passed
@koic koic deleted the fix_an_error_for_metrics_block_length branch April 29, 2024 17:02
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.

class Foo; bar do will crash Metrics/BlockLength
1 participant