From da296a451e5035a0d1070a59511aec2965337271 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 66f012534b81a5..338be3cb7709ba 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2553,6 +2553,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 c618485ae31e48bfa9cc885b52e055284f517e2f 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 338be3cb7709ba..66f012534b81a5 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2553,9 +2553,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 8e77166770a5a77b7e06d9abb28418e88c87b186 Mon Sep 17 00:00:00 2001 From: Swastik Date: Fri, 26 Apr 2024 17:56:40 +0530 Subject: [PATCH 3/3] builtin.string: enforce fasely values for numerical values when using `is_upper` and `is_lower` --- vlib/builtin/string.v | 6 ++++++ vlib/builtin/string_test.v | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 5ea321c6752d89..0494be2a62a546 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -1524,6 +1524,9 @@ pub fn (s string) to_lower() string { // Example: assert 'hello developer'.is_lower() == true @[direct_array_access] pub fn (s string) is_lower() bool { + if s[0].is_digit() { + return false + } for i in 0 .. s.len { if s[i] >= `A` && s[i] <= `Z` { return false @@ -1555,6 +1558,9 @@ pub fn (s string) to_upper() string { // Example: assert 'HELLO V'.is_upper() == true @[direct_array_access] pub fn (s string) is_upper() bool { + if s[0].is_digit() { + return false + } for i in 0 .. s.len { if s[i] >= `a` && s[i] <= `z` { return false diff --git a/vlib/builtin/string_test.v b/vlib/builtin/string_test.v index 82a0ce8ff99e9d..c0427e4c6f2ce6 100644 --- a/vlib/builtin/string_test.v +++ b/vlib/builtin/string_test.v @@ -1013,6 +1013,9 @@ fn test_lower() { assert s.to_lower() == 'hi' assert 'aloha!'[0] == `a` assert 'aloha!'[5] == `!` + s = '123' + assert !s.is_lower() + assert s.to_lower() == '123' } fn test_upper() { @@ -1033,6 +1036,9 @@ fn test_upper() { s = 'HI' assert s.is_upper() assert s.to_upper() == 'HI' + s = '123' + assert !s.is_upper() + assert s.to_upper() == '123' } fn test_capitalize() {