Ruby

Ruby

Made by DeepSource

Unnecessary condition used RB-ST1117

Anti-pattern
Minor
Autofix

The provided condition is unnecessary and can be substituted with || operator. Since this operator implicitly checks if the first operand is truthy, there is no need to explicitly check it with a condition.

Bad practice

a = b ? b : c

ary << if foo
  foo
else
  bar
end

if b
  b
else
  raise 'foo'
end

# this will return a nil value if bar is falsy
bar if bar

if foo
  bar(foo)
else
  bar({})
end

Recommended

a = b || c

b || c

if b
  b
elsif cond
  c
end

b || raise('foo')

ary << (foo || bar)

# Checking for a hash key before returning it is allowed
if @cache[key]
  @cache[key]
else
  @cache[key] = heavy_load[key]
end

bar(foo || {})