Report the unpredictable_function_pointer_comparisons lint in macro

This commit is contained in:
Urgau 2024-12-19 22:07:05 +01:00
parent f77bb1b294
commit fafc0f249f
8 changed files with 91 additions and 26 deletions

View file

@ -196,7 +196,8 @@ declare_lint! {
/// same address after being merged together.
UNPREDICTABLE_FUNCTION_POINTER_COMPARISONS,
Warn,
"detects unpredictable function pointer comparisons"
"detects unpredictable function pointer comparisons",
report_in_external_macro
}
#[derive(Copy, Clone, Default)]

View file

@ -12,6 +12,6 @@ fn main() {
let _ = Some::<FnPtr>(func) == Some(func as unsafe extern "C" fn());
//~^ WARN function pointer comparisons
// Undecided as of https://github.com/rust-lang/rust/pull/134536
assert_eq!(Some::<FnPtr>(func), Some(func as unsafe extern "C" fn()));
//~^ WARN function pointer comparisons
}

View file

@ -9,5 +9,16 @@ LL | let _ = Some::<FnPtr>(func) == Some(func as unsafe extern "C" fn());
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
= note: `#[warn(unpredictable_function_pointer_comparisons)]` on by default
warning: 1 warning emitted
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
--> $DIR/fn-ptr-comparisons-some.rs:15:5
|
LL | assert_eq!(Some::<FnPtr>(func), Some(func as unsafe extern "C" fn()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the address of the same function can vary between different codegen units
= note: furthermore, different functions could have the same address after being merged together
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
= note: this warning originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
warning: 2 warnings emitted

View file

@ -1,5 +1,23 @@
//@ check-pass
#[derive(PartialEq, Eq)]
struct A {
f: fn(),
//~^ WARN function pointer comparisons
}
#[allow(unpredictable_function_pointer_comparisons)]
#[derive(PartialEq, Eq)]
struct AllowedAbove {
f: fn(),
}
#[derive(PartialEq, Eq)]
#[allow(unpredictable_function_pointer_comparisons)]
struct AllowedBelow {
f: fn(),
}
fn main() {
let f: fn() = main;
let g: fn() = main;
@ -12,4 +30,8 @@ fn main() {
//~^ WARN function pointer comparisons
let _ = f < g;
//~^ WARN function pointer comparisons
let _ = assert_eq!(g, g);
//~^ WARN function pointer comparisons
let _ = assert_ne!(g, g);
//~^ WARN function pointer comparisons
}

View file

@ -1,8 +1,11 @@
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
--> $DIR/fn-ptr-comparisons-weird.rs:7:13
--> $DIR/fn-ptr-comparisons-weird.rs:5:5
|
LL | let _ = f > g;
| ^^^^^
LL | #[derive(PartialEq, Eq)]
| --------- in this derive macro expansion
LL | struct A {
LL | f: fn(),
| ^^^^^^^
|
= note: the address of the same function can vary between different codegen units
= note: furthermore, different functions could have the same address after being merged together
@ -10,7 +13,17 @@ LL | let _ = f > g;
= note: `#[warn(unpredictable_function_pointer_comparisons)]` on by default
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
--> $DIR/fn-ptr-comparisons-weird.rs:9:13
--> $DIR/fn-ptr-comparisons-weird.rs:25:13
|
LL | let _ = f > g;
| ^^^^^
|
= note: the address of the same function can vary between different codegen units
= note: furthermore, different functions could have the same address after being merged together
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
--> $DIR/fn-ptr-comparisons-weird.rs:27:13
|
LL | let _ = f >= g;
| ^^^^^^
@ -20,7 +33,7 @@ LL | let _ = f >= g;
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
--> $DIR/fn-ptr-comparisons-weird.rs:11:13
--> $DIR/fn-ptr-comparisons-weird.rs:29:13
|
LL | let _ = f <= g;
| ^^^^^^
@ -30,7 +43,7 @@ LL | let _ = f <= g;
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
--> $DIR/fn-ptr-comparisons-weird.rs:13:13
--> $DIR/fn-ptr-comparisons-weird.rs:31:13
|
LL | let _ = f < g;
| ^^^^^
@ -39,5 +52,27 @@ LL | let _ = f < g;
= note: furthermore, different functions could have the same address after being merged together
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
warning: 4 warnings emitted
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
--> $DIR/fn-ptr-comparisons-weird.rs:33:13
|
LL | let _ = assert_eq!(g, g);
| ^^^^^^^^^^^^^^^^
|
= note: the address of the same function can vary between different codegen units
= note: furthermore, different functions could have the same address after being merged together
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
= note: this warning originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
--> $DIR/fn-ptr-comparisons-weird.rs:35:13
|
LL | let _ = assert_ne!(g, g);
| ^^^^^^^^^^^^^^^^
|
= note: the address of the same function can vary between different codegen units
= note: furthermore, different functions could have the same address after being merged together
= note: for more information visit <https://doc.rust-lang.org/nightly/core/ptr/fn.fn_addr_eq.html>
= note: this warning originates in the macro `assert_ne` (in Nightly builds, run with -Z macro-backtrace for more info)
warning: 7 warnings emitted

View file

@ -11,7 +11,6 @@ extern "C" fn c() {}
extern "C" fn args(_a: i32) -> i32 { 0 }
#[derive(PartialEq, Eq)]
struct A {
f: fn(),
}
@ -52,7 +51,6 @@ fn main() {
let _ = std::ptr::fn_addr_eq(t, test as unsafe extern "C" fn());
//~^ WARN function pointer comparisons
let _ = a1 == a2; // should not warn
let _ = std::ptr::fn_addr_eq(a1.f, a2.f);
//~^ WARN function pointer comparisons
}

View file

@ -11,7 +11,6 @@ extern "C" fn c() {}
extern "C" fn args(_a: i32) -> i32 { 0 }
#[derive(PartialEq, Eq)]
struct A {
f: fn(),
}
@ -52,7 +51,6 @@ fn main() {
let _ = t == test;
//~^ WARN function pointer comparisons
let _ = a1 == a2; // should not warn
let _ = a1.f == a2.f;
//~^ WARN function pointer comparisons
}

View file

@ -1,5 +1,5 @@
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
--> $DIR/fn-ptr-comparisons.rs:26:13
--> $DIR/fn-ptr-comparisons.rs:25:13
|
LL | let _ = f == a;
| ^^^^^^
@ -15,7 +15,7 @@ LL + let _ = std::ptr::fn_addr_eq(f, a as fn());
|
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
--> $DIR/fn-ptr-comparisons.rs:28:13
--> $DIR/fn-ptr-comparisons.rs:27:13
|
LL | let _ = f != a;
| ^^^^^^
@ -30,7 +30,7 @@ LL + let _ = !std::ptr::fn_addr_eq(f, a as fn());
|
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
--> $DIR/fn-ptr-comparisons.rs:30:13
--> $DIR/fn-ptr-comparisons.rs:29:13
|
LL | let _ = f == g;
| ^^^^^^
@ -45,7 +45,7 @@ LL + let _ = std::ptr::fn_addr_eq(f, g);
|
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
--> $DIR/fn-ptr-comparisons.rs:32:13
--> $DIR/fn-ptr-comparisons.rs:31:13
|
LL | let _ = f == f;
| ^^^^^^
@ -60,7 +60,7 @@ LL + let _ = std::ptr::fn_addr_eq(f, f);
|
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
--> $DIR/fn-ptr-comparisons.rs:34:13
--> $DIR/fn-ptr-comparisons.rs:33:13
|
LL | let _ = g == g;
| ^^^^^^
@ -75,7 +75,7 @@ LL + let _ = std::ptr::fn_addr_eq(g, g);
|
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
--> $DIR/fn-ptr-comparisons.rs:36:13
--> $DIR/fn-ptr-comparisons.rs:35:13
|
LL | let _ = g == g;
| ^^^^^^
@ -90,7 +90,7 @@ LL + let _ = std::ptr::fn_addr_eq(g, g);
|
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
--> $DIR/fn-ptr-comparisons.rs:38:13
--> $DIR/fn-ptr-comparisons.rs:37:13
|
LL | let _ = &g == &g;
| ^^^^^^^^
@ -105,7 +105,7 @@ LL + let _ = std::ptr::fn_addr_eq(g, g);
|
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
--> $DIR/fn-ptr-comparisons.rs:40:13
--> $DIR/fn-ptr-comparisons.rs:39:13
|
LL | let _ = a as fn() == g;
| ^^^^^^^^^^^^^^
@ -120,7 +120,7 @@ LL + let _ = std::ptr::fn_addr_eq(a as fn(), g);
|
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
--> $DIR/fn-ptr-comparisons.rs:44:13
--> $DIR/fn-ptr-comparisons.rs:43:13
|
LL | let _ = cfn == c;
| ^^^^^^^^
@ -135,7 +135,7 @@ LL + let _ = std::ptr::fn_addr_eq(cfn, c as extern "C" fn());
|
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
--> $DIR/fn-ptr-comparisons.rs:48:13
--> $DIR/fn-ptr-comparisons.rs:47:13
|
LL | let _ = argsfn == args;
| ^^^^^^^^^^^^^^
@ -150,7 +150,7 @@ LL + let _ = std::ptr::fn_addr_eq(argsfn, args as extern "C" fn(i32) -> i32)
|
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
--> $DIR/fn-ptr-comparisons.rs:52:13
--> $DIR/fn-ptr-comparisons.rs:51:13
|
LL | let _ = t == test;
| ^^^^^^^^^
@ -165,7 +165,7 @@ LL + let _ = std::ptr::fn_addr_eq(t, test as unsafe extern "C" fn());
|
warning: function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
--> $DIR/fn-ptr-comparisons.rs:56:13
--> $DIR/fn-ptr-comparisons.rs:54:13
|
LL | let _ = a1.f == a2.f;
| ^^^^^^^^^^^^