1
Fork 0

Removed test for unhandled case in function_item_references lint

Removed test for the unhandled case of calls to `fn f<T>(x: &T)` where `x` is a
function reference and is formatted as a pointer in `f`. This compiles since
`&T` implements `Pointer`, but is unlikely to occur in practice. Also tweaked
the lint's wording and modified tests accordingly.
This commit is contained in:
Ayrton 2020-10-06 11:59:14 -04:00
parent 511fe048b4
commit 432ebd57ef
3 changed files with 86 additions and 89 deletions

View file

@ -164,7 +164,7 @@ impl<'a, 'tcx> FunctionItemRefChecker<'a, 'tcx> {
let ret = if fn_sig.output().skip_binder().is_unit() { "" } else { " -> _" }; let ret = if fn_sig.output().skip_binder().is_unit() { "" } else { " -> _" };
self.tcx.struct_span_lint_hir(FUNCTION_ITEM_REFERENCES, lint_root, span, |lint| { self.tcx.struct_span_lint_hir(FUNCTION_ITEM_REFERENCES, lint_root, span, |lint| {
lint.build(&format!( lint.build(&format!(
"cast `{}` with `as {}{}fn({}{}){}` to use it as a pointer", "cast `{}` with `as {}{}fn({}{}){}` to obtain a function pointer",
ident, ident,
unsafety, unsafety,
abi, abi,

View file

@ -10,7 +10,7 @@ fn baz(x: u32, y: u32) -> u32 { x + y }
unsafe fn unsafe_fn() { } unsafe fn unsafe_fn() { }
extern "C" fn c_fn() { } extern "C" fn c_fn() { }
unsafe extern "C" fn unsafe_c_fn() { } unsafe extern "C" fn unsafe_c_fn() { }
unsafe extern fn variadic_fn(_x: u32, _args: ...) { } unsafe extern fn variadic(_x: u32, _args: ...) { }
//function references passed to these functions should never lint //function references passed to these functions should never lint
fn call_fn(f: &dyn Fn(u32) -> u32, x: u32) { f(x); } fn call_fn(f: &dyn Fn(u32) -> u32, x: u32) { f(x); }
@ -20,7 +20,6 @@ fn parameterized_call_fn<F: Fn(u32) -> u32>(f: &F, x: u32) { f(x); }
fn print_ptr<F: Pointer>(f: F) { println!("{:p}", f); } fn print_ptr<F: Pointer>(f: F) { println!("{:p}", f); }
fn bound_by_ptr_trait<F: Pointer>(_f: F) { } fn bound_by_ptr_trait<F: Pointer>(_f: F) { }
fn bound_by_ptr_trait_tuple<F: Pointer, G: Pointer>(_t: (F, G)) { } fn bound_by_ptr_trait_tuple<F: Pointer, G: Pointer>(_t: (F, G)) { }
fn implicit_ptr_trait<F>(f: &F) { println!("{:p}", f); }
fn main() { fn main() {
//`let` bindings with function references shouldn't lint //`let` bindings with function references shouldn't lint
@ -56,47 +55,47 @@ fn main() {
//potential ways to incorrectly try printing function pointers //potential ways to incorrectly try printing function pointers
println!("{:p}", &foo); println!("{:p}", &foo);
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer //~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
print!("{:p}", &foo); print!("{:p}", &foo);
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer //~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
format!("{:p}", &foo); format!("{:p}", &foo);
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer //~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
println!("{:p}", &foo as *const _); println!("{:p}", &foo as *const _);
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer //~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
println!("{:p}", zst_ref); println!("{:p}", zst_ref);
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer //~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
println!("{:p}", cast_zst_ptr); println!("{:p}", cast_zst_ptr);
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer //~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
println!("{:p}", coerced_zst_ptr); println!("{:p}", coerced_zst_ptr);
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer //~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
println!("{:p}", &fn_item); println!("{:p}", &fn_item);
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer //~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
println!("{:p}", indirect_ref); println!("{:p}", indirect_ref);
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer //~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
println!("{:p}", &nop); println!("{:p}", &nop);
//~^ WARNING cast `nop` with `as fn()` to use it as a pointer //~^ WARNING cast `nop` with `as fn()` to obtain a function pointer
println!("{:p}", &bar); println!("{:p}", &bar);
//~^ WARNING cast `bar` with `as fn(_) -> _` to use it as a pointer //~^ WARNING cast `bar` with `as fn(_) -> _` to obtain a function pointer
println!("{:p}", &baz); println!("{:p}", &baz);
//~^ WARNING cast `baz` with `as fn(_, _) -> _` to use it as a pointer //~^ WARNING cast `baz` with `as fn(_, _) -> _` to obtain a function pointer
println!("{:p}", &unsafe_fn); println!("{:p}", &unsafe_fn);
//~^ WARNING cast `unsafe_fn` with `as unsafe fn()` to use it as a pointer //~^ WARNING cast `unsafe_fn` with `as unsafe fn()` to obtain a function pointer
println!("{:p}", &c_fn); println!("{:p}", &c_fn);
//~^ WARNING cast `c_fn` with `as extern "C" fn()` to use it as a pointer //~^ WARNING cast `c_fn` with `as extern "C" fn()` to obtain a function pointer
println!("{:p}", &unsafe_c_fn); println!("{:p}", &unsafe_c_fn);
//~^ WARNING cast `unsafe_c_fn` with `as unsafe extern "C" fn()` to use it as a pointer //~^ WARNING cast `unsafe_c_fn` with `as unsafe extern "C" fn()` to obtain a function pointer
println!("{:p}", &variadic_fn); println!("{:p}", &variadic);
//~^ WARNING cast `variadic_fn` with `as unsafe extern "C" fn(_, ...)` to use it as a pointer //~^ WARNING cast `variadic` with `as unsafe extern "C" fn(_, ...)` to obtain a function pointer
println!("{:p}", &std::env::var::<String>); println!("{:p}", &std::env::var::<String>);
//~^ WARNING cast `var` with `as fn(_) -> _` to use it as a pointer //~^ WARNING cast `var` with `as fn(_) -> _` to obtain a function pointer
println!("{:p} {:p} {:p}", &nop, &foo, &bar); println!("{:p} {:p} {:p}", &nop, &foo, &bar);
//~^ WARNING cast `nop` with `as fn()` to use it as a pointer //~^ WARNING cast `nop` with `as fn()` to obtain a function pointer
//~^^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer //~^^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
//~^^^ WARNING cast `bar` with `as fn(_) -> _` to use it as a pointer //~^^^ WARNING cast `bar` with `as fn(_) -> _` to obtain a function pointer
//using a function reference to call a function shouldn't lint //using a function reference to call a function shouldn't lint
(&bar)(1); (&bar)(1);
@ -109,10 +108,10 @@ fn main() {
unsafe { unsafe {
//potential ways to incorrectly try transmuting function pointers //potential ways to incorrectly try transmuting function pointers
std::mem::transmute::<_, usize>(&foo); std::mem::transmute::<_, usize>(&foo);
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer //~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
std::mem::transmute::<_, (usize, usize)>((&foo, &bar)); std::mem::transmute::<_, (usize, usize)>((&foo, &bar));
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer //~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
//~^^ WARNING cast `bar` with `as fn(_) -> _` to use it as a pointer //~^^ WARNING cast `bar` with `as fn(_) -> _` to obtain a function pointer
//the correct way to transmute function pointers //the correct way to transmute function pointers
std::mem::transmute::<_, usize>(foo as fn() -> u32); std::mem::transmute::<_, usize>(foo as fn() -> u32);
@ -121,14 +120,12 @@ fn main() {
//function references as arguments required to be bound by std::fmt::Pointer should lint //function references as arguments required to be bound by std::fmt::Pointer should lint
print_ptr(&bar); print_ptr(&bar);
//~^ WARNING cast `bar` with `as fn(_) -> _` to use it as a pointer //~^ WARNING cast `bar` with `as fn(_) -> _` to obtain a function pointer
bound_by_ptr_trait(&bar); bound_by_ptr_trait(&bar);
//~^ WARNING cast `bar` with `as fn(_) -> _` to use it as a pointer //~^ WARNING cast `bar` with `as fn(_) -> _` to obtain a function pointer
bound_by_ptr_trait_tuple((&foo, &bar)); bound_by_ptr_trait_tuple((&foo, &bar));
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer //~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
//~^^ WARNING cast `bar` with `as fn(_) -> _` to use it as a pointer //~^^ WARNING cast `bar` with `as fn(_) -> _` to obtain a function pointer
implicit_ptr_trait(&bar);
//~^ WARNING cast `bar` with `as fn(_) -> _` to use it as a pointer
//correct ways to pass function pointers as arguments bound by std::fmt::Pointer //correct ways to pass function pointers as arguments bound by std::fmt::Pointer
print_ptr(bar as fn(u32) -> u32); print_ptr(bar as fn(u32) -> u32);

View file

@ -1,5 +1,5 @@
warning: cast `foo` with `as fn() -> _` to use it as a pointer warning: cast `foo` with `as fn() -> _` to obtain a function pointer
--> $DIR/function-references.rs:58:22 --> $DIR/function-references.rs:57:22
| |
LL | println!("{:p}", &foo); LL | println!("{:p}", &foo);
| ^^^^ | ^^^^
@ -10,158 +10,158 @@ note: the lint level is defined here
LL | #![warn(function_item_references)] LL | #![warn(function_item_references)]
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^
warning: cast `foo` with `as fn() -> _` to use it as a pointer warning: cast `foo` with `as fn() -> _` to obtain a function pointer
--> $DIR/function-references.rs:60:20 --> $DIR/function-references.rs:59:20
| |
LL | print!("{:p}", &foo); LL | print!("{:p}", &foo);
| ^^^^ | ^^^^
warning: cast `foo` with `as fn() -> _` to use it as a pointer warning: cast `foo` with `as fn() -> _` to obtain a function pointer
--> $DIR/function-references.rs:62:21 --> $DIR/function-references.rs:61:21
| |
LL | format!("{:p}", &foo); LL | format!("{:p}", &foo);
| ^^^^ | ^^^^
warning: cast `foo` with `as fn() -> _` to use it as a pointer warning: cast `foo` with `as fn() -> _` to obtain a function pointer
--> $DIR/function-references.rs:65:22 --> $DIR/function-references.rs:64:22
| |
LL | println!("{:p}", &foo as *const _); LL | println!("{:p}", &foo as *const _);
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
warning: cast `foo` with `as fn() -> _` to use it as a pointer warning: cast `foo` with `as fn() -> _` to obtain a function pointer
--> $DIR/function-references.rs:67:22 --> $DIR/function-references.rs:66:22
| |
LL | println!("{:p}", zst_ref); LL | println!("{:p}", zst_ref);
| ^^^^^^^ | ^^^^^^^
warning: cast `foo` with `as fn() -> _` to use it as a pointer warning: cast `foo` with `as fn() -> _` to obtain a function pointer
--> $DIR/function-references.rs:69:22 --> $DIR/function-references.rs:68:22
| |
LL | println!("{:p}", cast_zst_ptr); LL | println!("{:p}", cast_zst_ptr);
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
warning: cast `foo` with `as fn() -> _` to use it as a pointer warning: cast `foo` with `as fn() -> _` to obtain a function pointer
--> $DIR/function-references.rs:71:22 --> $DIR/function-references.rs:70:22
| |
LL | println!("{:p}", coerced_zst_ptr); LL | println!("{:p}", coerced_zst_ptr);
| ^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^
warning: cast `foo` with `as fn() -> _` to use it as a pointer warning: cast `foo` with `as fn() -> _` to obtain a function pointer
--> $DIR/function-references.rs:74:22 --> $DIR/function-references.rs:73:22
| |
LL | println!("{:p}", &fn_item); LL | println!("{:p}", &fn_item);
| ^^^^^^^^ | ^^^^^^^^
warning: cast `foo` with `as fn() -> _` to use it as a pointer warning: cast `foo` with `as fn() -> _` to obtain a function pointer
--> $DIR/function-references.rs:76:22 --> $DIR/function-references.rs:75:22
| |
LL | println!("{:p}", indirect_ref); LL | println!("{:p}", indirect_ref);
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
warning: cast `nop` with `as fn()` to use it as a pointer warning: cast `nop` with `as fn()` to obtain a function pointer
--> $DIR/function-references.rs:79:22 --> $DIR/function-references.rs:78:22
| |
LL | println!("{:p}", &nop); LL | println!("{:p}", &nop);
| ^^^^ | ^^^^
warning: cast `bar` with `as fn(_) -> _` to use it as a pointer warning: cast `bar` with `as fn(_) -> _` to obtain a function pointer
--> $DIR/function-references.rs:81:22 --> $DIR/function-references.rs:80:22
| |
LL | println!("{:p}", &bar); LL | println!("{:p}", &bar);
| ^^^^ | ^^^^
warning: cast `baz` with `as fn(_, _) -> _` to use it as a pointer warning: cast `baz` with `as fn(_, _) -> _` to obtain a function pointer
--> $DIR/function-references.rs:83:22 --> $DIR/function-references.rs:82:22
| |
LL | println!("{:p}", &baz); LL | println!("{:p}", &baz);
| ^^^^ | ^^^^
warning: cast `unsafe_fn` with `as unsafe fn()` to use it as a pointer warning: cast `unsafe_fn` with `as unsafe fn()` to obtain a function pointer
--> $DIR/function-references.rs:85:22 --> $DIR/function-references.rs:84:22
| |
LL | println!("{:p}", &unsafe_fn); LL | println!("{:p}", &unsafe_fn);
| ^^^^^^^^^^ | ^^^^^^^^^^
warning: cast `c_fn` with `as extern "C" fn()` to use it as a pointer warning: cast `c_fn` with `as extern "C" fn()` to obtain a function pointer
--> $DIR/function-references.rs:87:22 --> $DIR/function-references.rs:86:22
| |
LL | println!("{:p}", &c_fn); LL | println!("{:p}", &c_fn);
| ^^^^^ | ^^^^^
warning: cast `unsafe_c_fn` with `as unsafe extern "C" fn()` to use it as a pointer warning: cast `unsafe_c_fn` with `as unsafe extern "C" fn()` to obtain a function pointer
--> $DIR/function-references.rs:89:22 --> $DIR/function-references.rs:88:22
| |
LL | println!("{:p}", &unsafe_c_fn); LL | println!("{:p}", &unsafe_c_fn);
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
warning: cast `variadic_fn` with `as unsafe extern "C" fn(_, ...)` to use it as a pointer warning: cast `variadic` with `as unsafe extern "C" fn(_, ...)` to obtain a function pointer
--> $DIR/function-references.rs:91:22 --> $DIR/function-references.rs:90:22
| |
LL | println!("{:p}", &variadic_fn); LL | println!("{:p}", &variadic);
| ^^^^^^^^^^^^ | ^^^^^^^^^
warning: cast `var` with `as fn(_) -> _` to use it as a pointer warning: cast `var` with `as fn(_) -> _` to obtain a function pointer
--> $DIR/function-references.rs:93:22 --> $DIR/function-references.rs:92:22
| |
LL | println!("{:p}", &std::env::var::<String>); LL | println!("{:p}", &std::env::var::<String>);
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^
warning: cast `nop` with `as fn()` to use it as a pointer warning: cast `nop` with `as fn()` to obtain a function pointer
--> $DIR/function-references.rs:96:32 --> $DIR/function-references.rs:95:32
| |
LL | println!("{:p} {:p} {:p}", &nop, &foo, &bar); LL | println!("{:p} {:p} {:p}", &nop, &foo, &bar);
| ^^^^ | ^^^^
warning: cast `foo` with `as fn() -> _` to use it as a pointer warning: cast `foo` with `as fn() -> _` to obtain a function pointer
--> $DIR/function-references.rs:96:38 --> $DIR/function-references.rs:95:38
| |
LL | println!("{:p} {:p} {:p}", &nop, &foo, &bar); LL | println!("{:p} {:p} {:p}", &nop, &foo, &bar);
| ^^^^ | ^^^^
warning: cast `bar` with `as fn(_) -> _` to use it as a pointer warning: cast `bar` with `as fn(_) -> _` to obtain a function pointer
--> $DIR/function-references.rs:96:44 --> $DIR/function-references.rs:95:44
| |
LL | println!("{:p} {:p} {:p}", &nop, &foo, &bar); LL | println!("{:p} {:p} {:p}", &nop, &foo, &bar);
| ^^^^ | ^^^^
warning: cast `foo` with `as fn() -> _` to use it as a pointer warning: cast `foo` with `as fn() -> _` to obtain a function pointer
--> $DIR/function-references.rs:111:41 --> $DIR/function-references.rs:110:41
| |
LL | std::mem::transmute::<_, usize>(&foo); LL | std::mem::transmute::<_, usize>(&foo);
| ^^^^ | ^^^^
warning: cast `foo` with `as fn() -> _` to use it as a pointer warning: cast `foo` with `as fn() -> _` to obtain a function pointer
--> $DIR/function-references.rs:113:50 --> $DIR/function-references.rs:112:50
| |
LL | std::mem::transmute::<_, (usize, usize)>((&foo, &bar)); LL | std::mem::transmute::<_, (usize, usize)>((&foo, &bar));
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
warning: cast `bar` with `as fn(_) -> _` to use it as a pointer warning: cast `bar` with `as fn(_) -> _` to obtain a function pointer
--> $DIR/function-references.rs:113:50 --> $DIR/function-references.rs:112:50
| |
LL | std::mem::transmute::<_, (usize, usize)>((&foo, &bar)); LL | std::mem::transmute::<_, (usize, usize)>((&foo, &bar));
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
warning: cast `bar` with `as fn(_) -> _` to use it as a pointer warning: cast `bar` with `as fn(_) -> _` to obtain a function pointer
--> $DIR/function-references.rs:123:15 --> $DIR/function-references.rs:122:15
| |
LL | print_ptr(&bar); LL | print_ptr(&bar);
| ^^^^ | ^^^^
warning: cast `bar` with `as fn(_) -> _` to use it as a pointer warning: cast `bar` with `as fn(_) -> _` to obtain a function pointer
--> $DIR/function-references.rs:125:24 --> $DIR/function-references.rs:124:24
| |
LL | bound_by_ptr_trait(&bar); LL | bound_by_ptr_trait(&bar);
| ^^^^ | ^^^^
warning: cast `bar` with `as fn(_) -> _` to use it as a pointer warning: cast `bar` with `as fn(_) -> _` to obtain a function pointer
--> $DIR/function-references.rs:127:30 --> $DIR/function-references.rs:126:30
| |
LL | bound_by_ptr_trait_tuple((&foo, &bar)); LL | bound_by_ptr_trait_tuple((&foo, &bar));
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
warning: cast `foo` with `as fn() -> _` to use it as a pointer warning: cast `foo` with `as fn() -> _` to obtain a function pointer
--> $DIR/function-references.rs:127:30 --> $DIR/function-references.rs:126:30
| |
LL | bound_by_ptr_trait_tuple((&foo, &bar)); LL | bound_by_ptr_trait_tuple((&foo, &bar));
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^