checker: warn for deprecated methods too (#8861)

pull/8862/head
Nick Treleaven 2021-02-20 17:25:54 +00:00 committed by GitHub
parent 5243c5adce
commit 1e71c0eaca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 1 deletions

View File

@ -1562,6 +1562,15 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type {
c.warn('method `${left_type_sym.name}.$method_name` must be called from an `unsafe` block',
call_expr.pos)
}
if !c.cur_fn.is_deprecated && method.is_deprecated {
mut deprecation_message := 'method `${left_type_sym.name}.$method.name` has been deprecated'
for attr in method.attrs {
if attr.name == 'deprecated' && attr.arg != '' {
deprecation_message += '; $attr.arg'
}
}
c.warn(deprecation_message, call_expr.pos)
}
// TODO: typ optimize.. this node can get processed more than once
if call_expr.expected_arg_types.len == 0 {
for i in 1 .. method.params.len {
@ -1783,7 +1792,7 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type {
c.error('function `$f.name` is private, so you can not import it in module `$c.mod`',
call_expr.pos)
}
if f.is_deprecated {
if !c.cur_fn.is_deprecated && f.is_deprecated {
mut deprecation_message := 'function `$f.name` has been deprecated'
for d in f.attrs {
if d.name == 'deprecated' && d.arg != '' {

View File

@ -11,3 +11,10 @@ vlib/v/checker/tests/use_deprecated_function_warning.vv:13:2: error: function `a
13 | abc()
| ~~~~~
14 | }
15 |
vlib/v/checker/tests/use_deprecated_function_warning.vv:23:4: error: method `S1.m` has been deprecated; use bar instead
21 | fn method() {
22 | s := S1{}
23 | s.m()
| ~~~
24 | }

View File

@ -12,3 +12,13 @@ fn main() {
xyz()
abc()
}
struct S1 {}
[deprecated: 'use bar instead']
fn (s S1) m() {}
fn method() {
s := S1{}
s.m()
}