You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Auto merge of rust-lang#118833 - Urgau:lint_function_pointer_comparisons, r=<try>
Add lint against function pointer comparisons
This is kind of a follow-up to rust-lang#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
Copy file name to clipboardExpand all lines: compiler/rustc_lint/messages.ftl
+4
Original file line number
Diff line number
Diff line change
@@ -538,6 +538,10 @@ lint_unknown_lint =
538
538
lint_unknown_tool_in_scoped_lint = unknown tool name `{$tool_name}` found in scoped lint: `{$tool_name}::{$lint_name}`
539
539
.help = add `#![register_tool({$tool_name})]` to the crate root
540
540
541
+
lint_unpredictable_fn_pointer_comparisons = function pointer comparisons do not produce meaningful results since their addresses are not guaranteed to be unique
542
+
.note_duplicated_fn = the address of the same function can very between different codegen units
543
+
.note_deduplicated_fn = furthermore, different functions could have the same address after being merged together
544
+
541
545
lint_unsupported_group = `{$lint_group}` lint group is not supported with ´--force-warn´
542
546
543
547
lint_untranslatable_diag = diagnostics should be created using translatable messages
0 commit comments