Skip to content

Commit

Permalink
Rollup merge of #118833 - Urgau:lint_function_pointer_comparisons, r=…
Browse files Browse the repository at this point in the history
…cjgillot

Add lint against function pointer comparisons

This is kind of a follow-up to rust-lang/rust#117758 where we added a lint against wide pointer comparisons for being ambiguous and unreliable; well function pointer comparisons are also unreliable. We should IMO follow a similar logic and warn people about it.

-----

## `unpredictable_function_pointer_comparisons`

*warn-by-default*

The `unpredictable_function_pointer_comparisons` lint checks comparison of function pointer as the operands.

### Example

```rust
fn foo() {}
let a = foo as fn();

let _ = a == foo;
```

### Explanation

Function pointers comparisons do not produce meaningful result since they are never guaranteed to be unique and could vary between different code generation units. Furthermore different function could have the same address after being merged together.

----

This PR also uplift the very similar `clippy::fn_address_comparisons` lint, which only linted on if one of the operand was an `ty::FnDef` while this PR lints proposes to lint on all `ty::FnPtr` and `ty::FnDef`.

```@rustbot``` labels +I-lang-nominated

~~Edit: Blocked on rust-lang/libs-team#323 being accepted and it's follow-up pr~~
  • Loading branch information
fmease authored Dec 5, 2024
2 parents b27651f + 4096d4a commit 5f9531b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/pass/function_pointers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unpredictable_function_pointer_comparisons)]

use std::mem;

trait Answer {
Expand Down
2 changes: 2 additions & 0 deletions tests/pass/issues/issue-91636.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unpredictable_function_pointer_comparisons)]

type BuiltIn = for<'a> fn(&str);

struct Function {
Expand Down

0 comments on commit 5f9531b

Please sign in to comment.