From e86e44990d661ab4ef0f052d52248a82248ba391 Mon Sep 17 00:00:00 2001 From: Swastik Date: Thu, 2 Nov 2023 19:50:18 +0530 Subject: [PATCH 1/3] checker: warn on unused imported functions used via `import math { sin, cos }` --- vlib/v/checker/checker.v | 3 +++ .../checker/tests/import_sym_fn_unused_warning_err.out | 10 ++++++++++ .../checker/tests/import_sym_fn_unused_warning_err.vv | 3 +++ 3 files changed, 16 insertions(+) create mode 100644 vlib/v/checker/tests/import_sym_fn_unused_warning_err.out create mode 100644 vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 40bfc7a0991c38..1eb3616bf3ee1e 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2645,6 +2645,9 @@ fn (mut c Checker) import_stmt(node ast.Import) { if !func.is_pub { c.error('module `${node.mod}` function `${sym.name}()` is private', sym.pos) } + if func.usages != 1 { + c.warn('module `${node.mod}` function `${sym.name}()` is unused', sym.pos) + } continue } if _ := c.file.global_scope.find_const(name) { diff --git a/vlib/v/checker/tests/import_sym_fn_unused_warning_err.out b/vlib/v/checker/tests/import_sym_fn_unused_warning_err.out new file mode 100644 index 00000000000000..d0bb2c363db71a --- /dev/null +++ b/vlib/v/checker/tests/import_sym_fn_unused_warning_err.out @@ -0,0 +1,10 @@ +vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv:1:15: warning: module `math` function `sin()` is unused + 1 | import math { sin, cos } + | ~~~ + 2 | + 3 | fn main() {} +vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv:1:20: warning: module `math` function `cos()` is unused + 1 | import math { sin, cos } + | ~~~ + 2 | + 3 | fn main() {} diff --git a/vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv b/vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv new file mode 100644 index 00000000000000..e5a7d32933c895 --- /dev/null +++ b/vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv @@ -0,0 +1,3 @@ +import math { sin, cos } + +fn main() {} From b93a63287c9c550383a7860d07a6db2dfa24dc0f Mon Sep 17 00:00:00 2001 From: Swastik Date: Thu, 2 Nov 2023 19:50:42 +0530 Subject: [PATCH 2/3] Revert "checker: warn on unused imported functions used via `import math { sin, cos }`" This reverts commit ddf0468f1c4f9af54ada162712c212214e58ca56. --- vlib/v/checker/checker.v | 3 --- .../checker/tests/import_sym_fn_unused_warning_err.out | 10 ---------- .../checker/tests/import_sym_fn_unused_warning_err.vv | 3 --- 3 files changed, 16 deletions(-) delete mode 100644 vlib/v/checker/tests/import_sym_fn_unused_warning_err.out delete mode 100644 vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 1eb3616bf3ee1e..40bfc7a0991c38 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2645,9 +2645,6 @@ fn (mut c Checker) import_stmt(node ast.Import) { if !func.is_pub { c.error('module `${node.mod}` function `${sym.name}()` is private', sym.pos) } - if func.usages != 1 { - c.warn('module `${node.mod}` function `${sym.name}()` is unused', sym.pos) - } continue } if _ := c.file.global_scope.find_const(name) { diff --git a/vlib/v/checker/tests/import_sym_fn_unused_warning_err.out b/vlib/v/checker/tests/import_sym_fn_unused_warning_err.out deleted file mode 100644 index d0bb2c363db71a..00000000000000 --- a/vlib/v/checker/tests/import_sym_fn_unused_warning_err.out +++ /dev/null @@ -1,10 +0,0 @@ -vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv:1:15: warning: module `math` function `sin()` is unused - 1 | import math { sin, cos } - | ~~~ - 2 | - 3 | fn main() {} -vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv:1:20: warning: module `math` function `cos()` is unused - 1 | import math { sin, cos } - | ~~~ - 2 | - 3 | fn main() {} diff --git a/vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv b/vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv deleted file mode 100644 index e5a7d32933c895..00000000000000 --- a/vlib/v/checker/tests/import_sym_fn_unused_warning_err.vv +++ /dev/null @@ -1,3 +0,0 @@ -import math { sin, cos } - -fn main() {} From 52dec97f242fec3ddde776c15721f416025c227a Mon Sep 17 00:00:00 2001 From: Swastik Date: Fri, 27 Sep 2024 01:10:10 +0530 Subject: [PATCH 3/3] checker: disallow infix exper on left side of assign --- vlib/v/checker/assign.v | 4 ++++ .../tests/infix_expr_left_side_assogn_err.out | 13 +++++++++++++ .../tests/infix_expr_left_side_assogn_err.vv | 6 ++++++ vlib/v/checker/tests/invalid_literal_assign_err.out | 13 +++++++++++++ 4 files changed, 36 insertions(+) create mode 100644 vlib/v/checker/tests/infix_expr_left_side_assogn_err.out create mode 100644 vlib/v/checker/tests/infix_expr_left_side_assogn_err.vv diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index 0d4c909847686b..ebccbfb7333fd0 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -495,6 +495,10 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { left.recursive_mapset_is_setter(true) } } + if mut left is ast.InfixExpr { + c.error('cannot use infix expression on the left side of `${node.op}`', + left.pos) + } if is_decl { c.error('non-name `${left}` on left side of `:=`', left.pos()) } diff --git a/vlib/v/checker/tests/infix_expr_left_side_assogn_err.out b/vlib/v/checker/tests/infix_expr_left_side_assogn_err.out new file mode 100644 index 00000000000000..a7ca5f0933c304 --- /dev/null +++ b/vlib/v/checker/tests/infix_expr_left_side_assogn_err.out @@ -0,0 +1,13 @@ +vlib/v/checker/tests/infix_expr_left_side_assogn_err.vv:4:4: error: cannot use infix expression on the left side of `=` + 2 | a := 69 + 3 | b := 420 + 4 | a + b = a + b + | ^ + 5 | (a + b) = a + b + 6 | } +vlib/v/checker/tests/infix_expr_left_side_assogn_err.vv:5:5: error: cannot use infix expression on the left side of `=` + 3 | b := 420 + 4 | a + b = a + b + 5 | (a + b) = a + b + | ^ + 6 | } diff --git a/vlib/v/checker/tests/infix_expr_left_side_assogn_err.vv b/vlib/v/checker/tests/infix_expr_left_side_assogn_err.vv new file mode 100644 index 00000000000000..92ba4615d3c07b --- /dev/null +++ b/vlib/v/checker/tests/infix_expr_left_side_assogn_err.vv @@ -0,0 +1,6 @@ +fn main() { + a := 69 + b := 420 + a + b = a + b + (a + b) = a + b +} diff --git a/vlib/v/checker/tests/invalid_literal_assign_err.out b/vlib/v/checker/tests/invalid_literal_assign_err.out index 1c87f47c6bb1fc..2131ce5cc7bcc7 100644 --- a/vlib/v/checker/tests/invalid_literal_assign_err.out +++ b/vlib/v/checker/tests/invalid_literal_assign_err.out @@ -47,6 +47,13 @@ vlib/v/checker/tests/invalid_literal_assign_err.vv:10:2: error: non-name literal | ~~~~ 11 | 3 + 5 = 3 & 4 12 | (3 + 3.5) = 4 + 6.4 +vlib/v/checker/tests/invalid_literal_assign_err.vv:11:4: error: cannot use infix expression on the left side of `=` + 9 | Foo{} = Foo{} + 10 | true = false + 11 | 3 + 5 = 3 & 4 + | ^ + 12 | (3 + 3.5) = 4 + 6.4 + 13 | } vlib/v/checker/tests/invalid_literal_assign_err.vv:11:2: error: non-name literal value `3 + 5` on left side of `=` 9 | Foo{} = Foo{} 10 | true = false @@ -54,6 +61,12 @@ vlib/v/checker/tests/invalid_literal_assign_err.vv:11:2: error: non-name literal | ~~~~~ 12 | (3 + 3.5) = 4 + 6.4 13 | } +vlib/v/checker/tests/invalid_literal_assign_err.vv:12:5: error: cannot use infix expression on the left side of `=` + 10 | true = false + 11 | 3 + 5 = 3 & 4 + 12 | (3 + 3.5) = 4 + 6.4 + | ^ + 13 | } vlib/v/checker/tests/invalid_literal_assign_err.vv:12:3: error: non-name literal value `3 + 3.5` on left side of `=` 10 | true = false 11 | 3 + 5 = 3 & 4