Skip to content

Commit ef8da49

Browse files
authored
Rollup merge of #141470 - GuillaumeGomez:function_casts_as_integer, r=urgau
Add new `function_casts_as_integer` lint The `function_casts_as_integer` lint detects cases where users cast a function pointer into an integer. *warn-by-default* ### Example ```rust fn foo() {} let x = foo as usize; ``` ``` warning: casting a function into an integer implicitly --> $DIR/function_casts_as_integer.rs:9:17 | LL | let x = foo as usize; | ^^^^^^^^ | help: add `fn() as usize` | LL | let x = foo as fn() as usize; | +++++++ ``` ### Explanation You should never cast a function directly into an integer but go through a cast as `fn` first to make it obvious what's going on. It also allows to prevent confusion with (associated) constants. Related to rust-lang/rust#81686 and https://stackoverflow.com/questions/68701177/whats-the-meaning-of-casting-a-rust-enum-variant-to-a-numeric-data-type r? ````@urgau````
2 parents 92ed621 + a112cb2 commit ef8da49

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

src/shims/native_lib/trace/parent.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,8 @@ fn handle_segfault(
500500
capstone_disassemble(&instr, addr, cs, acc_events).expect("Failed to disassemble instruction");
501501

502502
// Move the instr ptr into the deprotection code.
503-
#[expect(clippy::as_conversions)]
503+
#[allow(unknown_lints)]
504+
#[expect(clippy::as_conversions, function_casts_as_integer)]
504505
new_regs.set_ip(mempr_off as usize);
505506
// Don't mess up the stack by accident!
506507
new_regs.set_sp(stack_ptr);
@@ -552,7 +553,8 @@ fn handle_segfault(
552553
new_regs = regs_bak;
553554

554555
// Reprotect everything and continue.
555-
#[expect(clippy::as_conversions)]
556+
#[allow(unknown_lints)]
557+
#[expect(clippy::as_conversions, function_casts_as_integer)]
556558
new_regs.set_ip(mempr_on as usize);
557559
new_regs.set_sp(stack_ptr);
558560
ptrace::setregs(pid, new_regs).unwrap();

tests/pass/backtrace/backtrace-api-v1.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//@normalize-stderr-test: "::<.*>" -> ""
22

3+
#![allow(function_casts_as_integer)]
4+
35
#[inline(never)]
46
fn func_a() -> Box<[*mut ()]> {
57
func_b::<u8>()
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
tests/pass/backtrace/backtrace-api-v1.rs:27:9 (func_d)
2-
tests/pass/backtrace/backtrace-api-v1.rs:14:9 (func_c)
3-
tests/pass/backtrace/backtrace-api-v1.rs:9:5 (func_b::<u8>)
4-
tests/pass/backtrace/backtrace-api-v1.rs:5:5 (func_a)
5-
tests/pass/backtrace/backtrace-api-v1.rs:34:18 (main)
1+
tests/pass/backtrace/backtrace-api-v1.rs:29:9 (func_d)
2+
tests/pass/backtrace/backtrace-api-v1.rs:16:9 (func_c)
3+
tests/pass/backtrace/backtrace-api-v1.rs:11:5 (func_b::<u8>)
4+
tests/pass/backtrace/backtrace-api-v1.rs:7:5 (func_a)
5+
tests/pass/backtrace/backtrace-api-v1.rs:36:18 (main)

0 commit comments

Comments
 (0)