rubocop icon indicating copy to clipboard operation
rubocop copied to clipboard

Unclear advice on conditional chaining after if/rescue block

Open forthrin opened this issue 3 months ago • 0 comments

Steps to reproduce the problem

We want nil from a set of tests or exceptions to fall back to a default value (given some extra criteria).

def foo
  if 1 + 1 == 2
    begin
      raise StandardError
    rescue StandardError
      nil
    else
      4
    end
  elsif 5 + 5 == 10
    nil
  elsif 6 + 6 == 12
    7
  end || 8 if 9 + 9 == 18
end

pp foo

Expected behavior

The code looks concisely streamlined and should be left as-is.

Actual behavior

Favor a normal if-statement over a modifier clause in a multiline statement. [[Correctable] Style/MultilineIfModifier]

It is quite unclear what a "normal if-statement" would signify in this situation, or whether it even applies.

Parenthesizing the expression after || silences the warning, though the expression without parentheses in a simpler context (eg. a = [][1..] || 8 if 9 + 9 == 18) does not produce a warning.

RuboCop version

System Version: macOS 26.1 (25B78)
Model Identifier: MacBookAir10,1
ruby 3.4.7 (2025-10-08 revision 7a5688e2a2) +PRISM [arm64-darwin24]
rubocop     2025-11-21 f0983bde0

forthrin avatar Nov 30 '25 14:11 forthrin