checker: check deprecated functions

pull/5464/head
yuyi 2020-06-23 22:25:24 +08:00 committed by GitHub
parent db28796b5f
commit 7efb3ecb34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 36 additions and 14 deletions

View File

@ -1307,7 +1307,8 @@ pub fn (s string) limit(max int) string {
[deprecated] [deprecated]
pub fn (c byte) is_white() bool { pub fn (c byte) is_white() bool {
panic('Use `string.is_space` instead of `string.is_white') eprintln('warning: `string.is_white` has been deprecated, use `string.is_space` instead')
return c.is_space()
} }
pub fn (s string) hash() int { pub fn (s string) hash() int {

View File

@ -204,7 +204,8 @@ pub fn cp(old, new string) ?bool {
[deprecated] [deprecated]
pub fn cp_r(osource_path, odest_path string, overwrite bool) ?bool { pub fn cp_r(osource_path, odest_path string, overwrite bool) ?bool {
panic('Use `os.cp_all` instead of `os.cp_r`') eprintln('warning: `os.cp_r` has been deprecated, use `os.cp_all` instead')
return cp_all(osource_path, odest_path, overwrite)
} }
pub fn cp_all(osource_path, odest_path string, overwrite bool) ?bool { pub fn cp_all(osource_path, odest_path string, overwrite bool) ?bool {
@ -622,7 +623,8 @@ pub fn is_readable(path string) bool {
[deprecated] [deprecated]
pub fn file_exists(_path string) bool { pub fn file_exists(_path string) bool {
panic('Use `os.exists` instead of `os.file_exists`') eprintln('warning: `os.file_exists` has been deprecated, use `os.exists` instead')
return exists(_path)
} }
// rm removes file in `path`. // rm removes file in `path`.
@ -645,7 +647,8 @@ pub fn rmdir(path string) {
[deprecated] [deprecated]
pub fn rmdir_recursive(path string) { pub fn rmdir_recursive(path string) {
panic('Use `os.rmdir_all` instead of `os.rmdir_recursive`') eprintln('warning: `os.rmdir_recursive` has been deprecated, use `os.rmdir_all` instead')
rmdir_all(path)
} }
pub fn rmdir_all(path string) { pub fn rmdir_all(path string) {
@ -1004,7 +1007,8 @@ pub fn exists_in_system_path(prog string) bool {
[deprecated] [deprecated]
pub fn dir_exists(path string) bool { pub fn dir_exists(path string) bool {
panic('Use `os.is_dir` instead of `os.dir_exists`') eprintln('warning: `os.dir_exists` has been deprecated, use `os.is_dir` instead')
return is_dir(path)
} }
// is_dir returns a boolean indicating whether the given path is a directory. // is_dir returns a boolean indicating whether the given path is a directory.
@ -1198,7 +1202,8 @@ pub fn log(s string) {
[deprecated] [deprecated]
pub fn flush_stdout() { pub fn flush_stdout() {
panic('Use `os.flush` instead of `os.flush_stdout`') eprintln('warning: `os.flush_stdout` has been deprecated, use `os.flush` instead')
flush()
} }
pub fn flush() { pub fn flush() {

View File

@ -999,6 +999,9 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type {
if !f.is_pub && f.language == .v && f.name.len > 0 && f.mod.len > 0 && f.mod != c.mod { if !f.is_pub && f.language == .v && f.name.len > 0 && f.mod.len > 0 && f.mod != c.mod {
c.error('function `$f.name` is private. curmod=$c.mod fmod=$f.mod', call_expr.pos) c.error('function `$f.name` is private. curmod=$c.mod fmod=$f.mod', call_expr.pos)
} }
if f.is_deprecated {
c.warn('function `$f.name` has been deprecated', call_expr.pos)
}
call_expr.return_type = f.return_type call_expr.return_type = f.return_type
if f.return_type == table.void_type && f.ctdefine.len > 0 && f.ctdefine !in c.pref.compile_defines { if f.return_type == table.void_type && f.ctdefine.len > 0 && f.ctdefine !in c.pref.compile_defines {
call_expr.should_be_skipped = true call_expr.should_be_skipped = true

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/use_deprecated_function_warning.v:3:5: error: function `os.cp_r` has been deprecated
1 | import os
2 | fn main() {
3 | os.cp_r('./aa', './bb', true)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
4 | }

View File

@ -0,0 +1,4 @@
import os
fn main() {
os.cp_r('./aa', './bb', true)
}

View File

@ -235,6 +235,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
is_variadic: is_variadic is_variadic: is_variadic
is_generic: is_generic is_generic: is_generic
is_pub: is_pub is_pub: is_pub
is_deprecated: is_deprecated
ctdefine: ctdefine ctdefine: ctdefine
}) })
} else { } else {
@ -256,6 +257,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
language: language language: language
is_generic: is_generic is_generic: is_generic
is_pub: is_pub is_pub: is_pub
is_deprecated: is_deprecated
ctdefine: ctdefine ctdefine: ctdefine
mod: p.mod mod: p.mod
}) })

View File

@ -27,6 +27,7 @@ pub:
language Language language Language
is_generic bool is_generic bool
is_pub bool is_pub bool
is_deprecated bool
mod string mod string
ctdefine string // compile time define. myflag, when [if myflag] tag ctdefine string // compile time define. myflag, when [if myflag] tag
pub mut: pub mut: