Fixes rubocop#6699.
This PR fixes infinite loop for `Layout/IndentationWidth` and
`Layout/IndentationConsistency` when bad modifier indentation
before good method definition.
```ruby
# exmaple.rb
class Foo
private
def foo
end
end
```
First, auto-corrected by `Layout/IndentationConsistency`.
```console
% rubocop -v
0.65.0
% rubocop -a --only Layout/IndentationConsistency example.rb
Inspecting 1 file
C
Offenses:
example.rb:4:3: C: [Corrected] Layout/IndentationConsistency: Inconsistent indentation detected.
def foo ...
^^^^^^^
1 file inspected, 1 offense detected, 1 offense corrected
```
```diff
% g diff
diff --git a/example.rb b/example.rb
index 23d3556..974f101 100644
--- a/example.rb
+++ b/example.rb
@@ -1,6 +1,6 @@
class Foo
private
- def foo
- end
+ def foo
+ end
end
```
Next, auto-corrected by `Layout/IndentationWidth`.
```console
% rubocop -a --only Layout/IndentationWidth example.rb
Inspecting 1 file
C
Offenses:
example.rb:4:1: C: [Corrected] Layout/IndentationWidth: Use 2 (not 6)
spaces for indentation.
def foo
^^^^^^
1 file inspected, 1 offense detected, 1 offense corrected
```
This will return to the original code.
```diff
% g diff
diff --git a/example.rb b/example.rb
index 974f101..23d3556 100644
--- a/example.rb
+++ b/example.rb
@@ -1,6 +1,6 @@
class Foo
private
- def foo
- end
+ def foo
+ end
end
```
That caused the infinite loop in `Layout/IndentationConsistency`
and `Layout/IndentationWidth`.
With this PR, `Layout/indentationWidth` cop makes aware of
the bad modifier indentation before good method definition.
```console
% rubocop -a --only Layout/IndentationWidth example.rb
Inspecting 1 file
C
Offenses:
example.rb:2:1: C: [Corrected] Layout/IndentationWidth: Use 2 (not 6)
spaces for indentation.
private
^^^^^^
1 file inspected, 1 offense detected, 1 offense corrected
```
```diff
diff --git a/example.rb b/example.rb
index 23d3556..07525ec 100644
--- a/example.rb
+++ b/example.rb
@@ -1,5 +1,5 @@
class Foo
- private
+ private
def foo
end
```
This PR fixes the above false negative. That would be an auto-correct expected.