Skip to content

Commit f91df3e

Browse files
fatkodimabbatsov
authored andcommitted
Add new Lint/EmptyConditionalBody cop
1 parent 7048b51 commit f91df3e

File tree

7 files changed

+254
-0
lines changed

7 files changed

+254
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* [#7425](https://github.com/rubocop-hq/rubocop/pull/7425): Add new `Lint/TopLevelReturnWithArgument` cop. ([@iamravitejag][])
2121
* [#8417](https://github.com/rubocop-hq/rubocop/pull/8417): Add new `Style/GlobalStdStream` cop. ([@fatkodima][])
2222
* [#7949](https://github.com/rubocop-hq/rubocop/issues/7949): Add new `Style/SingleArgumentDig` cop. ([@volfgox][])
23+
* [#8341](https://github.com/rubocop-hq/rubocop/pull/8341): Add new `Lint/EmptyConditionalBody` cop. ([@fatkodima][])
2324

2425
### Bug fixes
2526

config/default.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,12 @@ Lint/ElseLayout:
14421442
Enabled: true
14431443
VersionAdded: '0.17'
14441444

1445+
Lint/EmptyConditionalBody:
1446+
Description: 'This cop checks for the presence of `if`, `elsif` and `unless` branches without a body.'
1447+
Enabled: 'pending'
1448+
AllowComments: true
1449+
VersionAdded: '0.89'
1450+
14451451
Lint/EmptyEnsure:
14461452
Description: 'Checks for empty ensure block.'
14471453
Enabled: true

docs/modules/ROOT/pages/cops.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ In the following section you find all available cops:
200200
* xref:cops_lint.adoc#lintduplicaterescueexception[Lint/DuplicateRescueException]
201201
* xref:cops_lint.adoc#linteachwithobjectargument[Lint/EachWithObjectArgument]
202202
* xref:cops_lint.adoc#lintelselayout[Lint/ElseLayout]
203+
* xref:cops_lint.adoc#lintemptyconditionalbody[Lint/EmptyConditionalBody]
203204
* xref:cops_lint.adoc#lintemptyensure[Lint/EmptyEnsure]
204205
* xref:cops_lint.adoc#lintemptyexpression[Lint/EmptyExpression]
205206
* xref:cops_lint.adoc#lintemptyinterpolation[Lint/EmptyInterpolation]

docs/modules/ROOT/pages/cops_lint.adoc

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,90 @@ else
893893
end
894894
----
895895

896+
== Lint/EmptyConditionalBody
897+
898+
|===
899+
| Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
900+
901+
| Pending
902+
| Yes
903+
| No
904+
| 0.89
905+
| -
906+
|===
907+
908+
This cop checks for the presence of `if`, `elsif` and `unless` branches without a body.
909+
910+
=== Examples
911+
912+
[source,ruby]
913+
----
914+
# bad
915+
if condition
916+
end
917+
918+
# bad
919+
unless condition
920+
end
921+
922+
# bad
923+
if condition
924+
do_something
925+
elsif other_condition
926+
end
927+
928+
# good
929+
if condition
930+
do_something
931+
end
932+
933+
# good
934+
unless condition
935+
do_something
936+
end
937+
938+
# good
939+
if condition
940+
do_something
941+
elsif other_condition
942+
do_something_else
943+
end
944+
----
945+
946+
==== AllowComments: true (default)
947+
948+
[source,ruby]
949+
----
950+
# good
951+
if condition
952+
do_something
953+
elsif other_condition
954+
# noop
955+
end
956+
----
957+
958+
==== AllowComments: false
959+
960+
[source,ruby]
961+
----
962+
# bad
963+
if condition
964+
do_something
965+
elsif other_condition
966+
# noop
967+
end
968+
----
969+
970+
=== Configurable attributes
971+
972+
|===
973+
| Name | Default value | Configurable values
974+
975+
| AllowComments
976+
| `true`
977+
| Boolean
978+
|===
979+
896980
== Lint/EmptyEnsure
897981

898982
|===

lib/rubocop.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@
258258
require_relative 'rubocop/cop/lint/duplicate_rescue_exception'
259259
require_relative 'rubocop/cop/lint/each_with_object_argument'
260260
require_relative 'rubocop/cop/lint/else_layout'
261+
require_relative 'rubocop/cop/lint/empty_conditional_body'
261262
require_relative 'rubocop/cop/lint/empty_ensure'
262263
require_relative 'rubocop/cop/lint/empty_expression'
263264
require_relative 'rubocop/cop/lint/empty_interpolation'
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# frozen_string_literal: true
2+
3+
module RuboCop
4+
module Cop
5+
module Lint
6+
# This cop checks for the presence of `if`, `elsif` and `unless` branches without a body.
7+
# @example
8+
# # bad
9+
# if condition
10+
# end
11+
#
12+
# # bad
13+
# unless condition
14+
# end
15+
#
16+
# # bad
17+
# if condition
18+
# do_something
19+
# elsif other_condition
20+
# end
21+
#
22+
# # good
23+
# if condition
24+
# do_something
25+
# end
26+
#
27+
# # good
28+
# unless condition
29+
# do_something
30+
# end
31+
#
32+
# # good
33+
# if condition
34+
# do_something
35+
# elsif other_condition
36+
# do_something_else
37+
# end
38+
#
39+
# @example AllowComments: true (default)
40+
# # good
41+
# if condition
42+
# do_something
43+
# elsif other_condition
44+
# # noop
45+
# end
46+
#
47+
# @example AllowComments: false
48+
# # bad
49+
# if condition
50+
# do_something
51+
# elsif other_condition
52+
# # noop
53+
# end
54+
#
55+
class EmptyConditionalBody < Base
56+
MSG = 'Avoid `%<keyword>s` branches without a body.'
57+
58+
def on_if(node)
59+
return if node.body
60+
return if cop_config['AllowComments'] && comment_lines?(node)
61+
62+
add_offense(node, message: format(MSG, keyword: node.keyword))
63+
end
64+
end
65+
end
66+
end
67+
end
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe RuboCop::Cop::Lint::EmptyConditionalBody, :config do
4+
let(:cop_config) do
5+
{ 'AllowComments' => true }
6+
end
7+
8+
it 'registers an offense for missing `if` body' do
9+
expect_offense(<<~RUBY)
10+
if condition
11+
^^^^^^^^^^^^ Avoid `if` branches without a body.
12+
end
13+
RUBY
14+
end
15+
16+
it 'does not register an offense for missing `if` body with a comment' do
17+
expect_no_offenses(<<~RUBY)
18+
if condition
19+
# noop
20+
end
21+
RUBY
22+
end
23+
24+
it 'registers an offense for missing `elsif` body' do
25+
expect_offense(<<~RUBY)
26+
if condition
27+
do_something
28+
elsif other_condition
29+
^^^^^^^^^^^^^^^^^^^^^ Avoid `elsif` branches without a body.
30+
end
31+
RUBY
32+
end
33+
34+
it 'does not register an offense for missing `elsif` body with a comment' do
35+
expect_no_offenses(<<~RUBY)
36+
if condition
37+
do_something
38+
elsif other_condition
39+
# noop
40+
end
41+
RUBY
42+
end
43+
44+
it 'registers an offense for missing `unless` body' do
45+
expect_offense(<<~RUBY)
46+
unless condition
47+
^^^^^^^^^^^^^^^^ Avoid `unless` branches without a body.
48+
end
49+
RUBY
50+
end
51+
52+
it 'does not register an offense for missing `unless` body with a comment' do
53+
expect_no_offenses(<<~RUBY)
54+
unless condition
55+
# noop
56+
end
57+
RUBY
58+
end
59+
60+
context 'when AllowComments is false' do
61+
let(:cop_config) do
62+
{ 'AllowComments' => false }
63+
end
64+
65+
it 'registers an offense for missing `if` body with a comment' do
66+
expect_offense(<<~RUBY)
67+
if condition
68+
^^^^^^^^^^^^ Avoid `if` branches without a body.
69+
# noop
70+
end
71+
RUBY
72+
end
73+
74+
it 'registers an offense for missing `elsif` body with a comment' do
75+
expect_offense(<<~RUBY)
76+
if condition
77+
do_something
78+
elsif other_condition
79+
^^^^^^^^^^^^^^^^^^^^^ Avoid `elsif` branches without a body.
80+
# noop
81+
end
82+
RUBY
83+
end
84+
85+
it 'registers an offense for missing `unless` body with a comment' do
86+
expect_offense(<<~RUBY)
87+
unless condition
88+
^^^^^^^^^^^^^^^^ Avoid `unless` branches without a body.
89+
# noop
90+
end
91+
RUBY
92+
end
93+
end
94+
end

0 commit comments

Comments
 (0)