Skip to content

Conversation

@koic
Copy link
Member

@koic koic commented Nov 14, 2025

This change corrects some cops that previously assumed Layout/LineLength was always enabled.
They now respect the cop's actual enablement state, matching the expected behavior in the updated tests.

The update removes hard-coded max_line_length lookups, delegates to shared logic, and ensures consistent behavior across cops.


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.

This change corrects some cops that previously assumed
`Layout/LineLength` was always enabled.
They now respect the cop's actual enablement state,
matching the expected behavior in the updated tests.

The update removes hard-coded `max_line_length` lookups,
delegates to shared logic, and ensures consistent behavior across cops.
@koic koic marked this pull request as ready for review November 14, 2025 06:42
@bbatsov bbatsov merged commit 2eb271c into rubocop:master Nov 14, 2025
22 checks passed
@bbatsov
Copy link
Collaborator

bbatsov commented Nov 14, 2025

Great change, that we should have done much earlier. Thanks!

@koic koic deleted the fix_incorrect_behavior_when_layout_line_length_is_disabled branch November 14, 2025 06:56
viralpraxis added a commit to viralpraxis/rubocop that referenced this pull request Dec 19, 2025
Follow-up to rubocop#14658

If `Layout/LineLength` is disabled but forced (via `--only`), it throws
the same error as in rubocop#14719.

Looks like the easies way to fix that is to override `LineLength#max_line_length`
method.

The error without the patch:

```shell
Failures:

  1) RuboCop::CLI options --only when one cop is given when specifying disabled `Layout/LineLength` cop enables the given cop
     Got 2 failures from failure aggregation block.
     # ./spec/support/cli_spec_behavior.rb:26:in 'block (2 levels) in <top (required)>'
     # ./lib/rubocop/rspec/shared_contexts.rb:44:in 'block (2 levels) in <top (required)>'
     # ./lib/rubocop/rspec/shared_contexts.rb:29:in 'block (4 levels) in <top (required)>'
     # ./lib/rubocop/rspec/shared_contexts.rb:29:in 'Dir.chdir'
     # ./lib/rubocop/rspec/shared_contexts.rb:29:in 'block (3 levels) in <top (required)>'
     # ./lib/rubocop/rspec/shared_contexts.rb:7:in 'block (2 levels) in <top (required)>'

     1.1) Failure/Error:
            expect(cli.run(['--format', 'simple',
                            '--only', 'Layout/LineLength',
                            'example.rb'])).to be_zero

            expected `1.zero?` to be truthy, got false
          # ./spec/rubocop/cli/options_spec.rb:735:in 'block (5 levels) in <top (required)>'

     1.2) Failure/Error: expect($stderr.string).to eq('')

            expected: ""
                 got: "An error occurred while Layout/LineLength cop was inspecting /tmp/d20251219-51392-5a0566/work/exampl...Parser 3.3.10.0, rubocop-ast 1.48.0, analyzing as Ruby 2.7, running on ruby 3.4.7) [x86_64-linux]\n"

            (compared using ==)

            Diff:
            @@ -0,0 +1,11 @@
            +An error occurred while Layout/LineLength cop was inspecting /tmp/d20251219-51392-5a0566/work/example.rb.
            +To see the complete backtrace run rubocop -d.
            +
            +1 error occurred:
            +An error occurred while Layout/LineLength cop was inspecting /tmp/d20251219-51392-5a0566/work/example.rb.
            +Errors are usually caused by RuboCop bugs.
            +Please, update to the latest RuboCop version if not already in use, and report a bug if the issue still occurs on this version.
            +https://github.com/rubocop/rubocop/issues
            +
            +Mention the following information in the issue report:
            +1.82.0 (using Parser 3.3.10.0, rubocop-ast 1.48.0, analyzing as Ruby 2.7, running on ruby 3.4.7) [x86_64-linux]

          # ./spec/rubocop/cli/options_spec.rb:736:in 'block (5 levels) in <top (required)>'

Finished in 0.10147 seconds (files took 0.77374 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/rubocop/cli/options_spec.rb:723 # RuboCop::CLI options --only when one cop is given when specifying disabled `Layout/LineLength` cop enables the given cop

Randomized with seed 44805
```
viralpraxis added a commit to viralpraxis/rubocop that referenced this pull request Dec 21, 2025
Follow-up to rubocop#14658

If `Layout/LineLength` is disabled but forced (via `--only`), it throws
the same error as in rubocop#14719.

Looks like the easies way to fix that is to override `LineLength#max_line_length`
method.

The error without the patch:

```shell
Failures:

  1) RuboCop::CLI options --only when one cop is given when specifying disabled `Layout/LineLength` cop enables the given cop
     Got 2 failures from failure aggregation block.
     # ./spec/support/cli_spec_behavior.rb:26:in 'block (2 levels) in <top (required)>'
     # ./lib/rubocop/rspec/shared_contexts.rb:44:in 'block (2 levels) in <top (required)>'
     # ./lib/rubocop/rspec/shared_contexts.rb:29:in 'block (4 levels) in <top (required)>'
     # ./lib/rubocop/rspec/shared_contexts.rb:29:in 'Dir.chdir'
     # ./lib/rubocop/rspec/shared_contexts.rb:29:in 'block (3 levels) in <top (required)>'
     # ./lib/rubocop/rspec/shared_contexts.rb:7:in 'block (2 levels) in <top (required)>'

     1.1) Failure/Error:
            expect(cli.run(['--format', 'simple',
                            '--only', 'Layout/LineLength',
                            'example.rb'])).to be_zero

            expected `1.zero?` to be truthy, got false
          # ./spec/rubocop/cli/options_spec.rb:735:in 'block (5 levels) in <top (required)>'

     1.2) Failure/Error: expect($stderr.string).to eq('')

            expected: ""
                 got: "An error occurred while Layout/LineLength cop was inspecting /tmp/d20251219-51392-5a0566/work/exampl...Parser 3.3.10.0, rubocop-ast 1.48.0, analyzing as Ruby 2.7, running on ruby 3.4.7) [x86_64-linux]\n"

            (compared using ==)

            Diff:
            @@ -0,0 +1,11 @@
            +An error occurred while Layout/LineLength cop was inspecting /tmp/d20251219-51392-5a0566/work/example.rb.
            +To see the complete backtrace run rubocop -d.
            +
            +1 error occurred:
            +An error occurred while Layout/LineLength cop was inspecting /tmp/d20251219-51392-5a0566/work/example.rb.
            +Errors are usually caused by RuboCop bugs.
            +Please, update to the latest RuboCop version if not already in use, and report a bug if the issue still occurs on this version.
            +https://github.com/rubocop/rubocop/issues
            +
            +Mention the following information in the issue report:
            +1.82.0 (using Parser 3.3.10.0, rubocop-ast 1.48.0, analyzing as Ruby 2.7, running on ruby 3.4.7) [x86_64-linux]

          # ./spec/rubocop/cli/options_spec.rb:736:in 'block (5 levels) in <top (required)>'

Finished in 0.10147 seconds (files took 0.77374 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/rubocop/cli/options_spec.rb:723 # RuboCop::CLI options --only when one cop is given when specifying disabled `Layout/LineLength` cop enables the given cop

Randomized with seed 44805
```
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.

2 participants