Skip to content

Incorrect return type error with tuple return type within loop with conditional logic #15739

Open
@Blacksmoke16

Description

@Blacksmoke16

Bug Report

nodes = ["a", "b", "c"]

def foo(nodes) : {String, Int32}?
  nodes.each do |node|
    if node.is_a?(String) && (branches = conditional_statement_branches node) > 1
      return node, branches
    end
  end
end

private def conditional_statement_branches(node) : Int32
  2
end

pp foo nodes

Results in:

Error: method ::foo must return (Tuple(String, Int32) | Nil) but it is returning Tuple(String, Int32 | Nil)

Why does it think branches could be nil?

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind:bugA bug in the code. Does not apply to documentation, specs, etc.topic:compiler:semantic

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions