checker: add a checker error for fn calls on left side of an assignment (#5993)

pull/6018/head
Swastik Baranwal 2020-07-29 21:03:00 +05:30 committed by GitHub
parent 7b630f0350
commit 4500e7131e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 0 deletions

View File

@ -1619,6 +1619,10 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
// //
is_decl := assign_stmt.op == .decl_assign is_decl := assign_stmt.op == .decl_assign
for i, left in assign_stmt.left { for i, left in assign_stmt.left {
if left is ast.CallExpr {
c.error('cannot call function `${left.name}()` on the left side of an assignment',
left.pos)
}
is_blank_ident := left.is_blank_ident() is_blank_ident := left.is_blank_ident()
mut left_type := table.void_type mut left_type := table.void_type
if !is_decl && !is_blank_ident { if !is_decl && !is_blank_ident {

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/assign_fn_call_on_left_side_err.v:6:2: error: cannot call function `foo()` on the left side of an assignment
4 |
5 | fn main() {
6 | foo('s') = 1
| ~~~~~~~~
7 | }

View File

@ -0,0 +1,7 @@
fn foo(s string) int {
return 1
}
fn main() {
foo('s') = 1
}