From 49025fa15c372bebb4c372bae206aa611b87ca1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E6=9D=B0=E5=8F=8B=20Jieyou=20Xu=20=28Joe=29?= Date: Tue, 20 Feb 2024 11:35:13 +0000 Subject: [PATCH] Downgrade ambiguous_wide_pointer_comparisons suggestions to MaybeIncorrect It is possible to have more than one valid suggestion, which when applied together via rustfix causes the code to no longer compile. This is a temporary workaround; the real long term solution to these issues is to solve . --- compiler/rustc_lint/src/lints.rs | 6 ++--- ...wide_pointer_comparisons_suggestions.fixed | 25 +++++++++++++++++++ ...us_wide_pointer_comparisons_suggestions.rs | 25 +++++++++++++++++++ ...ide_pointer_comparisons_suggestions.stderr | 18 +++++++++++++ 4 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 tests/ui/lint/ambiguous_wide_pointer_comparisons_suggestions.fixed create mode 100644 tests/ui/lint/ambiguous_wide_pointer_comparisons_suggestions.rs create mode 100644 tests/ui/lint/ambiguous_wide_pointer_comparisons_suggestions.stderr diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index c204c67fc1f7c..826b49a0e33d8 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -1543,7 +1543,7 @@ pub enum AmbiguousWidePointerComparisons<'a> { #[multipart_suggestion( lint_addr_metadata_suggestion, style = "verbose", - applicability = "machine-applicable" + applicability = "maybe-incorrect" )] pub struct AmbiguousWidePointerComparisonsAddrMetadataSuggestion<'a> { pub ne: &'a str, @@ -1562,7 +1562,7 @@ pub enum AmbiguousWidePointerComparisonsAddrSuggestion<'a> { #[multipart_suggestion( lint_addr_suggestion, style = "verbose", - applicability = "machine-applicable" + applicability = "maybe-incorrect" )] AddrEq { ne: &'a str, @@ -1578,7 +1578,7 @@ pub enum AmbiguousWidePointerComparisonsAddrSuggestion<'a> { #[multipart_suggestion( lint_addr_suggestion, style = "verbose", - applicability = "machine-applicable" + applicability = "maybe-incorrect" )] Cast { deref_left: &'a str, diff --git a/tests/ui/lint/ambiguous_wide_pointer_comparisons_suggestions.fixed b/tests/ui/lint/ambiguous_wide_pointer_comparisons_suggestions.fixed new file mode 100644 index 0000000000000..0de6e3a2dd831 --- /dev/null +++ b/tests/ui/lint/ambiguous_wide_pointer_comparisons_suggestions.fixed @@ -0,0 +1,25 @@ +//@ run-rustfix +//@ rustfix-only-machine-applicable +//@ check-pass + +// See . + +use std::cmp::PartialEq; +use std::rc::Rc; +use std::sync::Arc; + +struct A; +struct B; + +trait T {} +impl T for A {} +impl T for B {} + +fn main() { + fn cmp(a: *mut T, b: *mut T) -> bool { + let _ = a == b; + //~^ WARN ambiguous wide pointer comparison + + panic!(); + } +} diff --git a/tests/ui/lint/ambiguous_wide_pointer_comparisons_suggestions.rs b/tests/ui/lint/ambiguous_wide_pointer_comparisons_suggestions.rs new file mode 100644 index 0000000000000..0de6e3a2dd831 --- /dev/null +++ b/tests/ui/lint/ambiguous_wide_pointer_comparisons_suggestions.rs @@ -0,0 +1,25 @@ +//@ run-rustfix +//@ rustfix-only-machine-applicable +//@ check-pass + +// See . + +use std::cmp::PartialEq; +use std::rc::Rc; +use std::sync::Arc; + +struct A; +struct B; + +trait T {} +impl T for A {} +impl T for B {} + +fn main() { + fn cmp(a: *mut T, b: *mut T) -> bool { + let _ = a == b; + //~^ WARN ambiguous wide pointer comparison + + panic!(); + } +} diff --git a/tests/ui/lint/ambiguous_wide_pointer_comparisons_suggestions.stderr b/tests/ui/lint/ambiguous_wide_pointer_comparisons_suggestions.stderr new file mode 100644 index 0000000000000..ea8383ad30eaf --- /dev/null +++ b/tests/ui/lint/ambiguous_wide_pointer_comparisons_suggestions.stderr @@ -0,0 +1,18 @@ +warning: ambiguous wide pointer comparison, the comparison includes metadata which may not be expected + --> $DIR/ambiguous_wide_pointer_comparisons_suggestions.rs:20:17 + | +LL | let _ = a == b; + | ^^^^^^ + | + = note: `#[warn(ambiguous_wide_pointer_comparisons)]` on by default +help: use `std::ptr::addr_eq` or untyped pointers to only compare their addresses + | +LL | let _ = std::ptr::addr_eq(a, b); + | ++++++++++++++++++ ~ + +help: use explicit `std::ptr::eq` method to compare metadata and addresses + | +LL | let _ = std::ptr::eq(a, b); + | +++++++++++++ ~ + + +warning: 1 warning emitted +