r7kamura / rubocop_todo_corrector

Auto-correct offenses defined in .rubocop_todo.yml.
MIT License
5 stars 2 forks source link

Add Safety section if `@safety` YARD tag is written in Cop class #14

Closed r7kamura closed 2 years ago

r7kamura commented 2 years ago

Auto-correct Lint/AmbiguousRange

Summary

Auto-corrected Lint/AmbiguousRange.

Details

Lint/AmbiguousRange

This cop checks for ambiguous ranges.

Ranges have quite low precedence, which leads to unexpected behavior when using a range with other operators. This cop avoids that by making ranges explicit by requiring parenthesis around complex range boundaries (anything that is not a literal: numerics, strings, symbols, etc.).

This cop can be configured with RequireParenthesesForMethodChains in order to specify whether method chains (including self.foo) should be wrapped in parens by this cop.

NOTE: Regardless of this configuration, if a method receiver is a basic literal value, it will be wrapped in order to prevent the ambiguity of 1..2.to_a.

Safety

The cop auto-corrects by wrapping the entire boundary in parentheses, which makes the outcome more explicit but is possible to not be the intention of the programmer. For this reason, this cop's auto-correct is unsafe (it will not change the behavior of the code, but will not necessarily match the intent of the program).

Examples

# bad
x || 1..2
(x || 1..2)
1..2.to_a

# good, unambiguous
1..2
'a'..'z'
:bar..:baz
MyClass::MIN..MyClass::MAX
@min..@max
a..b
-a..b

# good, ambiguity removed
x || (1..2)
(x || 1)..2
(x || 1)..(y || 2)
(1..2).to_a

RequireParenthesesForMethodChains: false (default)

# good
a.foo..b.bar
(a.foo)..(b.bar)

RequireParenthesesForMethodChains: true

# bad
a.foo..b.bar

# good
(a.foo)..(b.bar)

Note

This pull request was generated by rubocop_todo_corrector.

r7kamura commented 2 years ago

The comment above is an example output from:

bundle exec rubocop_todo_corrector describe --cop-name Lint/AmbiguousRange