parser: add a warning for only else branch in match (#8185)

pull/8188/head^2
Swastik Baranwal 2021-01-18 22:01:36 +05:30 committed by GitHub
parent f375418847
commit 6f1ae65811
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 0 deletions

View File

@ -1,3 +1,10 @@
vlib/v/checker/tests/return_missing_match_simple.vv:4:3: warning: `match` must have at least one non `else` branch
2 | a := 1
3 | match a {
4 | else { println('abc') }
| ~~~~
5 | }
6 | println('hello')
vlib/v/checker/tests/return_missing_match_simple.vv:1:1: error: missing return at end of function `h`
1 | fn h() int {
| ~~~~~~~~~~

View File

@ -247,6 +247,9 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
post_comments: post_comments
scope: branch_scope
}
if is_else && branches.len == 1 {
p.warn_with_pos('`match` must have at least one non `else` branch', pos)
}
if p.tok.kind == .rcbr || (is_else && no_lcbr) {
break
}