diff options
| author | Ayrton <a.munoz3327@gmail.com> | 2020-09-10 22:53:14 -0400 |
|---|---|---|
| committer | Ayrton <a.munoz3327@gmail.com> | 2020-10-27 11:04:04 -0400 |
| commit | 3214de735969602ddf2625ca500ff8d3ec57a745 (patch) | |
| tree | 7c9d4151373a76802c659f980dd0a21169fb5bb9 /src | |
| parent | 975547d475fcb3a7c6e440be5f8de3bf0497d383 (diff) | |
| download | rust-3214de735969602ddf2625ca500ff8d3ec57a745.tar.gz rust-3214de735969602ddf2625ca500ff8d3ec57a745.zip | |
modified lint to work with MIR
Working with MIR let's us exclude expressions like `&fn_name as &dyn Something` and `(&fn_name)()`. Also added ABI, unsafety and whether a function is variadic in the lint suggestion, included the `&` in the span of the lint and updated the test.
Diffstat (limited to 'src')
| -rw-r--r-- | src/test/ui/lint/function-references.rs | 75 | ||||
| -rw-r--r-- | src/test/ui/lint/function-references.stderr | 105 |
2 files changed, 156 insertions, 24 deletions
diff --git a/src/test/ui/lint/function-references.rs b/src/test/ui/lint/function-references.rs index 748f2560caa..ad0380375d6 100644 --- a/src/test/ui/lint/function-references.rs +++ b/src/test/ui/lint/function-references.rs @@ -1,22 +1,71 @@ // check-pass -fn foo() -> usize { 42 } -fn bar(x: usize) -> usize { x } -fn baz(x: usize, y: usize) -> usize { x + y } +#![feature(c_variadic)] +#![allow(dead_code)] + +fn foo() -> u32 { 42 } +fn bar(x: u32) -> u32 { x } +fn baz(x: u32, y: u32) -> u32 { x + y } +unsafe fn unsafe_fn() { } +extern "C" fn c_fn() { } +unsafe extern "C" fn unsafe_c_fn() { } +unsafe extern fn variadic_fn(_x: u32, _args: ...) { } +fn call_fn(f: &dyn Fn(u32) -> u32, x: u32) { f(x); } +fn parameterized_call_fn<F: Fn(u32) -> u32>(f: &F, x: u32) { f(x); } fn main() { + let _zst_ref = &foo; + //~^ WARN cast `foo` with `as fn() -> _` to use it as a pointer + let fn_item = foo; + let _indirect_ref = &fn_item; + //~^ WARN cast `fn_item` with `as fn() -> _` to use it as a pointer + let _cast_zst_ptr = &foo as *const _; + //~^ WARN cast `foo` with `as fn() -> _` to use it as a pointer + let _coerced_zst_ptr: *const _ = &foo; + //~^ WARN cast `foo` with `as fn() -> _` to use it as a pointer + + let _zst_ref = &mut foo; + //~^ WARN cast `foo` with `as fn() -> _` to use it as a pointer + let mut mut_fn_item = foo; + let _indirect_ref = &mut mut_fn_item; + //~^ WARN cast `fn_item` with `as fn() -> _` to use it as a pointer + let _cast_zst_ptr = &mut foo as *mut _; + //~^ WARN cast `foo` with `as fn() -> _` to use it as a pointer + let _coerced_zst_ptr: *mut _ = &mut foo; + //~^ WARN cast `foo` with `as fn() -> _` to use it as a pointer + + let _cast_zst_ref = &foo as &dyn Fn() -> u32; + let _coerced_zst_ref: &dyn Fn() -> u32 = &foo; + + let _cast_zst_ref = &mut foo as &mut dyn Fn() -> u32; + let _coerced_zst_ref: &mut dyn Fn() -> u32 = &mut foo; + let _fn_ptr = foo as fn() -> u32; + println!("{:p}", &foo); - //~^ WARN cast `foo` with `as *const fn() -> _` to use it as a pointer + //~^ WARN cast `foo` with as fn() -> _` to use it as a pointer println!("{:p}", &bar); - //~^ WARN cast `bar` with `as *const fn(_) -> _` to use it as a pointer + //~^ WARN cast `bar` with as fn(_) -> _` to use it as a pointer println!("{:p}", &baz); - //~^ WARN cast `baz` with `as *const fn(_, _) -> _` to use it as a pointer + //~^ WARN cast `baz` with as fn(_, _) -> _` to use it as a pointer + println!("{:p}", &unsafe_fn); + //~^ WARN cast `baz` with as unsafe fn()` to use it as a pointer + println!("{:p}", &c_fn); + //~^ WARN cast `baz` with as extern "C" fn()` to use it as a pointer + println!("{:p}", &unsafe_c_fn); + //~^ WARN cast `baz` with as unsafe extern "C" fn()` to use it as a pointer + println!("{:p}", &variadic_fn); + //~^ WARN cast `baz` with as unsafe extern "C" fn(_, ...) -> _` to use it as a pointer + println!("{:p}", &std::env::var::<String>); + //~^ WARN cast `std::env::var` with as fn(_) -> _` to use it as a pointer + + println!("{:p}", foo as fn() -> u32); - //should not produce any warnings - println!("{:p}", foo as *const fn() -> usize); - println!("{:p}", bar as *const fn(usize) -> usize); - println!("{:p}", baz as *const fn(usize, usize) -> usize); + unsafe { + std::mem::transmute::<_, usize>(&foo); + //~^ WARN cast `foo` with as fn() -> _` to use it as a pointer + std::mem::transmute::<_, usize>(foo as fn() -> u32); + } - //should not produce any warnings - let fn_thing = foo; - println!("{:p}", &fn_thing); + (&bar)(1); + call_fn(&bar, 1); + parameterized_call_fn(&bar, 1); } diff --git a/src/test/ui/lint/function-references.stderr b/src/test/ui/lint/function-references.stderr index bd2da6e1167..62dbf7f835d 100644 --- a/src/test/ui/lint/function-references.stderr +++ b/src/test/ui/lint/function-references.stderr @@ -1,22 +1,105 @@ -warning: cast `foo` with `as *const fn() -> _` to use it as a pointer - --> $DIR/function-references.rs:7:23 +warning: cast `foo` with `as fn() -> _` to use it as a pointer + --> $DIR/function-references.rs:16:20 | -LL | println!("{:p}", &foo); - | ^^^ +LL | let _zst_ref = &foo; + | ^^^^ | = note: `#[warn(function_references)]` on by default -warning: cast `bar` with `as *const fn(_) -> _` to use it as a pointer - --> $DIR/function-references.rs:9:23 +warning: cast `fn_item` with `as fn() -> _` to use it as a pointer + --> $DIR/function-references.rs:19:25 + | +LL | let _indirect_ref = &fn_item; + | ^^^^^^^^ + +warning: cast `foo` with `as fn() -> _` to use it as a pointer + --> $DIR/function-references.rs:21:25 + | +LL | let _cast_zst_ptr = &foo as *const _; + | ^^^^ + +warning: cast `foo` with `as fn() -> _` to use it as a pointer + --> $DIR/function-references.rs:23:38 + | +LL | let _coerced_zst_ptr: *const _ = &foo; + | ^^^^ + +warning: cast `foo` with `as fn() -> _` to use it as a pointer + --> $DIR/function-references.rs:26:20 + | +LL | let _zst_ref = &mut foo; + | ^^^^^^^^ + +warning: cast `mut_fn_item` with `as fn() -> _` to use it as a pointer + --> $DIR/function-references.rs:29:25 + | +LL | let _indirect_ref = &mut mut_fn_item; + | ^^^^^^^^^^^^^^^^ + +warning: cast `foo` with `as fn() -> _` to use it as a pointer + --> $DIR/function-references.rs:31:25 + | +LL | let _cast_zst_ptr = &mut foo as *mut _; + | ^^^^^^^^ + +warning: cast `foo` with `as fn() -> _` to use it as a pointer + --> $DIR/function-references.rs:33:36 + | +LL | let _coerced_zst_ptr: *mut _ = &mut foo; + | ^^^^^^^^ + +warning: cast `foo` with `as fn() -> _` to use it as a pointer + --> $DIR/function-references.rs:43:22 + | +LL | println!("{:p}", &foo); + | ^^^^ + +warning: cast `bar` with `as fn(_) -> _` to use it as a pointer + --> $DIR/function-references.rs:45:22 | LL | println!("{:p}", &bar); - | ^^^ + | ^^^^ -warning: cast `baz` with `as *const fn(_, _) -> _` to use it as a pointer - --> $DIR/function-references.rs:11:23 +warning: cast `baz` with `as fn(_, _) -> _` to use it as a pointer + --> $DIR/function-references.rs:47:22 | LL | println!("{:p}", &baz); - | ^^^ + | ^^^^ + +warning: cast `unsafe_fn` with `as unsafe fn()` to use it as a pointer + --> $DIR/function-references.rs:49:22 + | +LL | println!("{:p}", &unsafe_fn); + | ^^^^^^^^^^ + +warning: cast `c_fn` with `as extern "C" fn()` to use it as a pointer + --> $DIR/function-references.rs:51:22 + | +LL | println!("{:p}", &c_fn); + | ^^^^^ -warning: 3 warnings emitted +warning: cast `unsafe_c_fn` with `as unsafe extern "C" fn()` to use it as a pointer + --> $DIR/function-references.rs:53:22 + | +LL | println!("{:p}", &unsafe_c_fn); + | ^^^^^^^^^^^^ + +warning: cast `variadic_fn` with `as unsafe extern "C" fn(_, ...)` to use it as a pointer + --> $DIR/function-references.rs:55:22 + | +LL | println!("{:p}", &variadic_fn); + | ^^^^^^^^^^^^ + +warning: cast `std::env::var` with `as fn(_) -> _` to use it as a pointer + --> $DIR/function-references.rs:57:22 + | +LL | println!("{:p}", &std::env::var::<String>); + | ^^^^^^^^^^^^^^^^^^^^^^^^ + +warning: cast `foo` with `as fn() -> _` to use it as a pointer + --> $DIR/function-references.rs:63:41 + | +LL | std::mem::transmute::<_, usize>(&foo); + | ^^^^ +warning: 17 warnings emitted |
