forked from rubocop/rubocop
/
redundant_condition.rb
233 lines (194 loc) · 6.8 KB
/
redundant_condition.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# This cop checks for unnecessary conditional expressions.
#
# @example
# # bad
# a = b ? b : c
#
# # good
# a = b || c
#
# @example
# # bad
# if b
# b
# else
# c
# end
#
# # good
# b || c
#
# # good
# if b
# b
# elsif cond
# c
# end
#
class RedundantCondition < Base
include RangeHelp
extend AutoCorrector
MSG = 'Use double pipes `||` instead.'
REDUNDANT_CONDITION = 'This condition is not needed.'
def on_if(node)
return if node.elsif_conditional?
return unless offense?(node)
message = message(node)
add_offense(range_of_offense(node), message: message) do |corrector|
if node.ternary? && !branches_have_method?(node)
correct_ternary(corrector, node)
elsif redudant_condition?(node)
corrector.replace(node, node.if_branch.source)
else
corrected = make_ternary_form(node)
corrector.replace(node, corrected)
end
end
end
private
def message(node)
if redudant_condition?(node)
REDUNDANT_CONDITION
else
MSG
end
end
def range_of_offense(node)
return node.loc.expression unless node.ternary?
return node.loc.expression if node.ternary? && branches_have_method?(node)
range_between(node.loc.question.begin_pos, node.loc.colon.end_pos)
end
def offense?(node)
_condition, _if_branch, else_branch = *node
return false if use_if_branch?(else_branch) || use_hash_key_assignment?(else_branch)
synonymous_condition_and_branch?(node) && !node.elsif? &&
(node.ternary? || !else_branch.instance_of?(AST::Node) || else_branch.single_line?)
end
def redudant_condition?(node)
node.modifier_form? || !node.else_branch
end
def use_if_branch?(else_branch)
else_branch&.if_type?
end
def use_hash_key_assignment?(else_branch)
else_branch&.send_type? && else_branch&.method?(:[]=)
end
def use_hash_key_access?(node)
node.send_type? && node.method?(:[])
end
def synonymous_condition_and_branch?(node)
condition, if_branch, _else_branch = *node
# e.g.
# if var
# var
# else
# 'foo'
# end
return true if condition == if_branch
# e.g.
# if foo
# @value = foo
# else
# @value = another_value?
# end
return true if branches_have_assignment?(node) && condition == if_branch.expression
# e.g.
# if foo
# test.value = foo
# else
# test.value = another_value?
# end
branches_have_method?(node) && condition == if_branch.first_argument &&
!use_hash_key_access?(if_branch)
end
def branches_have_assignment?(node)
_condition, if_branch, else_branch = *node
return false unless if_branch && else_branch
asgn_type?(if_branch) && (if_branch_variable_name = if_branch.name) &&
asgn_type?(else_branch) && (else_branch_variable_name = else_branch.name) &&
if_branch_variable_name == else_branch_variable_name
end
def asgn_type?(node)
node.lvasgn_type? || node.ivasgn_type? || node.cvasgn_type? || node.gvasgn_type?
end
def branches_have_method?(node)
_condition, if_branch, else_branch = *node
return false unless if_branch && else_branch
if_branch.send_type? && if_branch.arguments.count == 1 &&
else_branch.send_type? && else_branch.arguments.count == 1 &&
if_branch.method?(else_branch.method_name)
end
def if_source(if_branch)
if branches_have_method?(if_branch.parent) && if_branch.parenthesized?
if_branch.source.delete_suffix(')')
else
if_branch.source
end
end
def else_source(else_branch)
if branches_have_method?(else_branch.parent)
else_source_if_has_method(else_branch)
elsif require_parentheses?(else_branch)
"(#{else_branch.source})"
elsif without_argument_parentheses_method?(else_branch)
"#{else_branch.method_name}(#{else_branch.arguments.map(&:source).join(', ')})"
elsif branches_have_assignment?(else_branch.parent)
else_source_if_has_assignment(else_branch)
else
else_branch.source
end
end
def else_source_if_has_method(else_branch)
if require_parentheses?(else_branch.first_argument)
"(#{else_branch.first_argument.source})"
elsif require_braces?(else_branch.first_argument)
"{ #{else_branch.first_argument.source} }"
else
else_branch.first_argument.source
end
end
def else_source_if_has_assignment(else_branch)
if require_parentheses?(else_branch.expression)
"(#{else_branch.expression.source})"
elsif require_braces?(else_branch.expression)
"{ #{else_branch.expression.source} }"
else
else_branch.expression.source
end
end
def make_ternary_form(node)
_condition, if_branch, else_branch = *node
ternary_form = [if_source(if_branch), else_source(else_branch)].join(' || ')
ternary_form += ')' if branches_have_method?(node) && if_branch.parenthesized?
if node.parent&.send_type?
"(#{ternary_form})"
else
ternary_form
end
end
def correct_ternary(corrector, node)
corrector.replace(range_of_offense(node), '||')
return unless node.else_branch.range_type?
corrector.wrap(node.else_branch, '(', ')')
end
def require_parentheses?(node)
(node.basic_conditional? && node.modifier_form?) ||
node.range_type? ||
node.rescue_type? ||
(node.respond_to?(:semantic_operator?) && node.semantic_operator?)
end
def require_braces?(node)
node.hash_type? && !node.braces?
end
def without_argument_parentheses_method?(node)
node.send_type? && !node.arguments.empty? &&
!node.parenthesized? && !node.operator_method? && !node.assignment_method?
end
end
end
end
end