checker: check error for match conditions with parenthesis (#13130)

pull/13135/head
yuyi 2022-01-11 15:50:20 +08:00 committed by GitHub
parent f54ad51946
commit 791972ebc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 0 deletions

View File

@ -8,6 +8,10 @@ import strings
pub fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
node.is_expr = c.expected_type != ast.void_type
node.expected_type = c.expected_type
if node.cond is ast.ParExpr {
c.error('unnecessary `()` in `match` condition, use `match expr {` instead of `match (expr) {`.',
node.cond.pos)
}
cond_type := c.expr(node.cond)
// we setting this here rather than at the end of the method
// since it is used in c.match_exprs() it saves checking twice

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/match_cond_with_parenthesis_err.vv:14:15: error: unnecessary `()` in `match` condition, use `match expr {` instead of `match (expr) {`.
12 |
13 | fn bar() bool {
14 | return match (foo()) {
| ~~~~~~~
15 | .a { true }
16 | .b, .c { false }

View File

@ -0,0 +1,22 @@
module main
enum Enum {
a
b
c
}
fn foo() ?Enum {
return .a
}
fn bar() bool {
return match (foo()) {
.a { true }
.b, .c { false }
}
}
fn main() {
bar()
}