/
unreachable_loop.rb
174 lines (155 loc) · 4.36 KB
/
unreachable_loop.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# frozen_string_literal: true
module RuboCop
module Cop
module Lint
# This cop checks for loops that will have at most one iteration.
#
# A loop that can never reach the second iteration is a possible error in the code.
# In rare cases where only one iteration (or at most one iteration) is intended behavior,
# the code should be refactored to use `if` conditionals.
#
# @example
# # bad
# while node
# do_something(node)
# node = node.parent
# break
# end
#
# # good
# while node
# do_something(node)
# node = node.parent
# end
#
# # bad
# def verify_list(head)
# item = head
# begin
# if verify(item)
# return true
# else
# return false
# end
# end while(item)
# end
#
# # good
# def verify_list(head)
# item = head
# begin
# if verify(item)
# item = item.next
# else
# return false
# end
# end while(item)
#
# true
# end
#
# # bad
# def find_something(items)
# items.each do |item|
# if something?(item)
# return item
# else
# raise NotFoundError
# end
# end
# end
#
# # good
# def find_something(items)
# items.each do |item|
# if something?(item)
# return item
# end
# end
# raise NotFoundError
# end
#
class UnreachableLoop < Base
MSG = 'This loop will have at most one iteration.'
def on_while(node)
check(node)
end
alias on_until on_while
alias on_while_post on_while
alias on_until_post on_while
alias on_for on_while
def on_block(node)
check(node) if loop_method?(node)
end
private
def loop_method?(node)
return false unless node.block_type?
send_node = node.send_node
send_node.enumerable_method? || send_node.enumerator_method? || send_node.method?(:loop)
end
def check(node)
statements = statements(node)
break_statement = statements.find { |statement| break_statement?(statement) }
return unless break_statement
add_offense(node) unless preceded_by_continue_statement?(break_statement)
end
def statements(node)
body = node.body
if body.nil?
[]
elsif body.begin_type?
body.children
else
[body]
end
end
def_node_matcher :break_command?, <<~PATTERN
{
return break
(send
{nil? (const {nil? cbase} :Kernel)}
{:raise :fail :throw :exit :exit! :abort}
...)
}
PATTERN
def break_statement?(node)
return true if break_command?(node)
case node.type
when :begin, :kwbegin
statements = *node
statements.any? { |statement| break_statement?(statement) }
when :if
check_if(node)
when :case
check_case(node)
else
false
end
end
def check_if(node)
if_branch = node.if_branch
else_branch = node.else_branch
if_branch && else_branch &&
break_statement?(if_branch) && break_statement?(else_branch)
end
def check_case(node)
else_branch = node.else_branch
return false unless else_branch
return false unless break_statement?(else_branch)
node.when_branches.all? do |branch|
branch.body && break_statement?(branch.body)
end
end
def preceded_by_continue_statement?(break_statement)
left_siblings_of(break_statement).any? do |sibling|
next if sibling.loop_keyword? || loop_method?(sibling)
sibling.each_descendant(:next, :redo).any?
end
end
def left_siblings_of(node)
node.parent.children[0, node.sibling_index]
end
end
end
end
end