Skip to content

Commit 7cc19d7

Browse files
authored
Rollup merge of #118833 - Urgau:lint_function_pointer_comparisons, r=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~~
2 parents f850d15 + c8d800e commit 7cc19d7

9 files changed

+79
-170
lines changed

Diff for: clippy_lints/src/declared_lints.rs

-1
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,6 @@ pub static LINTS: &[&crate::LintInfo] = &[
744744
crate::unit_types::LET_UNIT_VALUE_INFO,
745745
crate::unit_types::UNIT_ARG_INFO,
746746
crate::unit_types::UNIT_CMP_INFO,
747-
crate::unnamed_address::FN_ADDRESS_COMPARISONS_INFO,
748747
crate::unnecessary_box_returns::UNNECESSARY_BOX_RETURNS_INFO,
749748
crate::unnecessary_literal_bound::UNNECESSARY_LITERAL_BOUND_INFO,
750749
crate::unnecessary_map_on_constructor::UNNECESSARY_MAP_ON_CONSTRUCTOR_INFO,

Diff for: clippy_lints/src/deprecated_lints.rs

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ declare_with_version! { RENAMED(RENAMED_VERSION): &[(&str, &str)] = &[
7474
#[clippy::version = "1.53.0"]
7575
("clippy::filter_map", "clippy::manual_filter_map"),
7676
#[clippy::version = ""]
77+
("clippy::fn_address_comparisons", "unpredictable_function_pointer_comparisons"),
78+
#[clippy::version = ""]
7779
("clippy::identity_conversion", "clippy::useless_conversion"),
7880
#[clippy::version = "pre 1.29.0"]
7981
("clippy::if_let_redundant_pattern_matching", "clippy::redundant_pattern_matching"),

Diff for: clippy_lints/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,6 @@ mod uninhabited_references;
363363
mod uninit_vec;
364364
mod unit_return_expecting_ord;
365365
mod unit_types;
366-
mod unnamed_address;
367366
mod unnecessary_box_returns;
368367
mod unnecessary_literal_bound;
369368
mod unnecessary_map_on_constructor;
@@ -786,7 +785,6 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
786785
store.register_early_pass(|| Box::new(option_env_unwrap::OptionEnvUnwrap));
787786
store.register_late_pass(move |_| Box::new(wildcard_imports::WildcardImports::new(conf)));
788787
store.register_late_pass(|_| Box::<redundant_pub_crate::RedundantPubCrate>::default());
789-
store.register_late_pass(|_| Box::new(unnamed_address::UnnamedAddress));
790788
store.register_late_pass(|_| Box::<dereference::Dereferencing<'_>>::default());
791789
store.register_late_pass(|_| Box::new(option_if_let_else::OptionIfLetElse));
792790
store.register_late_pass(|_| Box::new(future_not_send::FutureNotSend));

Diff for: clippy_lints/src/unnamed_address.rs

-60
This file was deleted.

Diff for: tests/ui/fn_address_comparisons.rs

-23
This file was deleted.

Diff for: tests/ui/fn_address_comparisons.stderr

-17
This file was deleted.

Diff for: tests/ui/rename.fixed

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#![allow(unknown_lints)]
6060
#![allow(unused_labels)]
6161
#![allow(ambiguous_wide_pointer_comparisons)]
62+
#![allow(unpredictable_function_pointer_comparisons)]
6263
#![allow(clippy::reversed_empty_ranges)]
6364
#![warn(clippy::almost_complete_range)] //~ ERROR: lint `clippy::almost_complete_letter_range`
6465
#![warn(clippy::disallowed_names)] //~ ERROR: lint `clippy::blacklisted_name`
@@ -74,6 +75,7 @@
7475
#![warn(clippy::mixed_read_write_in_expression)] //~ ERROR: lint `clippy::eval_order_dependence`
7576
#![warn(clippy::manual_find_map)] //~ ERROR: lint `clippy::find_map`
7677
#![warn(clippy::manual_filter_map)] //~ ERROR: lint `clippy::filter_map`
78+
#![warn(unpredictable_function_pointer_comparisons)] //~ ERROR: lint `clippy::fn_address_comparisons`
7779
#![warn(clippy::useless_conversion)] //~ ERROR: lint `clippy::identity_conversion`
7880
#![warn(clippy::redundant_pattern_matching)] //~ ERROR: lint `clippy::if_let_redundant_pattern_matching`
7981
#![warn(clippy::match_result_ok)] //~ ERROR: lint `clippy::if_let_some_result`

Diff for: tests/ui/rename.rs

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#![allow(unknown_lints)]
6060
#![allow(unused_labels)]
6161
#![allow(ambiguous_wide_pointer_comparisons)]
62+
#![allow(unpredictable_function_pointer_comparisons)]
6263
#![allow(clippy::reversed_empty_ranges)]
6364
#![warn(clippy::almost_complete_letter_range)] //~ ERROR: lint `clippy::almost_complete_letter_range`
6465
#![warn(clippy::blacklisted_name)] //~ ERROR: lint `clippy::blacklisted_name`
@@ -74,6 +75,7 @@
7475
#![warn(clippy::eval_order_dependence)] //~ ERROR: lint `clippy::eval_order_dependence`
7576
#![warn(clippy::find_map)] //~ ERROR: lint `clippy::find_map`
7677
#![warn(clippy::filter_map)] //~ ERROR: lint `clippy::filter_map`
78+
#![warn(clippy::fn_address_comparisons)] //~ ERROR: lint `clippy::fn_address_comparisons`
7779
#![warn(clippy::identity_conversion)] //~ ERROR: lint `clippy::identity_conversion`
7880
#![warn(clippy::if_let_redundant_pattern_matching)] //~ ERROR: lint `clippy::if_let_redundant_pattern_matching`
7981
#![warn(clippy::if_let_some_result)] //~ ERROR: lint `clippy::if_let_some_result`

0 commit comments

Comments
 (0)