From 8327c9afc1ccd870190752dea5f71828a98546f2 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sat, 20 Feb 2021 17:35:29 +0200 Subject: [PATCH] checker: support a custom `[deprecated: 'message']` --- vlib/v/checker/checker.v | 8 +++++++- .../tests/use_deprecated_function_warning.out | 19 +++++++++++++------ .../tests/use_deprecated_function_warning.vv | 16 +++++++++++++--- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 5ef8c2e620..6ce3198c58 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1784,7 +1784,13 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type { call_expr.pos) } if f.is_deprecated { - c.warn('function `$f.name` has been deprecated', call_expr.pos) + mut deprecation_message := 'function `$f.name` has been deprecated' + for d in f.attrs { + if d.name == 'deprecated' && d.arg != '' { + deprecation_message += '; $d.arg' + } + } + c.warn(deprecation_message, call_expr.pos) } if f.is_unsafe && !c.inside_unsafe && (f.language != .c || (f.name[2] in [`m`, `s`] && f.mod == 'builtin')) { diff --git a/vlib/v/checker/tests/use_deprecated_function_warning.out b/vlib/v/checker/tests/use_deprecated_function_warning.out index 9e3c353a12..cce53f6e23 100644 --- a/vlib/v/checker/tests/use_deprecated_function_warning.out +++ b/vlib/v/checker/tests/use_deprecated_function_warning.out @@ -1,6 +1,13 @@ -vlib/v/checker/tests/use_deprecated_function_warning.vv:3:5: error: function `os.cp_r` has been deprecated - 1 | import os - 2 | fn main() { - 3 | os.cp_r('./aa', './bb', true) - | ~~~~~~~~~~~~~~~~~~~~~~~~~~ - 4 | } +vlib/v/checker/tests/use_deprecated_function_warning.vv:12:2: error: function `xyz` has been deprecated + 10 | + 11 | fn main() { + 12 | xyz() + | ~~~~~ + 13 | abc() + 14 | } +vlib/v/checker/tests/use_deprecated_function_warning.vv:13:2: error: function `abc` has been deprecated; use foo2 instead + 11 | fn main() { + 12 | xyz() + 13 | abc() + | ~~~~~ + 14 | } diff --git a/vlib/v/checker/tests/use_deprecated_function_warning.vv b/vlib/v/checker/tests/use_deprecated_function_warning.vv index 7f45b782d4..6a72a2ad92 100644 --- a/vlib/v/checker/tests/use_deprecated_function_warning.vv +++ b/vlib/v/checker/tests/use_deprecated_function_warning.vv @@ -1,4 +1,14 @@ -import os -fn main() { - os.cp_r('./aa', './bb', true) +[deprecated] +fn xyz() { + println('hi') +} + +[deprecated: 'use foo2 instead'] +fn abc() { + println('hi') +} + +fn main() { + xyz() + abc() }