diff --git a/compiler/rustc_driver_impl/src/signal_handler.rs b/compiler/rustc_driver_impl/src/signal_handler.rs index e7bc57c9749b5..3d5df19bf67ee 100644 --- a/compiler/rustc_driver_impl/src/signal_handler.rs +++ b/compiler/rustc_driver_impl/src/signal_handler.rs @@ -152,7 +152,8 @@ pub(super) fn install() { libc::sigaltstack(&alt_stack, ptr::null_mut()); let mut sa: libc::sigaction = mem::zeroed(); - sa.sa_sigaction = print_stack_trace as libc::sighandler_t; + sa.sa_sigaction = + print_stack_trace as unsafe extern "C" fn(libc::c_int) as libc::sighandler_t; sa.sa_flags = libc::SA_NODEFER | libc::SA_RESETHAND | libc::SA_ONSTACK; libc::sigemptyset(&mut sa.sa_mask); for (signum, _signame) in KILL_SIGNALS { diff --git a/compiler/rustc_lint/messages.ftl b/compiler/rustc_lint/messages.ftl index 8aa90c070acd3..c996181354b92 100644 --- a/compiler/rustc_lint/messages.ftl +++ b/compiler/rustc_lint/messages.ftl @@ -265,6 +265,9 @@ lint_forgetting_copy_types = calls to `std::mem::forget` with a value that imple lint_forgetting_references = calls to `std::mem::forget` with a reference instead of an owned value does nothing .label = argument has type `{$arg_ty}` +lint_function_casts_as_integer = direct cast of function item into an integer + .cast_as_fn = first cast to a pointer `as *const ()` + lint_hidden_glob_reexport = private item shadows public glob re-export .note_glob_reexport = the name `{$name}` in the {$namespace} namespace is supposed to be publicly re-exported here .note_private_item = but the private item here shadows it diff --git a/compiler/rustc_lint/src/function_cast_as_integer.rs b/compiler/rustc_lint/src/function_cast_as_integer.rs new file mode 100644 index 0000000000000..2a4ff1f63db52 --- /dev/null +++ b/compiler/rustc_lint/src/function_cast_as_integer.rs @@ -0,0 +1,63 @@ +use rustc_hir as hir; +use rustc_middle::ty; +use rustc_session::{declare_lint, declare_lint_pass}; +use rustc_span::BytePos; + +use crate::lints::{FunctionCastsAsIntegerDiag, FunctionCastsAsIntegerSugg}; +use crate::{LateContext, LateLintPass}; + +declare_lint! { + /// The `function_casts_as_integer` lint detects cases where a function item is cast + /// to an integer. + /// + /// ### Example + /// + /// ```rust + /// fn foo() {} + /// let x = foo as usize; + /// ``` + /// + /// {{produces}} + /// + /// ### Explanation + /// + /// When casting a function item to an integer, it implicitly creates a + /// function pointer that will in turn be cast to an integer. By making + /// it explicit, it improves readability of the code and prevents bugs. + pub FUNCTION_CASTS_AS_INTEGER, + Warn, + "casting a function into an integer", +} + +declare_lint_pass!( + /// Lint for casts of functions into integers. + FunctionCastsAsInteger => [FUNCTION_CASTS_AS_INTEGER] +); + +impl<'tcx> LateLintPass<'tcx> for FunctionCastsAsInteger { + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) { + let hir::ExprKind::Cast(cast_from_expr, cast_to_expr) = expr.kind else { return }; + let cast_to_ty = cx.typeck_results().expr_ty(expr); + // Casting to a function (pointer?), so all good. + // + // Normally, only casts to integers is possible, but if it ever changed, this condition + // will likely need to be updated. + if matches!(cast_to_ty.kind(), ty::FnDef(..) | ty::FnPtr(..) | ty::RawPtr(..)) { + return; + } + let cast_from_ty = cx.typeck_results().expr_ty(cast_from_expr); + if matches!(cast_from_ty.kind(), ty::FnDef(..)) { + cx.tcx.emit_node_span_lint( + FUNCTION_CASTS_AS_INTEGER, + expr.hir_id, + cast_to_expr.span.with_lo(cast_from_expr.span.hi() + BytePos(1)), + FunctionCastsAsIntegerDiag { + sugg: FunctionCastsAsIntegerSugg { + suggestion: cast_from_expr.span.shrink_to_hi(), + cast_to_ty, + }, + }, + ); + } + } +} diff --git a/compiler/rustc_lint/src/lib.rs b/compiler/rustc_lint/src/lib.rs index 8a83434e10c15..7c3a81e891300 100644 --- a/compiler/rustc_lint/src/lib.rs +++ b/compiler/rustc_lint/src/lib.rs @@ -45,6 +45,7 @@ mod errors; mod expect; mod for_loops_over_fallibles; mod foreign_modules; +mod function_cast_as_integer; mod if_let_rescope; mod impl_trait_overcaptures; mod internal; @@ -89,6 +90,7 @@ use deref_into_dyn_supertrait::*; use drop_forget_useless::*; use enum_intrinsics_non_enums::EnumIntrinsicsNonEnums; use for_loops_over_fallibles::*; +use function_cast_as_integer::*; use if_let_rescope::IfLetRescope; use impl_trait_overcaptures::ImplTraitOvercaptures; use internal::*; @@ -241,6 +243,7 @@ late_lint_methods!( IfLetRescope: IfLetRescope::default(), StaticMutRefs: StaticMutRefs, UnqualifiedLocalImports: UnqualifiedLocalImports, + FunctionCastsAsInteger: FunctionCastsAsInteger, CheckTransmutes: CheckTransmutes, LifetimeSyntax: LifetimeSyntax, ] diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index c55f2b9dd6f24..dc50f61f741d6 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -3019,6 +3019,26 @@ pub(crate) struct ReservedMultihash { pub suggestion: Span, } +#[derive(LintDiagnostic)] +#[diag(lint_function_casts_as_integer)] +pub(crate) struct FunctionCastsAsIntegerDiag<'tcx> { + #[subdiagnostic] + pub(crate) sugg: FunctionCastsAsIntegerSugg<'tcx>, +} + +#[derive(Subdiagnostic)] +#[suggestion( + lint_cast_as_fn, + code = " as *const ()", + applicability = "machine-applicable", + style = "verbose" +)] +pub(crate) struct FunctionCastsAsIntegerSugg<'tcx> { + #[primary_span] + pub suggestion: Span, + pub cast_to_ty: Ty<'tcx>, +} + #[derive(Debug)] pub(crate) struct MismatchedLifetimeSyntaxes { pub inputs: LifetimeSyntaxCategories>, diff --git a/compiler/rustc_session/src/filesearch.rs b/compiler/rustc_session/src/filesearch.rs index f64fa86948c8d..12b294e124c8d 100644 --- a/compiler/rustc_session/src/filesearch.rs +++ b/compiler/rustc_session/src/filesearch.rs @@ -72,7 +72,7 @@ fn current_dll_path() -> Result { #[cfg(not(target_os = "aix"))] unsafe { - let addr = current_dll_path as usize as *mut _; + let addr = current_dll_path as fn() -> Result as *mut _; let mut info = std::mem::zeroed(); if libc::dladdr(addr, &mut info) == 0 { return Err("dladdr failed".into()); @@ -151,7 +151,10 @@ fn current_dll_path() -> Result { unsafe { GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, - PCWSTR(current_dll_path as *mut u16), + PCWSTR( + current_dll_path as fn() -> Result + as *mut u16, + ), &mut module, ) } diff --git a/library/core/src/intrinsics/mod.rs b/library/core/src/intrinsics/mod.rs index 4f6a425a09839..6b13b4c4f56cc 100644 --- a/library/core/src/intrinsics/mod.rs +++ b/library/core/src/intrinsics/mod.rs @@ -622,7 +622,7 @@ pub const fn forget(_: T); /// // Crucially, we `as`-cast to a raw pointer before `transmute`ing to a function pointer. /// // This avoids an integer-to-pointer `transmute`, which can be problematic. /// // Transmuting between raw pointers and function pointers (i.e., two pointer types) is fine. -/// let pointer = foo as *const (); +/// let pointer = foo as fn() -> i32 as *const (); /// let function = unsafe { /// std::mem::transmute::<*const (), fn() -> i32>(pointer) /// }; diff --git a/library/coretests/tests/ptr.rs b/library/coretests/tests/ptr.rs index 4d5138d539b95..555a3b01f1fcb 100644 --- a/library/coretests/tests/ptr.rs +++ b/library/coretests/tests/ptr.rs @@ -565,6 +565,7 @@ fn ptr_metadata() { #[test] fn ptr_metadata_bounds() { + #[allow(unknown_lints, function_casts_as_integer)] fn metadata_eq_method_address() -> usize { // The `Metadata` associated type has an `Ord` bound, so this is valid: <::Metadata as PartialEq>::eq as usize diff --git a/library/panic_unwind/src/seh.rs b/library/panic_unwind/src/seh.rs index a5d67dbb6a9f4..257916c4d5cdc 100644 --- a/library/panic_unwind/src/seh.rs +++ b/library/panic_unwind/src/seh.rs @@ -336,6 +336,7 @@ unsafe fn throw_exception(data: Option>) -> ! { // In any case, we basically need to do something like this until we can // express more operations in statics (and we may never be able to). unsafe { + #[allow(function_casts_as_integer)] atomic_store::<_, { AtomicOrdering::SeqCst }>( (&raw mut THROW_INFO.pmfnUnwind).cast(), ptr_t::new(exception_cleanup as *mut u8).raw(), @@ -352,6 +353,7 @@ unsafe fn throw_exception(data: Option>) -> ! { (&raw mut CATCHABLE_TYPE.pType).cast(), ptr_t::new((&raw mut TYPE_DESCRIPTOR).cast()).raw(), ); + #[allow(function_casts_as_integer)] atomic_store::<_, { AtomicOrdering::SeqCst }>( (&raw mut CATCHABLE_TYPE.copyFunction).cast(), ptr_t::new(exception_copy as *mut u8).raw(), diff --git a/library/std/src/backtrace.rs b/library/std/src/backtrace.rs index c3fcb0e2e42b0..99724e29e02b2 100644 --- a/library/std/src/backtrace.rs +++ b/library/std/src/backtrace.rs @@ -293,7 +293,7 @@ impl Backtrace { if !Backtrace::enabled() { return Backtrace { inner: Inner::Disabled }; } - Backtrace::create(Backtrace::capture as usize) + Backtrace::create(Backtrace::capture as fn() -> Backtrace as usize) } /// Forcibly captures a full backtrace, regardless of environment variable @@ -309,7 +309,7 @@ impl Backtrace { #[stable(feature = "backtrace", since = "1.65.0")] #[inline(never)] // want to make sure there's a frame here to remove pub fn force_capture() -> Backtrace { - Backtrace::create(Backtrace::force_capture as usize) + Backtrace::create(Backtrace::force_capture as fn() -> Backtrace as usize) } /// Forcibly captures a disabled backtrace, regardless of environment diff --git a/library/std/src/sys/pal/unix/stack_overflow.rs b/library/std/src/sys/pal/unix/stack_overflow.rs index 062f031189fc4..226b6bce01b58 100644 --- a/library/std/src/sys/pal/unix/stack_overflow.rs +++ b/library/std/src/sys/pal/unix/stack_overflow.rs @@ -174,7 +174,9 @@ mod imp { } action.sa_flags = SA_SIGINFO | SA_ONSTACK; - action.sa_sigaction = signal_handler as sighandler_t; + action.sa_sigaction = signal_handler + as unsafe extern "C" fn(i32, *mut libc::siginfo_t, *mut libc::c_void) + as sighandler_t; // SAFETY: only overriding signals if the default is set unsafe { sigaction(signal, &action, ptr::null_mut()) }; } diff --git a/library/std/src/sys/pal/windows/compat.rs b/library/std/src/sys/pal/windows/compat.rs index 14f2c8d881cf1..c465ceb2301ce 100644 --- a/library/std/src/sys/pal/windows/compat.rs +++ b/library/std/src/sys/pal/windows/compat.rs @@ -155,7 +155,7 @@ macro_rules! compat_fn_with_fallback { /// When that is called it attempts to load the requested symbol. /// If it succeeds, `PTR` is set to the address of that symbol. /// If it fails, then `PTR` is set to `fallback`. - static PTR: Atomic<*mut c_void> = AtomicPtr::new(load as *mut _); + static PTR: Atomic<*mut c_void> = AtomicPtr::new(load as unsafe extern "system" fn($($argname: $argtype),*) -> $rettype as *mut _); unsafe extern "system" fn load($($argname: $argtype),*) -> $rettype { unsafe { @@ -171,7 +171,7 @@ macro_rules! compat_fn_with_fallback { PTR.store(f.as_ptr(), Ordering::Relaxed); mem::transmute(f) } else { - PTR.store(fallback as *mut _, Ordering::Relaxed); + PTR.store(fallback as unsafe extern "system" fn($($argname: $argtype),*) -> $rettype as *mut _, Ordering::Relaxed); fallback } } diff --git a/src/tools/clippy/tests/ui/cast_enum_constructor.rs b/src/tools/clippy/tests/ui/cast_enum_constructor.rs index eecf56f71a334..6d70387b6ce29 100644 --- a/src/tools/clippy/tests/ui/cast_enum_constructor.rs +++ b/src/tools/clippy/tests/ui/cast_enum_constructor.rs @@ -1,5 +1,5 @@ #![warn(clippy::cast_enum_constructor)] -#![allow(clippy::fn_to_numeric_cast)] +#![allow(clippy::fn_to_numeric_cast, function_casts_as_integer)] fn main() { enum Foo { diff --git a/src/tools/clippy/tests/ui/confusing_method_to_numeric_cast.fixed b/src/tools/clippy/tests/ui/confusing_method_to_numeric_cast.fixed index e698b99edd5c0..b9691a3284ac0 100644 --- a/src/tools/clippy/tests/ui/confusing_method_to_numeric_cast.fixed +++ b/src/tools/clippy/tests/ui/confusing_method_to_numeric_cast.fixed @@ -1,5 +1,6 @@ #![feature(float_minimum_maximum)] #![warn(clippy::confusing_method_to_numeric_cast)] +#![allow(function_casts_as_integer)] fn main() { let _ = u16::MAX as usize; //~ confusing_method_to_numeric_cast diff --git a/src/tools/clippy/tests/ui/confusing_method_to_numeric_cast.rs b/src/tools/clippy/tests/ui/confusing_method_to_numeric_cast.rs index ef65c21563d98..b402cbf92cb2b 100644 --- a/src/tools/clippy/tests/ui/confusing_method_to_numeric_cast.rs +++ b/src/tools/clippy/tests/ui/confusing_method_to_numeric_cast.rs @@ -1,5 +1,6 @@ #![feature(float_minimum_maximum)] #![warn(clippy::confusing_method_to_numeric_cast)] +#![allow(function_casts_as_integer)] fn main() { let _ = u16::max as usize; //~ confusing_method_to_numeric_cast diff --git a/src/tools/clippy/tests/ui/confusing_method_to_numeric_cast.stderr b/src/tools/clippy/tests/ui/confusing_method_to_numeric_cast.stderr index ba90df2059af6..0d5e08f880773 100644 --- a/src/tools/clippy/tests/ui/confusing_method_to_numeric_cast.stderr +++ b/src/tools/clippy/tests/ui/confusing_method_to_numeric_cast.stderr @@ -1,5 +1,5 @@ error: casting function pointer `u16::max` to `usize` - --> tests/ui/confusing_method_to_numeric_cast.rs:5:13 + --> tests/ui/confusing_method_to_numeric_cast.rs:6:13 | LL | let _ = u16::max as usize; | ^^^^^^^^^^^^^^^^^ @@ -13,7 +13,7 @@ LL + let _ = u16::MAX as usize; | error: casting function pointer `u16::min` to `usize` - --> tests/ui/confusing_method_to_numeric_cast.rs:6:13 + --> tests/ui/confusing_method_to_numeric_cast.rs:7:13 | LL | let _ = u16::min as usize; | ^^^^^^^^^^^^^^^^^ @@ -25,7 +25,7 @@ LL + let _ = u16::MIN as usize; | error: casting function pointer `u16::max_value` to `usize` - --> tests/ui/confusing_method_to_numeric_cast.rs:7:13 + --> tests/ui/confusing_method_to_numeric_cast.rs:8:13 | LL | let _ = u16::max_value as usize; | ^^^^^^^^^^^^^^^^^^^^^^^ @@ -37,7 +37,7 @@ LL + let _ = u16::MAX as usize; | error: casting function pointer `u16::min_value` to `usize` - --> tests/ui/confusing_method_to_numeric_cast.rs:8:13 + --> tests/ui/confusing_method_to_numeric_cast.rs:9:13 | LL | let _ = u16::min_value as usize; | ^^^^^^^^^^^^^^^^^^^^^^^ @@ -49,7 +49,7 @@ LL + let _ = u16::MIN as usize; | error: casting function pointer `f32::maximum` to `usize` - --> tests/ui/confusing_method_to_numeric_cast.rs:10:13 + --> tests/ui/confusing_method_to_numeric_cast.rs:11:13 | LL | let _ = f32::maximum as usize; | ^^^^^^^^^^^^^^^^^^^^^ @@ -61,7 +61,7 @@ LL + let _ = f32::MAX as usize; | error: casting function pointer `f32::max` to `usize` - --> tests/ui/confusing_method_to_numeric_cast.rs:11:13 + --> tests/ui/confusing_method_to_numeric_cast.rs:12:13 | LL | let _ = f32::max as usize; | ^^^^^^^^^^^^^^^^^ @@ -73,7 +73,7 @@ LL + let _ = f32::MAX as usize; | error: casting function pointer `f32::minimum` to `usize` - --> tests/ui/confusing_method_to_numeric_cast.rs:12:13 + --> tests/ui/confusing_method_to_numeric_cast.rs:13:13 | LL | let _ = f32::minimum as usize; | ^^^^^^^^^^^^^^^^^^^^^ @@ -85,7 +85,7 @@ LL + let _ = f32::MIN as usize; | error: casting function pointer `f32::min` to `usize` - --> tests/ui/confusing_method_to_numeric_cast.rs:13:13 + --> tests/ui/confusing_method_to_numeric_cast.rs:14:13 | LL | let _ = f32::min as usize; | ^^^^^^^^^^^^^^^^^ diff --git a/src/tools/clippy/tests/ui/derive.rs b/src/tools/clippy/tests/ui/derive.rs index 6b60421c35490..036f6c444b649 100644 --- a/src/tools/clippy/tests/ui/derive.rs +++ b/src/tools/clippy/tests/ui/derive.rs @@ -9,7 +9,6 @@ #![expect(incomplete_features)] // `unsafe_fields` is incomplete for the time being #![feature(unsafe_fields)] // `clone()` cannot be derived automatically on unsafe fields - #[derive(Copy)] struct Qux; diff --git a/src/tools/clippy/tests/ui/derive.stderr b/src/tools/clippy/tests/ui/derive.stderr index 2b97a58e9d6f6..2701680e788de 100644 --- a/src/tools/clippy/tests/ui/derive.stderr +++ b/src/tools/clippy/tests/ui/derive.stderr @@ -1,5 +1,5 @@ error: you are implementing `Clone` explicitly on a `Copy` type - --> tests/ui/derive.rs:16:1 + --> tests/ui/derive.rs:15:1 | LL | / impl Clone for Qux { LL | | @@ -14,7 +14,7 @@ LL | | } = help: to override `-D warnings` add `#[allow(clippy::expl_impl_clone_on_copy)]` error: you are implementing `Clone` explicitly on a `Copy` type - --> tests/ui/derive.rs:42:1 + --> tests/ui/derive.rs:41:1 | LL | / impl<'a> Clone for Lt<'a> { LL | | @@ -27,7 +27,7 @@ LL | | } = help: consider deriving `Clone` or removing `Copy` error: you are implementing `Clone` explicitly on a `Copy` type - --> tests/ui/derive.rs:55:1 + --> tests/ui/derive.rs:54:1 | LL | / impl Clone for BigArray { LL | | @@ -40,7 +40,7 @@ LL | | } = help: consider deriving `Clone` or removing `Copy` error: you are implementing `Clone` explicitly on a `Copy` type - --> tests/ui/derive.rs:68:1 + --> tests/ui/derive.rs:67:1 | LL | / impl Clone for FnPtr { LL | | @@ -53,7 +53,7 @@ LL | | } = help: consider deriving `Clone` or removing `Copy` error: you are implementing `Clone` explicitly on a `Copy` type - --> tests/ui/derive.rs:90:1 + --> tests/ui/derive.rs:89:1 | LL | / impl Clone for Generic2 { LL | | diff --git a/src/tools/clippy/tests/ui/fn_to_numeric_cast.64bit.stderr b/src/tools/clippy/tests/ui/fn_to_numeric_cast.64bit.stderr index 48961d14f2bba..694690ae5bfa2 100644 --- a/src/tools/clippy/tests/ui/fn_to_numeric_cast.64bit.stderr +++ b/src/tools/clippy/tests/ui/fn_to_numeric_cast.64bit.stderr @@ -1,5 +1,5 @@ error: casting function pointer `foo` to `i8`, which truncates the value - --> tests/ui/fn_to_numeric_cast.rs:12:13 + --> tests/ui/fn_to_numeric_cast.rs:13:13 | LL | let _ = foo as i8; | ^^^^^^^^^ help: try: `foo as usize` @@ -8,19 +8,19 @@ LL | let _ = foo as i8; = help: to override `-D warnings` add `#[allow(clippy::fn_to_numeric_cast_with_truncation)]` error: casting function pointer `foo` to `i16`, which truncates the value - --> tests/ui/fn_to_numeric_cast.rs:14:13 + --> tests/ui/fn_to_numeric_cast.rs:15:13 | LL | let _ = foo as i16; | ^^^^^^^^^^ help: try: `foo as usize` error: casting function pointer `foo` to `i32`, which truncates the value - --> tests/ui/fn_to_numeric_cast.rs:16:13 + --> tests/ui/fn_to_numeric_cast.rs:17:13 | LL | let _ = foo as i32; | ^^^^^^^^^^ help: try: `foo as usize` error: casting function pointer `foo` to `i64` - --> tests/ui/fn_to_numeric_cast.rs:19:13 + --> tests/ui/fn_to_numeric_cast.rs:20:13 | LL | let _ = foo as i64; | ^^^^^^^^^^ help: try: `foo as usize` @@ -29,115 +29,115 @@ LL | let _ = foo as i64; = help: to override `-D warnings` add `#[allow(clippy::fn_to_numeric_cast)]` error: casting function pointer `foo` to `i128` - --> tests/ui/fn_to_numeric_cast.rs:21:13 + --> tests/ui/fn_to_numeric_cast.rs:22:13 | LL | let _ = foo as i128; | ^^^^^^^^^^^ help: try: `foo as usize` error: casting function pointer `foo` to `isize` - --> tests/ui/fn_to_numeric_cast.rs:23:13 + --> tests/ui/fn_to_numeric_cast.rs:24:13 | LL | let _ = foo as isize; | ^^^^^^^^^^^^ help: try: `foo as usize` error: casting function pointer `foo` to `u8`, which truncates the value - --> tests/ui/fn_to_numeric_cast.rs:26:13 + --> tests/ui/fn_to_numeric_cast.rs:27:13 | LL | let _ = foo as u8; | ^^^^^^^^^ help: try: `foo as usize` error: casting function pointer `foo` to `u16`, which truncates the value - --> tests/ui/fn_to_numeric_cast.rs:28:13 + --> tests/ui/fn_to_numeric_cast.rs:29:13 | LL | let _ = foo as u16; | ^^^^^^^^^^ help: try: `foo as usize` error: casting function pointer `foo` to `u32`, which truncates the value - --> tests/ui/fn_to_numeric_cast.rs:30:13 + --> tests/ui/fn_to_numeric_cast.rs:31:13 | LL | let _ = foo as u32; | ^^^^^^^^^^ help: try: `foo as usize` error: casting function pointer `foo` to `u64` - --> tests/ui/fn_to_numeric_cast.rs:33:13 + --> tests/ui/fn_to_numeric_cast.rs:34:13 | LL | let _ = foo as u64; | ^^^^^^^^^^ help: try: `foo as usize` error: casting function pointer `foo` to `u128` - --> tests/ui/fn_to_numeric_cast.rs:35:13 + --> tests/ui/fn_to_numeric_cast.rs:36:13 | LL | let _ = foo as u128; | ^^^^^^^^^^^ help: try: `foo as usize` error: casting function pointer `abc` to `i8`, which truncates the value - --> tests/ui/fn_to_numeric_cast.rs:49:13 + --> tests/ui/fn_to_numeric_cast.rs:50:13 | LL | let _ = abc as i8; | ^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `i16`, which truncates the value - --> tests/ui/fn_to_numeric_cast.rs:51:13 + --> tests/ui/fn_to_numeric_cast.rs:52:13 | LL | let _ = abc as i16; | ^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `i32`, which truncates the value - --> tests/ui/fn_to_numeric_cast.rs:53:13 + --> tests/ui/fn_to_numeric_cast.rs:54:13 | LL | let _ = abc as i32; | ^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `i64` - --> tests/ui/fn_to_numeric_cast.rs:56:13 + --> tests/ui/fn_to_numeric_cast.rs:57:13 | LL | let _ = abc as i64; | ^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `i128` - --> tests/ui/fn_to_numeric_cast.rs:58:13 + --> tests/ui/fn_to_numeric_cast.rs:59:13 | LL | let _ = abc as i128; | ^^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `isize` - --> tests/ui/fn_to_numeric_cast.rs:60:13 + --> tests/ui/fn_to_numeric_cast.rs:61:13 | LL | let _ = abc as isize; | ^^^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `u8`, which truncates the value - --> tests/ui/fn_to_numeric_cast.rs:63:13 + --> tests/ui/fn_to_numeric_cast.rs:64:13 | LL | let _ = abc as u8; | ^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `u16`, which truncates the value - --> tests/ui/fn_to_numeric_cast.rs:65:13 + --> tests/ui/fn_to_numeric_cast.rs:66:13 | LL | let _ = abc as u16; | ^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `u32`, which truncates the value - --> tests/ui/fn_to_numeric_cast.rs:67:13 + --> tests/ui/fn_to_numeric_cast.rs:68:13 | LL | let _ = abc as u32; | ^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `u64` - --> tests/ui/fn_to_numeric_cast.rs:70:13 + --> tests/ui/fn_to_numeric_cast.rs:71:13 | LL | let _ = abc as u64; | ^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `u128` - --> tests/ui/fn_to_numeric_cast.rs:72:13 + --> tests/ui/fn_to_numeric_cast.rs:73:13 | LL | let _ = abc as u128; | ^^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `f` to `i32`, which truncates the value - --> tests/ui/fn_to_numeric_cast.rs:80:5 + --> tests/ui/fn_to_numeric_cast.rs:81:5 | LL | f as i32 | ^^^^^^^^ help: try: `f as usize` diff --git a/src/tools/clippy/tests/ui/fn_to_numeric_cast.rs b/src/tools/clippy/tests/ui/fn_to_numeric_cast.rs index f53cbacdb3771..0a07aeff366eb 100644 --- a/src/tools/clippy/tests/ui/fn_to_numeric_cast.rs +++ b/src/tools/clippy/tests/ui/fn_to_numeric_cast.rs @@ -3,6 +3,7 @@ //@[64bit]ignore-bitwidth: 32 //@no-rustfix #![warn(clippy::fn_to_numeric_cast, clippy::fn_to_numeric_cast_with_truncation)] +#![allow(function_casts_as_integer)] fn foo() -> String { String::new() diff --git a/src/tools/clippy/tests/ui/fn_to_numeric_cast_any.rs b/src/tools/clippy/tests/ui/fn_to_numeric_cast_any.rs index 42f2128cd3783..83c1e9a8387ef 100644 --- a/src/tools/clippy/tests/ui/fn_to_numeric_cast_any.rs +++ b/src/tools/clippy/tests/ui/fn_to_numeric_cast_any.rs @@ -1,5 +1,6 @@ #![warn(clippy::fn_to_numeric_cast_any)] #![allow(clippy::fn_to_numeric_cast, clippy::fn_to_numeric_cast_with_truncation)] +#![allow(function_casts_as_integer)] //@no-rustfix fn foo() -> u8 { 0 diff --git a/src/tools/clippy/tests/ui/fn_to_numeric_cast_any.stderr b/src/tools/clippy/tests/ui/fn_to_numeric_cast_any.stderr index 58fac2d406a0f..f7c49b8ff88b5 100644 --- a/src/tools/clippy/tests/ui/fn_to_numeric_cast_any.stderr +++ b/src/tools/clippy/tests/ui/fn_to_numeric_cast_any.stderr @@ -1,5 +1,5 @@ error: casting function pointer `foo` to `i8` - --> tests/ui/fn_to_numeric_cast_any.rs:23:13 + --> tests/ui/fn_to_numeric_cast_any.rs:24:13 | LL | let _ = foo as i8; | ^^^^^^^^^ @@ -12,7 +12,7 @@ LL | let _ = foo() as i8; | ++ error: casting function pointer `foo` to `i16` - --> tests/ui/fn_to_numeric_cast_any.rs:26:13 + --> tests/ui/fn_to_numeric_cast_any.rs:27:13 | LL | let _ = foo as i16; | ^^^^^^^^^^ @@ -23,7 +23,7 @@ LL | let _ = foo() as i16; | ++ error: casting function pointer `foo` to `i32` - --> tests/ui/fn_to_numeric_cast_any.rs:29:13 + --> tests/ui/fn_to_numeric_cast_any.rs:30:13 | LL | let _ = foo as i32; | ^^^^^^^^^^ @@ -34,7 +34,7 @@ LL | let _ = foo() as i32; | ++ error: casting function pointer `foo` to `i64` - --> tests/ui/fn_to_numeric_cast_any.rs:32:13 + --> tests/ui/fn_to_numeric_cast_any.rs:33:13 | LL | let _ = foo as i64; | ^^^^^^^^^^ @@ -45,7 +45,7 @@ LL | let _ = foo() as i64; | ++ error: casting function pointer `foo` to `i128` - --> tests/ui/fn_to_numeric_cast_any.rs:35:13 + --> tests/ui/fn_to_numeric_cast_any.rs:36:13 | LL | let _ = foo as i128; | ^^^^^^^^^^^ @@ -56,7 +56,7 @@ LL | let _ = foo() as i128; | ++ error: casting function pointer `foo` to `isize` - --> tests/ui/fn_to_numeric_cast_any.rs:38:13 + --> tests/ui/fn_to_numeric_cast_any.rs:39:13 | LL | let _ = foo as isize; | ^^^^^^^^^^^^ @@ -67,7 +67,7 @@ LL | let _ = foo() as isize; | ++ error: casting function pointer `foo` to `u8` - --> tests/ui/fn_to_numeric_cast_any.rs:41:13 + --> tests/ui/fn_to_numeric_cast_any.rs:42:13 | LL | let _ = foo as u8; | ^^^^^^^^^ @@ -78,7 +78,7 @@ LL | let _ = foo() as u8; | ++ error: casting function pointer `foo` to `u16` - --> tests/ui/fn_to_numeric_cast_any.rs:44:13 + --> tests/ui/fn_to_numeric_cast_any.rs:45:13 | LL | let _ = foo as u16; | ^^^^^^^^^^ @@ -89,7 +89,7 @@ LL | let _ = foo() as u16; | ++ error: casting function pointer `foo` to `u32` - --> tests/ui/fn_to_numeric_cast_any.rs:47:13 + --> tests/ui/fn_to_numeric_cast_any.rs:48:13 | LL | let _ = foo as u32; | ^^^^^^^^^^ @@ -100,7 +100,7 @@ LL | let _ = foo() as u32; | ++ error: casting function pointer `foo` to `u64` - --> tests/ui/fn_to_numeric_cast_any.rs:50:13 + --> tests/ui/fn_to_numeric_cast_any.rs:51:13 | LL | let _ = foo as u64; | ^^^^^^^^^^ @@ -111,7 +111,7 @@ LL | let _ = foo() as u64; | ++ error: casting function pointer `foo` to `u128` - --> tests/ui/fn_to_numeric_cast_any.rs:53:13 + --> tests/ui/fn_to_numeric_cast_any.rs:54:13 | LL | let _ = foo as u128; | ^^^^^^^^^^^ @@ -122,7 +122,7 @@ LL | let _ = foo() as u128; | ++ error: casting function pointer `foo` to `usize` - --> tests/ui/fn_to_numeric_cast_any.rs:56:13 + --> tests/ui/fn_to_numeric_cast_any.rs:57:13 | LL | let _ = foo as usize; | ^^^^^^^^^^^^ @@ -133,7 +133,7 @@ LL | let _ = foo() as usize; | ++ error: casting function pointer `Struct::static_method` to `usize` - --> tests/ui/fn_to_numeric_cast_any.rs:61:13 + --> tests/ui/fn_to_numeric_cast_any.rs:62:13 | LL | let _ = Struct::static_method as usize; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -144,7 +144,7 @@ LL | let _ = Struct::static_method() as usize; | ++ error: casting function pointer `f` to `usize` - --> tests/ui/fn_to_numeric_cast_any.rs:66:5 + --> tests/ui/fn_to_numeric_cast_any.rs:67:5 | LL | f as usize | ^^^^^^^^^^ @@ -155,7 +155,7 @@ LL | f() as usize | ++ error: casting function pointer `T::static_method` to `usize` - --> tests/ui/fn_to_numeric_cast_any.rs:71:5 + --> tests/ui/fn_to_numeric_cast_any.rs:72:5 | LL | T::static_method as usize | ^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -166,7 +166,7 @@ LL | T::static_method() as usize | ++ error: casting function pointer `(clos as fn(u32) -> u32)` to `usize` - --> tests/ui/fn_to_numeric_cast_any.rs:78:13 + --> tests/ui/fn_to_numeric_cast_any.rs:79:13 | LL | let _ = (clos as fn(u32) -> u32) as usize; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -177,7 +177,7 @@ LL | let _ = (clos as fn(u32) -> u32)() as usize; | ++ error: casting function pointer `foo` to `*const ()` - --> tests/ui/fn_to_numeric_cast_any.rs:83:13 + --> tests/ui/fn_to_numeric_cast_any.rs:84:13 | LL | let _ = foo as *const (); | ^^^^^^^^^^^^^^^^ diff --git a/src/tools/clippy/tests/ui/ptr_eq.fixed b/src/tools/clippy/tests/ui/ptr_eq.fixed index 9629b3eea5870..d3624a129b5fd 100644 --- a/src/tools/clippy/tests/ui/ptr_eq.fixed +++ b/src/tools/clippy/tests/ui/ptr_eq.fixed @@ -1,4 +1,5 @@ #![warn(clippy::ptr_eq)] +#![allow(function_casts_as_integer)] macro_rules! mac { ($a:expr, $b:expr) => { diff --git a/src/tools/clippy/tests/ui/ptr_eq.rs b/src/tools/clippy/tests/ui/ptr_eq.rs index 2b741d8df4684..f06a99cabc814 100644 --- a/src/tools/clippy/tests/ui/ptr_eq.rs +++ b/src/tools/clippy/tests/ui/ptr_eq.rs @@ -1,4 +1,5 @@ #![warn(clippy::ptr_eq)] +#![allow(function_casts_as_integer)] macro_rules! mac { ($a:expr, $b:expr) => { diff --git a/src/tools/clippy/tests/ui/ptr_eq.stderr b/src/tools/clippy/tests/ui/ptr_eq.stderr index e7340624b5950..f6be4c3f016b5 100644 --- a/src/tools/clippy/tests/ui/ptr_eq.stderr +++ b/src/tools/clippy/tests/ui/ptr_eq.stderr @@ -1,5 +1,5 @@ error: use `std::ptr::eq` when comparing raw pointers - --> tests/ui/ptr_eq.rs:22:13 + --> tests/ui/ptr_eq.rs:23:13 | LL | let _ = a as *const _ as usize == b as *const _ as usize; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::eq(a, b)` @@ -8,31 +8,31 @@ LL | let _ = a as *const _ as usize == b as *const _ as usize; = help: to override `-D warnings` add `#[allow(clippy::ptr_eq)]` error: use `std::ptr::eq` when comparing raw pointers - --> tests/ui/ptr_eq.rs:24:13 + --> tests/ui/ptr_eq.rs:25:13 | LL | let _ = a as *const _ == b as *const _; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::eq(a, b)` error: use `std::ptr::eq` when comparing raw pointers - --> tests/ui/ptr_eq.rs:50:13 + --> tests/ui/ptr_eq.rs:51:13 | LL | let _ = x as *const u32 == y as *mut u32 as *const u32; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::eq(x, y)` error: use `std::ptr::eq` when comparing raw pointers - --> tests/ui/ptr_eq.rs:53:13 + --> tests/ui/ptr_eq.rs:54:13 | LL | let _ = x as *const u32 != y as *mut u32 as *const u32; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `!std::ptr::eq(x, y)` error: use `std::ptr::eq` when comparing raw pointers - --> tests/ui/ptr_eq.rs:61:13 + --> tests/ui/ptr_eq.rs:62:13 | LL | let _ = mac!(cast a) as *const _ == mac!(cast b) as *const _; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::eq(mac!(cast a), mac!(cast b))` error: use `std::ptr::eq` when comparing raw pointers - --> tests/ui/ptr_eq.rs:65:13 + --> tests/ui/ptr_eq.rs:66:13 | LL | let _ = mac!(cast a) as *const _ == mac!(cast b) as *const _; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::eq(mac!(cast a), mac!(cast b))` diff --git a/src/tools/clippy/tests/ui/transmutes_expressible_as_ptr_casts.fixed b/src/tools/clippy/tests/ui/transmutes_expressible_as_ptr_casts.fixed index 02f67f79e2b19..f332e02a2d327 100644 --- a/src/tools/clippy/tests/ui/transmutes_expressible_as_ptr_casts.fixed +++ b/src/tools/clippy/tests/ui/transmutes_expressible_as_ptr_casts.fixed @@ -4,6 +4,7 @@ #![warn(clippy::useless_transmute)] #![warn(clippy::transmute_ptr_to_ptr)] #![allow(unused, clippy::borrow_as_ptr, clippy::missing_transmute_annotations)] +#![allow(function_casts_as_integer)] use std::mem::{size_of, transmute}; diff --git a/src/tools/clippy/tests/ui/transmutes_expressible_as_ptr_casts.rs b/src/tools/clippy/tests/ui/transmutes_expressible_as_ptr_casts.rs index c5e156405ebca..c29a42ddca53b 100644 --- a/src/tools/clippy/tests/ui/transmutes_expressible_as_ptr_casts.rs +++ b/src/tools/clippy/tests/ui/transmutes_expressible_as_ptr_casts.rs @@ -4,6 +4,7 @@ #![warn(clippy::useless_transmute)] #![warn(clippy::transmute_ptr_to_ptr)] #![allow(unused, clippy::borrow_as_ptr, clippy::missing_transmute_annotations)] +#![allow(function_casts_as_integer)] use std::mem::{size_of, transmute}; diff --git a/src/tools/clippy/tests/ui/transmutes_expressible_as_ptr_casts.stderr b/src/tools/clippy/tests/ui/transmutes_expressible_as_ptr_casts.stderr index f39a64d57eb49..5ddc3de6a039f 100644 --- a/src/tools/clippy/tests/ui/transmutes_expressible_as_ptr_casts.stderr +++ b/src/tools/clippy/tests/ui/transmutes_expressible_as_ptr_casts.stderr @@ -1,5 +1,5 @@ error: transmute from a pointer to a pointer - --> tests/ui/transmutes_expressible_as_ptr_casts.rs:19:38 + --> tests/ui/transmutes_expressible_as_ptr_casts.rs:20:38 | LL | let _ptr_i8_transmute = unsafe { transmute::<*const i32, *const i8>(ptr_i32) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -13,7 +13,7 @@ LL + let _ptr_i8_transmute = unsafe { ptr_i32.cast::() }; | error: transmute from a pointer to a pointer - --> tests/ui/transmutes_expressible_as_ptr_casts.rs:26:46 + --> tests/ui/transmutes_expressible_as_ptr_casts.rs:27:46 | LL | let _ptr_to_unsized_transmute = unsafe { transmute::<*const [i32], *const [u32]>(slice_ptr) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -25,7 +25,7 @@ LL + let _ptr_to_unsized_transmute = unsafe { slice_ptr as *const [u32] }; | error: transmute from `*const i32` to `usize` which could be expressed as a pointer cast instead - --> tests/ui/transmutes_expressible_as_ptr_casts.rs:33:50 + --> tests/ui/transmutes_expressible_as_ptr_casts.rs:34:50 | LL | let _usize_from_int_ptr_transmute = unsafe { transmute::<*const i32, usize>(ptr_i32) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as usize` @@ -34,7 +34,7 @@ LL | let _usize_from_int_ptr_transmute = unsafe { transmute::<*const i32, us = help: to override `-D warnings` add `#[allow(clippy::transmutes_expressible_as_ptr_casts)]` error: transmute from a reference to a pointer - --> tests/ui/transmutes_expressible_as_ptr_casts.rs:40:41 + --> tests/ui/transmutes_expressible_as_ptr_casts.rs:41:41 | LL | let _array_ptr_transmute = unsafe { transmute::<&[i32; 4], *const [i32; 4]>(array_ref) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `array_ref as *const [i32; 4]` @@ -43,31 +43,31 @@ LL | let _array_ptr_transmute = unsafe { transmute::<&[i32; 4], *const [i32; = help: to override `-D warnings` add `#[allow(clippy::useless_transmute)]` error: transmute from `fn(usize) -> u8` to `*const usize` which could be expressed as a pointer cast instead - --> tests/ui/transmutes_expressible_as_ptr_casts.rs:49:41 + --> tests/ui/transmutes_expressible_as_ptr_casts.rs:50:41 | LL | let _usize_ptr_transmute = unsafe { transmute:: u8, *const usize>(foo) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as *const usize` error: transmute from `fn(usize) -> u8` to `usize` which could be expressed as a pointer cast instead - --> tests/ui/transmutes_expressible_as_ptr_casts.rs:54:49 + --> tests/ui/transmutes_expressible_as_ptr_casts.rs:55:49 | LL | let _usize_from_fn_ptr_transmute = unsafe { transmute:: u8, usize>(foo) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as usize` error: transmute from `*const u32` to `usize` which could be expressed as a pointer cast instead - --> tests/ui/transmutes_expressible_as_ptr_casts.rs:58:36 + --> tests/ui/transmutes_expressible_as_ptr_casts.rs:59:36 | LL | let _usize_from_ref = unsafe { transmute::<*const u32, usize>(&1u32) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&1u32 as *const u32 as usize` error: transmute from a reference to a pointer - --> tests/ui/transmutes_expressible_as_ptr_casts.rs:70:14 + --> tests/ui/transmutes_expressible_as_ptr_casts.rs:71:14 | LL | unsafe { transmute::<&[i32; 1], *const u8>(in_param) } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `in_param as *const [i32; 1] as *const u8` error: transmute from `fn()` to `*const u8` which could be expressed as a pointer cast instead - --> tests/ui/transmutes_expressible_as_ptr_casts.rs:89:28 + --> tests/ui/transmutes_expressible_as_ptr_casts.rs:90:28 | LL | let _x: u8 = unsafe { *std::mem::transmute::(f) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(f as *const u8)` diff --git a/src/tools/miri/src/shims/native_lib/trace/parent.rs b/src/tools/miri/src/shims/native_lib/trace/parent.rs index 335188b331838..f6ebbc469f737 100644 --- a/src/tools/miri/src/shims/native_lib/trace/parent.rs +++ b/src/tools/miri/src/shims/native_lib/trace/parent.rs @@ -500,7 +500,8 @@ fn handle_segfault( capstone_disassemble(&instr, addr, cs, acc_events).expect("Failed to disassemble instruction"); // Move the instr ptr into the deprotection code. - #[expect(clippy::as_conversions)] + #[allow(unknown_lints)] + #[expect(clippy::as_conversions, function_casts_as_integer)] new_regs.set_ip(mempr_off as usize); // Don't mess up the stack by accident! new_regs.set_sp(stack_ptr); @@ -552,7 +553,8 @@ fn handle_segfault( new_regs = regs_bak; // Reprotect everything and continue. - #[expect(clippy::as_conversions)] + #[allow(unknown_lints)] + #[expect(clippy::as_conversions, function_casts_as_integer)] new_regs.set_ip(mempr_on as usize); new_regs.set_sp(stack_ptr); ptrace::setregs(pid, new_regs).unwrap(); diff --git a/src/tools/miri/tests/pass/backtrace/backtrace-api-v1.rs b/src/tools/miri/tests/pass/backtrace/backtrace-api-v1.rs index a3060abc39402..cf6f43dbbfa91 100644 --- a/src/tools/miri/tests/pass/backtrace/backtrace-api-v1.rs +++ b/src/tools/miri/tests/pass/backtrace/backtrace-api-v1.rs @@ -1,5 +1,7 @@ //@normalize-stderr-test: "::<.*>" -> "" +#![allow(function_casts_as_integer)] + #[inline(never)] fn func_a() -> Box<[*mut ()]> { func_b::() diff --git a/src/tools/miri/tests/pass/backtrace/backtrace-api-v1.stdout b/src/tools/miri/tests/pass/backtrace/backtrace-api-v1.stdout index 5c2995e132aa6..b3a4beb4e80ec 100644 --- a/src/tools/miri/tests/pass/backtrace/backtrace-api-v1.stdout +++ b/src/tools/miri/tests/pass/backtrace/backtrace-api-v1.stdout @@ -1,5 +1,5 @@ -tests/pass/backtrace/backtrace-api-v1.rs:27:9 (func_d) -tests/pass/backtrace/backtrace-api-v1.rs:14:9 (func_c) -tests/pass/backtrace/backtrace-api-v1.rs:9:5 (func_b::) -tests/pass/backtrace/backtrace-api-v1.rs:5:5 (func_a) -tests/pass/backtrace/backtrace-api-v1.rs:34:18 (main) +tests/pass/backtrace/backtrace-api-v1.rs:29:9 (func_d) +tests/pass/backtrace/backtrace-api-v1.rs:16:9 (func_c) +tests/pass/backtrace/backtrace-api-v1.rs:11:5 (func_b::) +tests/pass/backtrace/backtrace-api-v1.rs:7:5 (func_a) +tests/pass/backtrace/backtrace-api-v1.rs:36:18 (main) diff --git a/tests/ui/abi/stack-protector.rs b/tests/ui/abi/stack-protector.rs index 928b49c0eea50..dd0d0d43182ea 100644 --- a/tests/ui/abi/stack-protector.rs +++ b/tests/ui/abi/stack-protector.rs @@ -6,6 +6,8 @@ //@ compile-flags: -g //@ ignore-backends: gcc +#![allow(function_casts_as_integer)] + use std::env; use std::process::{Command, ExitStatus}; diff --git a/tests/ui/associated-consts/traits-associated-consts-ice-56870.rs b/tests/ui/associated-consts/traits-associated-consts-ice-56870.rs index 0c5a2b8477309..8dec3982ef205 100644 --- a/tests/ui/associated-consts/traits-associated-consts-ice-56870.rs +++ b/tests/ui/associated-consts/traits-associated-consts-ice-56870.rs @@ -2,6 +2,8 @@ //@ build-pass // Regression test for #56870: Internal compiler error (traits & associated consts) +#![allow(function_casts_as_integer)] + use std::fmt::Debug; pub trait Foo { diff --git a/tests/ui/cast/associated-type-bounds-cast-54094.rs b/tests/ui/cast/associated-type-bounds-cast-54094.rs index c9f307e95b9a0..1daca9c2cd565 100644 --- a/tests/ui/cast/associated-type-bounds-cast-54094.rs +++ b/tests/ui/cast/associated-type-bounds-cast-54094.rs @@ -1,5 +1,8 @@ // https://github.com/rust-lang/rust/issues/54094 //@ check-pass + +#![allow(function_casts_as_integer)] + trait Zoo { type X; } diff --git a/tests/ui/cast/cast-rfc0401.rs b/tests/ui/cast/cast-rfc0401.rs index f917f93a1c824..cb03065f0a690 100644 --- a/tests/ui/cast/cast-rfc0401.rs +++ b/tests/ui/cast/cast-rfc0401.rs @@ -1,6 +1,6 @@ //@ run-pass -#![allow(dead_code)] +#![allow(dead_code, function_casts_as_integer)] use std::vec; diff --git a/tests/ui/consts/const-eval/const_prop_errors.rs b/tests/ui/consts/const-eval/const_prop_errors.rs index 4580944e1bc57..68118da20e5d1 100644 --- a/tests/ui/consts/const-eval/const_prop_errors.rs +++ b/tests/ui/consts/const-eval/const_prop_errors.rs @@ -1,5 +1,7 @@ //@ check-pass +#![allow(function_casts_as_integer)] + pub trait Foo { fn foo(self) -> u32; } diff --git a/tests/ui/extern/extern-C-non-FFI-safe-arg-ice-52334.rs b/tests/ui/extern/extern-C-non-FFI-safe-arg-ice-52334.rs index 33d295f7ebe19..084c5ba73fcbc 100644 --- a/tests/ui/extern/extern-C-non-FFI-safe-arg-ice-52334.rs +++ b/tests/ui/extern/extern-C-non-FFI-safe-arg-ice-52334.rs @@ -4,6 +4,8 @@ //@ normalize-stderr: "\[i8\]" -> "[i8 or u8 (arch dependant)]" //@ normalize-stderr: "\[u8\]" -> "[i8 or u8 (arch dependant)]" +#![allow(function_casts_as_integer)] + type Foo = extern "C" fn(::std::ffi::CStr); //~^ WARN `extern` fn uses type extern "C" { diff --git a/tests/ui/extern/extern-C-non-FFI-safe-arg-ice-52334.stderr b/tests/ui/extern/extern-C-non-FFI-safe-arg-ice-52334.stderr index 044c1ae2dd42f..3b98f9a55f8ed 100644 --- a/tests/ui/extern/extern-C-non-FFI-safe-arg-ice-52334.stderr +++ b/tests/ui/extern/extern-C-non-FFI-safe-arg-ice-52334.stderr @@ -1,5 +1,5 @@ warning: `extern` fn uses type `CStr`, which is not FFI-safe - --> $DIR/extern-C-non-FFI-safe-arg-ice-52334.rs:7:12 + --> $DIR/extern-C-non-FFI-safe-arg-ice-52334.rs:9:12 | LL | type Foo = extern "C" fn(::std::ffi::CStr); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe @@ -9,7 +9,7 @@ LL | type Foo = extern "C" fn(::std::ffi::CStr); = note: `#[warn(improper_ctypes_definitions)]` on by default warning: `extern` block uses type `CStr`, which is not FFI-safe - --> $DIR/extern-C-non-FFI-safe-arg-ice-52334.rs:10:18 + --> $DIR/extern-C-non-FFI-safe-arg-ice-52334.rs:12:18 | LL | fn meh(blah: Foo); | ^^^ not FFI-safe diff --git a/tests/ui/lint/function_casts_as_integer.fixed b/tests/ui/lint/function_casts_as_integer.fixed new file mode 100644 index 0000000000000..e46bd6a349cfc --- /dev/null +++ b/tests/ui/lint/function_casts_as_integer.fixed @@ -0,0 +1,22 @@ +//@ run-rustfix + +#![deny(function_casts_as_integer)] +#![allow(unused_variables, dead_code)] // For the rustfix-ed code. + +fn foo() {} + +enum MyEnum { + Variant(u32), +} + +struct MyStruct(u32); + +fn main() { + let x = foo as *const () as usize; //~ ERROR: function_casts_as_integer + let x = String::len as *const () as usize; //~ ERROR: function_casts_as_integer + let x = MyEnum::Variant as *const () as usize; //~ ERROR: function_casts_as_integer + let x = MyStruct as *const () as usize; //~ ERROR: function_casts_as_integer + // Ok. + let x = foo as fn() as usize; + let x = foo as *const () as usize; +} diff --git a/tests/ui/lint/function_casts_as_integer.rs b/tests/ui/lint/function_casts_as_integer.rs new file mode 100644 index 0000000000000..f6f83d89463f1 --- /dev/null +++ b/tests/ui/lint/function_casts_as_integer.rs @@ -0,0 +1,22 @@ +//@ run-rustfix + +#![deny(function_casts_as_integer)] +#![allow(unused_variables, dead_code)] // For the rustfix-ed code. + +fn foo() {} + +enum MyEnum { + Variant(u32), +} + +struct MyStruct(u32); + +fn main() { + let x = foo as usize; //~ ERROR: function_casts_as_integer + let x = String::len as usize; //~ ERROR: function_casts_as_integer + let x = MyEnum::Variant as usize; //~ ERROR: function_casts_as_integer + let x = MyStruct as usize; //~ ERROR: function_casts_as_integer + // Ok. + let x = foo as fn() as usize; + let x = foo as *const () as usize; +} diff --git a/tests/ui/lint/function_casts_as_integer.stderr b/tests/ui/lint/function_casts_as_integer.stderr new file mode 100644 index 0000000000000..dbfd54a3b56f9 --- /dev/null +++ b/tests/ui/lint/function_casts_as_integer.stderr @@ -0,0 +1,51 @@ +error: direct cast of function item into an integer + --> $DIR/function_casts_as_integer.rs:15:17 + | +LL | let x = foo as usize; + | ^^^^^^^^ + | +note: the lint level is defined here + --> $DIR/function_casts_as_integer.rs:3:9 + | +LL | #![deny(function_casts_as_integer)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ +help: first cast to a pointer `as *const ()` + | +LL | let x = foo as *const () as usize; + | ++++++++++++ + +error: direct cast of function item into an integer + --> $DIR/function_casts_as_integer.rs:16:25 + | +LL | let x = String::len as usize; + | ^^^^^^^^ + | +help: first cast to a pointer `as *const ()` + | +LL | let x = String::len as *const () as usize; + | ++++++++++++ + +error: direct cast of function item into an integer + --> $DIR/function_casts_as_integer.rs:17:29 + | +LL | let x = MyEnum::Variant as usize; + | ^^^^^^^^ + | +help: first cast to a pointer `as *const ()` + | +LL | let x = MyEnum::Variant as *const () as usize; + | ++++++++++++ + +error: direct cast of function item into an integer + --> $DIR/function_casts_as_integer.rs:18:22 + | +LL | let x = MyStruct as usize; + | ^^^^^^^^ + | +help: first cast to a pointer `as *const ()` + | +LL | let x = MyStruct as *const () as usize; + | ++++++++++++ + +error: aborting due to 4 previous errors + diff --git a/tests/ui/lto/thin-lto-inlines.rs b/tests/ui/lto/thin-lto-inlines.rs index eeaae5c4c2575..c36f0e1d6c0b3 100644 --- a/tests/ui/lto/thin-lto-inlines.rs +++ b/tests/ui/lto/thin-lto-inlines.rs @@ -8,6 +8,8 @@ // praying two functions go into separate codegen units and then assuming that // if inlining *doesn't* happen the first byte of the functions will differ. +#![allow(function_casts_as_integer)] + pub fn foo() -> u32 { bar::bar() } diff --git a/tests/ui/lto/thin-lto-inlines2.rs b/tests/ui/lto/thin-lto-inlines2.rs index 4c7b9278b0822..62d65bbe0965e 100644 --- a/tests/ui/lto/thin-lto-inlines2.rs +++ b/tests/ui/lto/thin-lto-inlines2.rs @@ -11,6 +11,8 @@ // praying two functions go into separate codegen units and then assuming that // if inlining *doesn't* happen the first byte of the functions will differ. +#![allow(function_casts_as_integer)] + extern crate thin_lto_inlines_aux as bar; pub fn foo() -> u32 { diff --git a/tests/ui/mir/mir_coercions.rs b/tests/ui/mir/mir_coercions.rs index 11230c3fbb7ee..17053f410778b 100644 --- a/tests/ui/mir/mir_coercions.rs +++ b/tests/ui/mir/mir_coercions.rs @@ -1,5 +1,6 @@ //@ run-pass #![feature(coerce_unsized, unsize)] +#![allow(function_casts_as_integer)] use std::ops::CoerceUnsized; use std::marker::Unsize; diff --git a/tests/ui/mir/mir_misc_casts.rs b/tests/ui/mir/mir_misc_casts.rs index 35f217fceba6d..c44f1751448cf 100644 --- a/tests/ui/mir/mir_misc_casts.rs +++ b/tests/ui/mir/mir_misc_casts.rs @@ -1,4 +1,7 @@ //@ run-pass + +#![allow(function_casts_as_integer)] + fn func(){} const STR: &'static str = "hello"; diff --git a/tests/ui/mir/mir_refs_correct.rs b/tests/ui/mir/mir_refs_correct.rs index f1832d90a0f56..f7826202a83ec 100644 --- a/tests/ui/mir/mir_refs_correct.rs +++ b/tests/ui/mir/mir_refs_correct.rs @@ -1,6 +1,7 @@ //@ run-pass //@ aux-build:mir_external_refs.rs +#![allow(function_casts_as_integer)] #![allow(unpredictable_function_pointer_comparisons)] extern crate mir_external_refs as ext; diff --git a/tests/ui/runtime/signal-alternate-stack-cleanup.rs b/tests/ui/runtime/signal-alternate-stack-cleanup.rs index f2af86be0a5f5..8d52e9e26a29c 100644 --- a/tests/ui/runtime/signal-alternate-stack-cleanup.rs +++ b/tests/ui/runtime/signal-alternate-stack-cleanup.rs @@ -9,6 +9,7 @@ //@ ignore-vxworks no SIGWINCH in user space //@ ignore-nto no SA_ONSTACK +#![allow(function_casts_as_integer)] #![feature(rustc_private)] extern crate libc; diff --git a/tests/ui/traits/trait-upcasting/mono-impossible.rs b/tests/ui/traits/trait-upcasting/mono-impossible.rs index f19f0538efa70..5deec77a225bc 100644 --- a/tests/ui/traits/trait-upcasting/mono-impossible.rs +++ b/tests/ui/traits/trait-upcasting/mono-impossible.rs @@ -1,5 +1,7 @@ //@ build-pass +#![allow(function_casts_as_integer)] + trait Supertrait { fn method(&self) {} }