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

[pull] master from rubocop-hq:master #80

Open
wants to merge 5,103 commits into
base: master
Choose a base branch
from
Open

Conversation

pull[bot]
Copy link

@pull pull bot commented Jun 16, 2020

See Commits and Changes for more details.


Created by pull[bot]. Want to support this open source service? Please star it : )

@pull pull bot added the ⤵️ pull label Jun 16, 2020
Earlopain and others added 29 commits September 11, 2024 15:27
… optional position arguments

Having `...` and optional positional arguments is a syntax error in Ruyb 3.0 and lower
Rails does this in their `bin/rubocop` wrapper script.
See rails/rails#52878
Fix a false positive for `Style/ArgumentForwarding` with Ruby 3.0 and optional position arguments
…ing`

Fixes #13220.

This PR fixes an incorrect autocorrect for `Style/ArgumentsForwarding`
when using only forwarded arguments in brackets.
These cases are syntax errors as well, in addition to anonymous forwarding
Fix `--auto-gen-config` when passing an absolute config path
Fix false positives for `Style/OperatorMethodCall` with named forwarding
This PR fixes the following error when using Ruby 3.4-dev (default by Prism).

```console
$ ruby -v
ruby 3.4.0dev (2024-09-13T00:28:51Z master d80a81c152) +PRISM [x86_64-darwin23]
$ bundle exec rspec './spec/rubocop/cli/options_spec.rb[1:2:5:1]' './spec/rubocop/cli/options_spec.rb[1:2:4:1]' \
'./spec/rubocop/server/rubocop_server_spec.rb[1:4:1]'
(snip)
       +/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/server/socket_reader.rb:36:in 'RuboCop::Server::SocketReader#read!':
undefined method 'string' for nil (NoMethodError)
       +
       +        Cache.stderr_path.write(stderr.string)
       +                                      ^^^^^^^
       +     from /Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/server/core.rb:90:in 'RuboCop::Server::Core#read_socket'
(snip)
       +/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/server/socket_reader.rb:27:in 'RuboCop::Server::SocketReader#read!':
uninitialized constant RuboCop::Server::SocketReader::StringIO (NameError)
       +
       +        stderr = StringIO.new
       +                 ^^^^^^^^
       +     from /Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/server/core.rb:90:in 'RuboCop::Server::Core#read_socket'
```
Fix an error when using Ruby 3.4-dev
Fixes #13235.

This PR fixes an error for `Style/IfUnlessModifier`
when multiline `if` that fits on one line and using implicit method call with hash value omission syntax.
Issues found with this:
* #13211
* #13215
* #13216
* #13219
* #13224
* #13225

I checked some repos in the rubocop org and they seem to not be affected by this change
…style_arguments_forwarding

[Fix #13220] Fix an incorrect autocorrect for `Style/ArgumentsForwarding`
Using `on_block` is expensive, since this checks each send node in a block.
Instead, use `on_send`. This is much cheaper since barely any send nodes will be named `it`.

From `bundle exec prof:slow_cops`
```
     551  (    2.9%)  RuboCop::Cop::Layout::IndentationConsistency#on_begin
     402  (    2.2%)  RuboCop::Cop::Layout::RedundantLineBreak#on_send
     358  (    1.9%)  RuboCop::Cop::Lint::ItWithoutArgumentsInBlock#on_block
(snip)
```

After:
```
(103 previous entries)
      42  (    0.2%)  RuboCop::Cop::Lint::ItWithoutArgumentsInBlock#on_send
(snip)
```

Overall, this saves about 200ms for the RuboCop repo.
Improve performance of `Lint/ItWithoutArgumentsInBlock`
…ifier

[Fix #13235] Fix an error for `Style/IfUnlessModifier`
This commit fixes `Style/CollectionCompact` to ignore `delete_if`, since `delete_if` always returns self, whereas `compact!` returns `nil` if no changes are made. Therefore, the two methods are not compatible.

Co-authored-by: Koichi ITO <koic.ito@gmail.com>
Fix false positive for `Style/CollectionCompact` when using `delete_if`
This PR makes `InternalAffairs/RedundantSourceRange` aware that
`source_range` is redundant in `add_offense(node.source_range)`.
It's a heavy gem to require for something that may not even be used:

Old:
```
$ hyperfine -w 5 -r 20 "bundle exec rubocop Rakefile"
Benchmark 1: bundle exec rubocop Rakefile
  Time (mean ± σ):      3.061 s ±  0.087 s    [User: 2.494 s, System: 0.321 s]
  Range (min … max):    2.947 s …  3.275 s    20 runs
```

New:
```
$ hyperfine -w 5 -r 20 "bundle exec rubocop Rakefile"
Benchmark 1: bundle exec rubocop Rakefile
  Time (mean ± σ):      2.801 s ±  0.064 s    [User: 2.317 s, System: 0.235 s]
  Range (min … max):    2.710 s …  2.958 s    20 runs
```

~9% faster startup time

Of course, most time is still spent requiring all the various cops of rubocop itself (there are quite a few)
`require 'language-server_protocol'` on-demand
This should not be carried over to RuboCop v2
…t_source_range

Make `InternalAffairs/RedundantSourceRange` aware of `add_offense`
This matters since there are quite a few public methods per cop class.
In total, it gets called ~57k times, or ~104 times per cop class.

`method_defined?` is faster than string checking, so move that first. It also eliminates most of the checks, Base contains 98 methods.

Current:
```
$ hyperfine -w 10 -r 25 "bundle exec rubocop Rakefile --cache=false"
Benchmark 1: bundle exec rubocop Rakefile --cache=false
  Time (mean ± σ):     932.8 ms ±   7.6 ms    [User: 828.0 ms, System: 104.0 ms]
  Range (min … max):   919.5 ms … 952.2 ms    25 runs
```

With String:
```
$ hyperfine -w 10 -r 25 "bundle exec rubocop Rakefile --cache=false"
Benchmark 1: bundle exec rubocop Rakefile --cache=false
  Time (mean ± σ):     924.1 ms ±   7.8 ms    [User: 816.3 ms, System: 107.3 ms]
  Range (min … max):   912.8 ms … 941.5 ms    25 runs
```

With String + order switched:
```
$ hyperfine -w 10 -r 25 "bundle exec rubocop Rakefile --cache=false"
Benchmark 1: bundle exec rubocop Rakefile --cache=false
  Time (mean ± σ):     920.7 ms ±   6.9 ms    [User: 814.5 ms, System: 105.6 ms]
  Range (min … max):   906.9 ms … 932.5 ms    25 runs
```

~ 1.3% faster
Optimize `Base.callbacks_needed` by reordering checks
…tion-compact

Support `filter/filter!` in `Style/CollectionCompact`
Emit a deprecation for `Team.new([Cop], config)`
This has been long-marked as deprecated but not everyone has gotten the memo:
https://github.com/search?q=%2F%3C+RuboCop%3A%3ACop%3A%3ACop%2F+lang%3Aruby+-is%3Afork&type=code

Some methods _are_ deprecated but its perfectly possible to just not use any of them.
dvandersluis and others added 30 commits November 7, 2024 13:34
Fix EmptyLinesAroundMethodBody for methods with arguments spanning multiple lines
… with assignment methods

Maybe this cop should not register offenses for methods that take arguments
but let's fix wrong autocorrect first.
This commit tweaks the following offense message for `Style/IfWithSemicolon`.

Before:

```console
$ echo 'if cond; foo else bar'arg'; baz end' | bundle exec rubocop --stdin dummy.rb -a --only Style/IfWithSemicolon
Inspecting 1 file
C

Offenses:

dummy.rb:1:1: C: [Corrected] Style/IfWithSemicolon: Do not use if cond; - use if/else instead.
if cond; foo else bararg; baz end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
====================
if cond
 foo else bararg; baz end
```

After:

```console
$ echo 'if cond; foo else bar'arg'; baz end' | bundle exec rubocop --stdin dummy.rb -a --only Style/IfWithSemicolon
Inspecting 1 file
C

Offenses:

dummy.rb:1:1: C: [Corrected] Style/IfWithSemicolon: Do not use if cond; - use a newline instead.
if cond; foo else bararg; baz end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
====================
if cond
 foo else bararg; baz end
```
[Fix #13434] False positive for `Naming/MemoizedInstanceVariableName` with assignment methods
…for-lint-redundant-splat-expansion

Recognize percent literal array arg in safe navigation for `Lint/RedundantSplatExpansion`
Fix incorrect correction in `Lint/Void` if an operator is called in a void context using a dot
This PR fixes the following incorrect autocorrect for `Style/IfWithSemicolon`
when using `return` with value in `if` with a semicolon is used.

```console
$ echo 'if cond; return value end' | bundle exec rubocop --stdin dummy.rb -a --only Style/IfWithSemicolon
Inspecting 1 file
F

Offenses:

dummy.rb:1:1: C: [Corrected] Style/IfWithSemicolon: Do not use if cond; - use a ternary operator instead.
if cond; return value end
^^^^^^^^^^^^^^^^^^^^^^^^^
dummy.rb:1:15: F: Lint/Syntax: unexpected token tIDENTIFIER
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
cond ? return value : nil
              ^^^^^

1 file inspected, 2 offenses detected, 1 offense corrected
====================
cond ? return value : nil
```
…style_if_with_semicolon

Fix an incorrect autocorrect for `Style/IfWithSemicolon`
This PR fixes an incorrect autocorrect for `Style/NestedTernaryOperator`
when ternary operators are nested and the inner condition is parenthesized.

Before:

Autocorrect makes invalid syntax:

```console
$ echo 'foo ? (bar && baz) ? qux : quux : corge' | bundle exec rubocop --stdin dummy.rb -a --only Style/NestedTernaryOperator
Inspecting 1 file
F

Offenses:

dummy.rb:1:7: C: [Corrected] Style/NestedTernaryOperator: Ternary operators must not be nested. Prefer if or else constructs instead.
foo ? (bar && baz) ? qux : quux : corge
      ^^^^^^^^^^^^^^^^^^^^^^^^^
dummy.rb:2:11: F: Lint/Syntax: unexpected token tRPAREN
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
bar && baz) ? qux : quux
          ^

1 file inspected, 2 offenses detected, 1 offense corrected
====================
if foo
bar && baz) ? qux : quux
else
corge
end
```

After:

Autocorrect makes valid syntax:

```console
$ echo 'foo ? (bar && baz) ? qux : quux : corge' | bundle exec rubocop --stdin dummy.rb -a --only Style/NestedTernaryOperator
Inspecting 1 file
C

Offenses:

dummy.rb:1:7: C: [Corrected] Style/NestedTernaryOperator: Ternary operators must not be nested. Prefer if or else constructs instead.
foo ? (bar && baz) ? qux : quux : corge
      ^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
====================
if foo
(bar && baz) ? qux : quux
else
corge
end
```
…le_nested_ternary_operator

Fix an incorrect autocorrect for `Style/NestedTernaryOperator`
This PR fixes an incorrect autocorrect for `Style/OneLineConditional`
when the else branch of a ternary operator has multiple expressions.

Before:

The autocorrect makes incorrect code that is not compatible.

```console
$ echo 'if cond; foo; else bar; baz; end' | bundle exec rubocop --stdin dummy.rb -a --only Style/OneLineConditional
Inspecting 1 file
C

Offenses:

dummy.rb:1:1: C: [Corrected] Style/OneLineConditional: Favor the ternary operator (?:) or multi-line constructs over single-line if/then/else/end constructs.
if cond; foo; else bar; baz; end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
====================
cond ? foo : bar; baz
```

`cond ? foo : bar; baz` is equivalent to `(cond ? foo : bar); baz`, not `cond ? foo : (bar; baz)`.

```console
$ ruby-parse -e '(cond ? foo : bar); baz'
(begin
  (begin
    (if
      (send nil :cond)
      (send nil :foo)
      (send nil :bar)))
  (send nil :baz))
```

After:

The autocorrect makes compatible code.

```console
$ echo 'if cond; foo; else bar; baz; end' | bundle exec rubocop --stdin dummy.rb -a --only Style/OneLineConditional
Inspecting 1 file
C

Offenses:

dummy.rb:1:1: C: [Corrected] Style/OneLineConditional: Favor the ternary operator (?:) or multi-line constructs over single-line if/then/else/end constructs.
if cond; foo; else bar; baz; end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
====================
if cond
  foo
else
  bar; baz
end
```

Multiple expressions in the else branch using semicolons are resolved by the `Style/Semicolon` cop.
…le_one_line_conditional

Fix an incorrect autocorrect for `Style/OneLineConditional`
This PR fixes an incorrect autocorrect for `Style/IfWithSemicolon`
when the then body contains an arithmetic operator method call.

## Before

Autocorrect makes invalid syntax:

```console
$ echo 'if cond;do_something - arg end' | bundle exec rubocop --stdin dummy.rb -a --only Style/IfWithSemicolon
Inspecting 1 file
F

Offenses:

dummy.rb:1:1: C: [Corrected] Style/IfWithSemicolon: Do not use if cond; - use a ternary operator instead.
if cond;do_something - arg end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
dummy.rb:1:21: F: Lint/Syntax: -' interpreted as argument prefix
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops`)
cond ? do_something -(arg) : nil
                    ^
dummy.rb:1:28: F: Lint/Syntax: unexpected token tCOLON
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
cond ? do_something -(arg) : nil
                           ^

1 file inspected, 3 offenses detected, 1 offense corrected
====================
cond ? do_something -(arg) : nil
```

## After

Autocorrect makes valid syntax:

```console
$ echo 'if cond;do_something - arg end' | bundle exec rubocop --stdin dummy.rb -a --only Style/IfWithSemicolon
Inspecting 1 file
C

Offenses:

dummy.rb:1:1: C: [Corrected] Style/IfWithSemicolon: Do not use if cond; - use a ternary operator instead.
if cond;do_something - arg end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
====================
cond ? do_something - arg : nil
```
…style_if_with_semicolon_cop

Fix an incorrect autocorrect for `Style/IfWithSemicolon`
In `rubocop-ast`, main gem tests should not fail when a warning is introduced
Make strict warnings opt-in, in CI as well as locally
This PR fixes a false positive for `Layout/EmptyLineAfterGuardClause`
when using a guard clause outside oneliner block.

It avoids the following infinit loop error:

```console
$ cat example.rb
return if condition; foo do
  bar
end

$ bundle exec rubocop --only Layout/EmptyLineAfterGuardClause,Layout/EmptyLinesAroundBlockBody -a
Inspecting 1 file
C

Offenses:

example.rb:1:1: C: [Corrected] Layout/EmptyLineAfterGuardClause: Add empty line after guard clause.
return if condition; foo do
^^^^^^^^^^^^^^^^^^^
example.rb:2:1: C: [Corrected] Layout/EmptyLinesAroundBlockBody: Extra empty line detected at block body beginning.

0 files inspected, 2 offenses detected, 2 offenses corrected
Infinite loop detected in /Users/koic/src/github.com/koic/rubocop-issues/block/example.rb and
caused by Layout/EmptyLineAfterGuardClause -> Layout/EmptyLinesAroundBlockBody
Hint: Please update to the latest RuboCop version if not already in use, and report a bug if the issue still occurs on this version.
Please check the latest version at https://rubygems.org/gems/rubocop.
```
…mpty_line_after_guard_clause

Fix a false positive for `Layout/EmptyLineAfterGuardClause`
…dition-tests

Add additional tests for compound clauses for `Lint/AssignmentInCondition`
Enforces the use of `node.operator_keyword?` instead of `node.and_type? || node.or_type?`.

```ruby
# bad
node.and_type? || node.or_type?
node.or_type? || node.and_type?

# good
node.operator_keyword?
```
This PR fixes false positives for `Lint/InterpolationCheck` when using invalid syntax in interpolation.

```console
$ cat example.rb
'#{%<expression>s}'

$ bundle exec rubocop -A /tmp/a/a.rb --only Lint/InterpolationCheck
Inspecting 1 file
F

Offenses:

/tmp/a/a.rb:1:1: W: [Corrected] Lint/InterpolationCheck: Interpolation in single quoted string detected. Use double quoted strings if you need interpolation.
'#{%<expression>s}'
^^^^^^^^^^^^^^^^^^^
/tmp/a/a.rb:1:17: F: Lint/Syntax: unexpected token tIDENTIFIER
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
"#{%<expression>s}"
                ^

1 file inspected, 2 offenses detected, 1 offense corrected
````

A string interpolation should not be expected in a state where a syntax error occurs.
…_keyword_cop

Add new `InternalAffairs/OperatorKeyword` cop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.