mirror of
https://github.com/ruby/ruby.git
synced 2026-10-28 16:36:20 +08:00
[Bug #21669] Implement void value expression check for case/when
If the all `when` and `else` branches are void, the `case` is also void.
This commit is contained in:
2 files changed
+36
Notes:
git
2026-02-04 05:01:23 +00:00
No files matched your search
@@ -13864,6 +13864,18 @@ value_expr_check(struct parser_params *p, NODE *node)
|
||||
case NODE_RETRY:
|
||||
goto found;
|
||||
|
||||
case NODE_CASE:
|
||||
case NODE_CASE2:
|
||||
for (node = RNODE_CASE(node)->nd_body;
|
||||
node && nd_type_p(node, NODE_WHEN);
|
||||
node = RNODE_WHEN(node)->nd_next) {
|
||||
if (!(vn = value_expr_check(p, RNODE_WHEN(node)->nd_body))) {
|
||||
return NULL;
|
||||
}
|
||||
if (!void_node) void_node = vn;
|
||||
}
|
||||
break;
|
||||
|
||||
case NODE_CASE3:
|
||||
if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) {
|
||||
compile_error(p, "unexpected node");
|
||||
|
||||
@@ -2053,6 +2053,30 @@ eom
|
||||
};
|
||||
end
|
||||
|
||||
def test_value_expr_in_case
|
||||
assert_syntax_error("#{<<~"{#"}\n#{<<~'};'}", /void value expression/, nil, "#{BUG_21669} 1.3")
|
||||
{#
|
||||
x =
|
||||
case a
|
||||
when 1; return
|
||||
when 2; return
|
||||
else return
|
||||
end
|
||||
};
|
||||
end
|
||||
|
||||
def test_value_expr_in_case2
|
||||
assert_syntax_error("#{<<~"{#"}\n#{<<~'};'}", /void value expression/, nil, "#{BUG_21669} 1.3")
|
||||
{#
|
||||
x =
|
||||
case
|
||||
when 1; return
|
||||
when 2; return
|
||||
else return
|
||||
end
|
||||
};
|
||||
end
|
||||
|
||||
def test_tautological_condition
|
||||
assert_valid_syntax("def f() return if false and invalid; nil end")
|
||||
assert_valid_syntax("def f() return unless true or invalid; nil end")
|
||||
|
||||
Reference in new issue
Block a user