Skip to content
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

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

Merged
merged 1 commit into from
Jan 11, 2022
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/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()
}