Skip to content

Commit cdcc4f7

Browse files
committed
Disallow the unless...else code pattern.
1 parent d4d00fa commit cdcc4f7

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

.rubocop.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,3 +369,8 @@ Style/StringLiterals:
369369
- single_quotes
370370
- double_quotes
371371
ConsistentQuotesInMultiline: false
372+
373+
# Supports --auto-correct
374+
Style/UnlessElse:
375+
Description: Checks for unless expressions with else clauses.
376+
Enabled: true

src/main/ruby/core/hash.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,7 @@ def keep_if
245245
def merge(*others)
246246
current = self.dup
247247

248-
unless block_given?
249-
others.each do |other|
250-
other = Truffle::Type.coerce_to(other, Hash, :to_hash)
251-
current = Truffle::HashOperations.hash_merge(current, other)
252-
end
253-
else
248+
if block_given?
254249
others.each do |other|
255250
other.each do |k, v|
256251
other = Truffle::Type.coerce_to(other, Hash, :to_hash)
@@ -261,6 +256,11 @@ def merge(*others)
261256
end
262257
end
263258
end
259+
else
260+
others.each do |other|
261+
other = Truffle::Type.coerce_to(other, Hash, :to_hash)
262+
current = Truffle::HashOperations.hash_merge(current, other)
263+
end
264264
end
265265
current
266266
end

src/main/ruby/core/kernel.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,14 @@ def eval(str, a_binding=nil, file=nil, line=nil)
231231
str = str.to_str unless str.class == String
232232
file = file.to_str unless file.class == String
233233
line = line.to_i unless line.is_a?(Integer)
234-
unless a_binding
235-
receiver = self
236-
a_binding = Truffle.invoke_primitive(:caller_binding)
237-
else
234+
if a_binding
238235
unless a_binding.class == Binding
239236
raise TypeError, "Wrong argument type #{a_binding.class} (expected binding)"
240237
end
241238
receiver = a_binding.receiver
239+
else
240+
receiver = self
241+
a_binding = Truffle.invoke_primitive(:caller_binding)
242242
end
243243

244244
Truffle.invoke_primitive(:kernel_eval, receiver, str, a_binding, file, line)

src/main/ruby/core/regexp.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ def self.union(*patterns)
166166
str = ''.encode(enc)
167167

168168
patterns = patterns.map do |pat|
169-
unless pat.kind_of? Regexp
170-
StringValue(pat)
171-
else
169+
if pat.kind_of? Regexp
172170
pat
171+
else
172+
StringValue(pat)
173173
end
174174
end
175175

0 commit comments

Comments
 (0)