Skip to content

Commit

Permalink
checker: check error for match conditions with parenthesis (#13130)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Jan 11, 2022
1 parent f54ad51 commit 791972e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions vlib/v/checker/match.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/match_cond_with_parenthesis_err.out
Original file line number Diff line number Diff line change
@@ -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 }
22 changes: 22 additions & 0 deletions vlib/v/checker/tests/match_cond_with_parenthesis_err.vv
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 791972e

Please sign in to comment.