Skip to content

checker: add missing check for IfExpr and MatchExpr with no valid type #23832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions vlib/v/checker/if.v
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,10 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
c.returns = false
}
}
if node.typ == ast.none_type {
c.error('invalid if expression, must supply at least one value other than `none`',
node.pos)
}
// if only untyped literals were given default to int/f64
node.typ = ast.mktyp(node.typ)
if expr_required && !node.has_else {
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/checker/match.v
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
c.returns = false
}
}
if ret_type == ast.none_type {
c.error('invalid match expression, must supply at least one value other than `none`',
node.pos)
}
node.return_type = ret_type
cond_var := c.get_base_name(&node.cond)
if cond_var != '' {
Expand Down
10 changes: 10 additions & 0 deletions vlib/v/checker/tests/if_expr_with_none_only.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
vlib/v/checker/tests/if_expr_with_none_only.vv:2:2: warning: unused variable: `a`
1 | fn main() {
2 | a := if true { none } else { none }
| ^
3 | }
vlib/v/checker/tests/if_expr_with_none_only.vv:2:7: error: invalid if expression, must supply at least one value other than `none`
1 | fn main() {
2 | a := if true { none } else { none }
| ~~
3 | }
3 changes: 3 additions & 0 deletions vlib/v/checker/tests/if_expr_with_none_only.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
a := if true { none } else { none }
}
12 changes: 12 additions & 0 deletions vlib/v/checker/tests/match_expr_with_none_only.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
vlib/v/checker/tests/match_expr_with_none_only.vv:2:2: warning: unused variable: `a`
1 | fn main() {
2 | a := match 1 {
| ^
3 | 1 { none }
4 | else { none }
vlib/v/checker/tests/match_expr_with_none_only.vv:2:7: error: invalid match expression, must supply at least one value other than `none`
1 | fn main() {
2 | a := match 1 {
| ~~~~~~~~~
3 | 1 { none }
4 | else { none }
6 changes: 6 additions & 0 deletions vlib/v/checker/tests/match_expr_with_none_only.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
a := match 1 {
1 { none }
else { none }
}
}
Loading