Changed lint to check for std::fmt::Pointer and transmute

The lint checks arguments in calls to `transmute` or functions that have
`Pointer` as a trait bound and displays a warning if the argument is a function
reference. Also checks for `std::fmt::Pointer::fmt` to handle formatting macros
although it doesn't depend on the exact expansion of the macro or formatting
internals. `std::fmt::Pointer` and `std::fmt::Pointer::fmt` were also added as
diagnostic items and symbols.
This commit is contained in:
Ayrton 2020-10-06 09:51:10 -04:00
parent 3214de7359
commit 511fe048b4
7 changed files with 355 additions and 199 deletions

View File

@ -1,134 +1,154 @@
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_middle::mir::visit::Visitor; use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::*; use rustc_middle::mir::*;
use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::ty::{self, subst::GenericArgKind, PredicateAtom, Ty, TyCtxt, TyS};
use rustc_session::lint::builtin::FUNCTION_REFERENCES; use rustc_session::lint::builtin::FUNCTION_ITEM_REFERENCES;
use rustc_span::Span; use rustc_span::{symbol::sym, Span};
use rustc_target::spec::abi::Abi; use rustc_target::spec::abi::Abi;
use crate::transform::{MirPass, MirSource}; use crate::transform::MirPass;
pub struct FunctionReferences; pub struct FunctionItemReferences;
impl<'tcx> MirPass<'tcx> for FunctionReferences { impl<'tcx> MirPass<'tcx> for FunctionItemReferences {
fn run_pass(&self, tcx: TyCtxt<'tcx>, _src: MirSource<'tcx>, body: &mut Body<'tcx>) { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let source_info = SourceInfo::outermost(body.span); let mut checker = FunctionItemRefChecker { tcx, body };
let mut checker = FunctionRefChecker {
tcx,
body,
potential_lints: Vec::new(),
casts: Vec::new(),
calls: Vec::new(),
source_info,
};
checker.visit_body(&body); checker.visit_body(&body);
} }
} }
struct FunctionRefChecker<'a, 'tcx> { struct FunctionItemRefChecker<'a, 'tcx> {
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
body: &'a Body<'tcx>, body: &'a Body<'tcx>,
potential_lints: Vec<FunctionRefLint>,
casts: Vec<Span>,
calls: Vec<Span>,
source_info: SourceInfo,
} }
impl<'a, 'tcx> Visitor<'tcx> for FunctionRefChecker<'a, 'tcx> { impl<'a, 'tcx> Visitor<'tcx> for FunctionItemRefChecker<'a, 'tcx> {
fn visit_basic_block_data(&mut self, block: BasicBlock, data: &BasicBlockData<'tcx>) {
self.super_basic_block_data(block, data);
for cast_span in self.casts.drain(..) {
self.potential_lints.retain(|lint| lint.source_info.span != cast_span);
}
for call_span in self.calls.drain(..) {
self.potential_lints.retain(|lint| lint.source_info.span != call_span);
}
for lint in self.potential_lints.drain(..) {
lint.emit(self.tcx, self.body);
}
}
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
self.source_info = statement.source_info;
self.super_statement(statement, location);
}
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) { fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
self.source_info = terminator.source_info;
if let TerminatorKind::Call { if let TerminatorKind::Call {
func, func,
args: _, args,
destination: _, destination: _,
cleanup: _, cleanup: _,
from_hir_call: _, from_hir_call: _,
fn_span: _, fn_span: _,
} = &terminator.kind } = &terminator.kind
{ {
let span = match func { let func_ty = func.ty(self.body, self.tcx);
Operand::Copy(place) | Operand::Move(place) => { if let ty::FnDef(def_id, substs_ref) = *func_ty.kind() {
self.body.local_decls[place.local].source_info.span //check arguments for `std::mem::transmute`
if self.tcx.is_diagnostic_item(sym::transmute, def_id) {
let arg_ty = args[0].ty(self.body, self.tcx);
for generic_inner_ty in arg_ty.walk() {
if let GenericArgKind::Type(inner_ty) = generic_inner_ty.unpack() {
if let Some(fn_id) = FunctionItemRefChecker::is_fn_ref(inner_ty) {
let ident = self.tcx.item_name(fn_id).to_ident_string();
let source_info = *self.body.source_info(location);
let span = self.nth_arg_span(&args, 0);
self.emit_lint(ident, fn_id, source_info, span);
}
}
}
} else {
//check arguments for any function with `std::fmt::Pointer` as a bound trait
let param_env = self.tcx.param_env(def_id);
let bounds = param_env.caller_bounds();
for bound in bounds {
if let Some(bound_ty) = self.is_pointer_trait(&bound.skip_binders()) {
let arg_defs = self.tcx.fn_sig(def_id).skip_binder().inputs();
for (arg_num, arg_def) in arg_defs.iter().enumerate() {
for generic_inner_ty in arg_def.walk() {
if let GenericArgKind::Type(inner_ty) =
generic_inner_ty.unpack()
{
//if any type reachable from the argument types in the fn sig matches the type bound by `Pointer`
if TyS::same_type(inner_ty, bound_ty) {
//check if this type is a function reference in the function call
let norm_ty =
self.tcx.subst_and_normalize_erasing_regions(
substs_ref, param_env, &inner_ty,
);
if let Some(fn_id) =
FunctionItemRefChecker::is_fn_ref(norm_ty)
{
let ident =
self.tcx.item_name(fn_id).to_ident_string();
let source_info = *self.body.source_info(location);
let span = self.nth_arg_span(&args, arg_num);
self.emit_lint(ident, fn_id, source_info, span);
}
}
}
}
}
}
}
} }
Operand::Constant(constant) => constant.span, }
}; }
self.calls.push(span);
};
self.super_terminator(terminator, location); self.super_terminator(terminator, location);
} }
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) { //check for `std::fmt::Pointer::<T>::fmt` where T is a function reference
match rvalue { //this is used in formatting macros, but doesn't rely on the specific expansion
Rvalue::Ref(_, _, place) | Rvalue::AddressOf(_, place) => { fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
let decl = &self.body.local_decls[place.local]; let op_ty = operand.ty(self.body, self.tcx);
if let ty::FnDef(def_id, _) = decl.ty.kind { if let ty::FnDef(def_id, substs_ref) = *op_ty.kind() {
let ident = self if self.tcx.is_diagnostic_item(sym::pointer_trait_fmt, def_id) {
.body let param_ty = substs_ref.type_at(0);
.var_debug_info if let Some(fn_id) = FunctionItemRefChecker::is_fn_ref(param_ty) {
.iter() let source_info = *self.body.source_info(location);
.find(|info| info.source_info.span == decl.source_info.span) let callsite_ctxt = source_info.span.source_callsite().ctxt();
.map(|info| info.name.to_ident_string()) let span = source_info.span.with_ctxt(callsite_ctxt);
.unwrap_or(self.tcx.def_path_str(def_id)); let ident = self.tcx.item_name(fn_id).to_ident_string();
let lint = FunctionRefLint { ident, def_id, source_info: self.source_info }; self.emit_lint(ident, fn_id, source_info, span);
self.potential_lints.push(lint);
} }
} }
Rvalue::Cast(_, op, _) => {
let op_ty = op.ty(self.body, self.tcx);
if self.is_fn_ref(op_ty) {
self.casts.push(self.source_info.span);
}
}
_ => {}
} }
self.super_rvalue(rvalue, location); self.super_operand(operand, location);
} }
} }
impl<'a, 'tcx> FunctionRefChecker<'a, 'tcx> { impl<'a, 'tcx> FunctionItemRefChecker<'a, 'tcx> {
fn is_fn_ref(&self, ty: Ty<'tcx>) -> bool { //return the bound parameter type if the trait is `std::fmt::Pointer`
let referent_ty = match ty.kind { fn is_pointer_trait(&self, bound: &PredicateAtom<'tcx>) -> Option<Ty<'tcx>> {
if let ty::PredicateAtom::Trait(predicate, _) = bound {
if self.tcx.is_diagnostic_item(sym::pointer_trait, predicate.def_id()) {
Some(predicate.trait_ref.self_ty())
} else {
None
}
} else {
None
}
}
fn is_fn_ref(ty: Ty<'tcx>) -> Option<DefId> {
let referent_ty = match ty.kind() {
ty::Ref(_, referent_ty, _) => Some(referent_ty), ty::Ref(_, referent_ty, _) => Some(referent_ty),
ty::RawPtr(ty_and_mut) => Some(ty_and_mut.ty), ty::RawPtr(ty_and_mut) => Some(&ty_and_mut.ty),
_ => None, _ => None,
}; };
referent_ty referent_ty
.map(|ref_ty| if let ty::FnDef(..) = ref_ty.kind { true } else { false }) .map(
.unwrap_or(false) |ref_ty| {
if let ty::FnDef(def_id, _) = *ref_ty.kind() { Some(def_id) } else { None }
},
)
.unwrap_or(None)
} }
} fn nth_arg_span(&self, args: &Vec<Operand<'tcx>>, n: usize) -> Span {
match &args[n] {
struct FunctionRefLint { Operand::Copy(place) | Operand::Move(place) => {
ident: String, self.body.local_decls[place.local].source_info.span
def_id: DefId, }
source_info: SourceInfo, Operand::Constant(constant) => constant.span,
} }
}
impl<'tcx> FunctionRefLint { fn emit_lint(&self, ident: String, fn_id: DefId, source_info: SourceInfo, span: Span) {
fn emit(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) { let lint_root = self.body.source_scopes[source_info.scope]
let def_id = self.def_id;
let source_info = self.source_info;
let lint_root = body.source_scopes[source_info.scope]
.local_data .local_data
.as_ref() .as_ref()
.assert_crate_local() .assert_crate_local()
.lint_root; .lint_root;
let fn_sig = tcx.fn_sig(def_id); let fn_sig = self.tcx.fn_sig(fn_id);
let unsafety = fn_sig.unsafety().prefix_str(); let unsafety = fn_sig.unsafety().prefix_str();
let abi = match fn_sig.abi() { let abi = match fn_sig.abi() {
Abi::Rust => String::from(""), Abi::Rust => String::from(""),
@ -142,17 +162,17 @@ impl<'tcx> FunctionRefLint {
let num_args = fn_sig.inputs().map_bound(|inputs| inputs.len()).skip_binder(); let num_args = fn_sig.inputs().map_bound(|inputs| inputs.len()).skip_binder();
let variadic = if fn_sig.c_variadic() { ", ..." } else { "" }; let variadic = if fn_sig.c_variadic() { ", ..." } else { "" };
let ret = if fn_sig.output().skip_binder().is_unit() { "" } else { " -> _" }; let ret = if fn_sig.output().skip_binder().is_unit() { "" } else { " -> _" };
tcx.struct_span_lint_hir(FUNCTION_REFERENCES, lint_root, source_info.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 use it as a pointer",
self.ident, ident,
unsafety, unsafety,
abi, abi,
vec!["_"; num_args].join(", "), vec!["_"; num_args].join(", "),
variadic, variadic,
ret, ret,
)) ))
.emit() .emit();
}); });
} }
} }

View File

@ -267,7 +267,7 @@ fn mir_const<'tcx>(
// MIR-level lints. // MIR-level lints.
&check_packed_ref::CheckPackedRef, &check_packed_ref::CheckPackedRef,
&check_const_item_mutation::CheckConstItemMutation, &check_const_item_mutation::CheckConstItemMutation,
&function_references::FunctionReferences, &function_references::FunctionItemReferences,
// What we need to do constant evaluation. // What we need to do constant evaluation.
&simplify::SimplifyCfg::new("initial"), &simplify::SimplifyCfg::new("initial"),
&rustc_peek::SanityCheck, &rustc_peek::SanityCheck,

View File

@ -2645,9 +2645,10 @@ declare_lint! {
reference: "issue #76200 <https://github.com/rust-lang/rust/issues/76200>", reference: "issue #76200 <https://github.com/rust-lang/rust/issues/76200>",
edition: None, edition: None,
}; };
}
declare_lint! { declare_lint! {
pub FUNCTION_REFERENCES, pub FUNCTION_ITEM_REFERENCES,
Warn, Warn,
"suggest casting functions to pointers when attempting to take references", "suggest casting functions to pointers when attempting to take references",
} }
@ -2767,7 +2768,7 @@ declare_lint_pass! {
CONST_EVALUATABLE_UNCHECKED, CONST_EVALUATABLE_UNCHECKED,
INEFFECTIVE_UNSTABLE_TRAIT_IMPL, INEFFECTIVE_UNSTABLE_TRAIT_IMPL,
UNINHABITED_STATIC, UNINHABITED_STATIC,
FUNCTION_REFERENCES, FUNCTION_ITEM_REFERENCES,
] ]
} }

View File

@ -796,6 +796,8 @@ symbols! {
plugin_registrar, plugin_registrar,
plugins, plugins,
pointer, pointer,
pointer_trait,
pointer_trait_fmt,
poll, poll,
position, position,
post_dash_lto: "post-lto", post_dash_lto: "post-lto",

View File

@ -920,9 +920,11 @@ pub trait UpperHex {
/// assert_eq!(&l_ptr[..2], "0x"); /// assert_eq!(&l_ptr[..2], "0x");
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "pointer_trait"]
pub trait Pointer { pub trait Pointer {
/// Formats the value using the given formatter. /// Formats the value using the given formatter.
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "pointer_trait_fmt"]
fn fmt(&self, f: &mut Formatter<'_>) -> Result; fn fmt(&self, f: &mut Formatter<'_>) -> Result;
} }

View File

@ -1,7 +1,9 @@
// check-pass // check-pass
#![feature(c_variadic)] #![feature(c_variadic)]
#![allow(dead_code)] #![warn(function_item_references)]
use std::fmt::Pointer;
fn nop() { }
fn foo() -> u32 { 42 } fn foo() -> u32 { 42 }
fn bar(x: u32) -> u32 { x } fn bar(x: u32) -> u32 { x }
fn baz(x: u32, y: u32) -> u32 { x + y } fn baz(x: u32, y: u32) -> u32 { x + y }
@ -9,63 +11,127 @@ 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_fn(_x: u32, _args: ...) { }
//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); }
fn parameterized_call_fn<F: Fn(u32) -> u32>(f: &F, x: u32) { f(x); } fn parameterized_call_fn<F: Fn(u32) -> u32>(f: &F, x: u32) { f(x); }
fn main() { //function references passed to these functions should lint
let _zst_ref = &foo; fn print_ptr<F: Pointer>(f: F) { println!("{:p}", f); }
//~^ WARN cast `foo` with `as fn() -> _` to use it as a pointer fn bound_by_ptr_trait<F: Pointer>(_f: F) { }
let fn_item = foo; fn bound_by_ptr_trait_tuple<F: Pointer, G: Pointer>(_t: (F, G)) { }
let _indirect_ref = &fn_item; fn implicit_ptr_trait<F>(f: &F) { println!("{:p}", f); }
//~^ 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; fn main() {
//~^ WARN cast `foo` with `as fn() -> _` to use it as a pointer //`let` bindings with function references shouldn't lint
let _ = &foo;
let _ = &mut foo;
let zst_ref = &foo;
let fn_item = foo;
let indirect_ref = &fn_item;
let _mut_zst_ref = &mut foo;
let mut mut_fn_item = foo; let mut mut_fn_item = foo;
let _indirect_ref = &mut mut_fn_item; let _mut_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 _; 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;
let _coerced_zst_ptr: *mut _ = &mut foo;
//~^ WARN cast `foo` with `as fn() -> _` to use it as a pointer let _mut_cast_zst_ptr = &mut foo as *mut _;
let _mut_coerced_zst_ptr: *mut _ = &mut foo;
let _cast_zst_ref = &foo as &dyn Fn() -> u32; let _cast_zst_ref = &foo as &dyn Fn() -> u32;
let _coerced_zst_ref: &dyn Fn() -> u32 = &foo; let _coerced_zst_ref: &dyn Fn() -> u32 = &foo;
let _cast_zst_ref = &mut foo as &mut dyn Fn() -> u32; let _mut_cast_zst_ref = &mut foo as &mut dyn Fn() -> u32;
let _coerced_zst_ref: &mut dyn Fn() -> u32 = &mut foo; let _mut_coerced_zst_ref: &mut dyn Fn() -> u32 = &mut foo;
let _fn_ptr = foo as fn() -> u32;
println!("{:p}", &foo); //the suggested way to cast to a function pointer
//~^ WARN cast `foo` with as fn() -> _` to use it as a pointer let fn_ptr = foo as fn() -> u32;
println!("{:p}", &bar);
//~^ WARN cast `bar` with as fn(_) -> _` to use it as a pointer
println!("{:p}", &baz);
//~^ 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
//correct ways to print function pointers
println!("{:p}", foo as fn() -> u32); println!("{:p}", foo as fn() -> u32);
println!("{:p}", fn_ptr);
unsafe { //potential ways to incorrectly try printing function pointers
std::mem::transmute::<_, usize>(&foo); println!("{:p}", &foo);
//~^ WARN cast `foo` with as fn() -> _` to use it as a pointer //~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
std::mem::transmute::<_, usize>(foo as fn() -> u32); print!("{:p}", &foo);
} //~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
format!("{:p}", &foo);
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
println!("{:p}", &foo as *const _);
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
println!("{:p}", zst_ref);
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
println!("{:p}", cast_zst_ptr);
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
println!("{:p}", coerced_zst_ptr);
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
println!("{:p}", &fn_item);
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
println!("{:p}", indirect_ref);
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
println!("{:p}", &nop);
//~^ WARNING cast `nop` with `as fn()` to use it as a pointer
println!("{:p}", &bar);
//~^ WARNING cast `bar` with `as fn(_) -> _` to use it as a pointer
println!("{:p}", &baz);
//~^ WARNING cast `baz` with `as fn(_, _) -> _` to use it as a pointer
println!("{:p}", &unsafe_fn);
//~^ WARNING cast `unsafe_fn` with `as unsafe fn()` to use it as a pointer
println!("{:p}", &c_fn);
//~^ WARNING cast `c_fn` with `as extern "C" fn()` to use it as a pointer
println!("{:p}", &unsafe_c_fn);
//~^ WARNING cast `unsafe_c_fn` with `as unsafe extern "C" fn()` to use it as a pointer
println!("{:p}", &variadic_fn);
//~^ WARNING cast `variadic_fn` with `as unsafe extern "C" fn(_, ...)` to use it as a pointer
println!("{:p}", &std::env::var::<String>);
//~^ WARNING cast `var` with `as fn(_) -> _` to use it as a pointer
println!("{:p} {:p} {:p}", &nop, &foo, &bar);
//~^ WARNING cast `nop` with `as fn()` to use it as a pointer
//~^^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
//~^^^ WARNING cast `bar` with `as fn(_) -> _` to use it as a pointer
//using a function reference to call a function shouldn't lint
(&bar)(1); (&bar)(1);
//passing a function reference to an arbitrary function shouldn't lint
call_fn(&bar, 1); call_fn(&bar, 1);
parameterized_call_fn(&bar, 1); parameterized_call_fn(&bar, 1);
std::mem::size_of_val(&foo);
unsafe {
//potential ways to incorrectly try transmuting function pointers
std::mem::transmute::<_, usize>(&foo);
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
std::mem::transmute::<_, (usize, usize)>((&foo, &bar));
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
//~^^ WARNING cast `bar` with `as fn(_) -> _` to use it as a pointer
//the correct way to transmute function pointers
std::mem::transmute::<_, usize>(foo as fn() -> u32);
std::mem::transmute::<_, (usize, usize)>((foo as fn() -> u32, bar as fn(u32) -> u32));
}
//function references as arguments required to be bound by std::fmt::Pointer should lint
print_ptr(&bar);
//~^ WARNING cast `bar` with `as fn(_) -> _` to use it as a pointer
bound_by_ptr_trait(&bar);
//~^ WARNING cast `bar` with `as fn(_) -> _` to use it as a pointer
bound_by_ptr_trait_tuple((&foo, &bar));
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
//~^^ WARNING cast `bar` with `as fn(_) -> _` to use it as a 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
print_ptr(bar as fn(u32) -> u32);
bound_by_ptr_trait(bar as fn(u32) -> u32);
bound_by_ptr_trait_tuple((foo as fn() -> u32, bar as fn(u32) -> u32));
} }

View File

@ -1,105 +1,170 @@
warning: cast `foo` with `as fn() -> _` to use it as a pointer warning: cast `foo` with `as fn() -> _` to use it as a pointer
--> $DIR/function-references.rs:16:20 --> $DIR/function-references.rs:58:22
|
LL | let _zst_ref = &foo;
| ^^^^
|
= note: `#[warn(function_references)]` on by default
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); LL | println!("{:p}", &foo);
| ^^^^ | ^^^^
|
note: the lint level is defined here
--> $DIR/function-references.rs:3:9
|
LL | #![warn(function_item_references)]
| ^^^^^^^^^^^^^^^^^^^^^^^^
warning: cast `foo` with `as fn() -> _` to use it as a pointer
--> $DIR/function-references.rs:60:20
|
LL | print!("{:p}", &foo);
| ^^^^
warning: cast `foo` with `as fn() -> _` to use it as a pointer
--> $DIR/function-references.rs:62:21
|
LL | format!("{:p}", &foo);
| ^^^^
warning: cast `foo` with `as fn() -> _` to use it as a pointer
--> $DIR/function-references.rs:65:22
|
LL | println!("{:p}", &foo as *const _);
| ^^^^^^^^^^^^^^^^
warning: cast `foo` with `as fn() -> _` to use it as a pointer
--> $DIR/function-references.rs:67:22
|
LL | println!("{:p}", zst_ref);
| ^^^^^^^
warning: cast `foo` with `as fn() -> _` to use it as a pointer
--> $DIR/function-references.rs:69:22
|
LL | println!("{:p}", cast_zst_ptr);
| ^^^^^^^^^^^^
warning: cast `foo` with `as fn() -> _` to use it as a pointer
--> $DIR/function-references.rs:71:22
|
LL | println!("{:p}", coerced_zst_ptr);
| ^^^^^^^^^^^^^^^
warning: cast `foo` with `as fn() -> _` to use it as a pointer
--> $DIR/function-references.rs:74:22
|
LL | println!("{:p}", &fn_item);
| ^^^^^^^^
warning: cast `foo` with `as fn() -> _` to use it as a pointer
--> $DIR/function-references.rs:76:22
|
LL | println!("{:p}", indirect_ref);
| ^^^^^^^^^^^^
warning: cast `nop` with `as fn()` to use it as a pointer
--> $DIR/function-references.rs:79:22
|
LL | println!("{:p}", &nop);
| ^^^^
warning: cast `bar` with `as fn(_) -> _` to use it as a pointer warning: cast `bar` with `as fn(_) -> _` to use it as a pointer
--> $DIR/function-references.rs:45:22 --> $DIR/function-references.rs:81: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 use it as a pointer
--> $DIR/function-references.rs:47:22 --> $DIR/function-references.rs:83: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 use it as a pointer
--> $DIR/function-references.rs:49:22 --> $DIR/function-references.rs:85: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 use it as a pointer
--> $DIR/function-references.rs:51:22 --> $DIR/function-references.rs:87: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 use it as a pointer
--> $DIR/function-references.rs:53:22 --> $DIR/function-references.rs:89: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_fn` with `as unsafe extern "C" fn(_, ...)` to use it as a pointer
--> $DIR/function-references.rs:55:22 --> $DIR/function-references.rs:91:22
| |
LL | println!("{:p}", &variadic_fn); LL | println!("{:p}", &variadic_fn);
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
warning: cast `std::env::var` with `as fn(_) -> _` to use it as a pointer warning: cast `var` with `as fn(_) -> _` to use it as a pointer
--> $DIR/function-references.rs:57:22 --> $DIR/function-references.rs:93: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
--> $DIR/function-references.rs:96:32
|
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 use it as a pointer
--> $DIR/function-references.rs:63:41 --> $DIR/function-references.rs:96:38
|
LL | println!("{:p} {:p} {:p}", &nop, &foo, &bar);
| ^^^^
warning: cast `bar` with `as fn(_) -> _` to use it as a pointer
--> $DIR/function-references.rs:96:44
|
LL | println!("{:p} {:p} {:p}", &nop, &foo, &bar);
| ^^^^
warning: cast `foo` with `as fn() -> _` to use it as a pointer
--> $DIR/function-references.rs:111:41
| |
LL | std::mem::transmute::<_, usize>(&foo); LL | std::mem::transmute::<_, usize>(&foo);
| ^^^^ | ^^^^
warning: 17 warnings emitted warning: cast `foo` with `as fn() -> _` to use it as a pointer
--> $DIR/function-references.rs:113:50
|
LL | std::mem::transmute::<_, (usize, usize)>((&foo, &bar));
| ^^^^^^^^^^^^
warning: cast `bar` with `as fn(_) -> _` to use it as a pointer
--> $DIR/function-references.rs:113:50
|
LL | std::mem::transmute::<_, (usize, usize)>((&foo, &bar));
| ^^^^^^^^^^^^
warning: cast `bar` with `as fn(_) -> _` to use it as a pointer
--> $DIR/function-references.rs:123:15
|
LL | print_ptr(&bar);
| ^^^^
warning: cast `bar` with `as fn(_) -> _` to use it as a pointer
--> $DIR/function-references.rs:125:24
|
LL | bound_by_ptr_trait(&bar);
| ^^^^
warning: cast `bar` with `as fn(_) -> _` to use it as a pointer
--> $DIR/function-references.rs:127:30
|
LL | bound_by_ptr_trait_tuple((&foo, &bar));
| ^^^^^^^^^^^^
warning: cast `foo` with `as fn() -> _` to use it as a pointer
--> $DIR/function-references.rs:127:30
|
LL | bound_by_ptr_trait_tuple((&foo, &bar));
| ^^^^^^^^^^^^
warning: 27 warnings emitted