Skip to content

Commit

Permalink
Downgrade ambiguous_wide_pointer_comparisons suggestions to MaybeInco…
Browse files Browse the repository at this point in the history
…rrect

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 <rust-lang#53934>.
  • Loading branch information
jieyouxu committed Feb 20, 2024
1 parent ad14a22 commit 49025fa
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
6 changes: 3 additions & 3 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
25 changes: 25 additions & 0 deletions tests/ui/lint/ambiguous_wide_pointer_comparisons_suggestions.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//@ run-rustfix
//@ rustfix-only-machine-applicable
//@ check-pass

// See <https://github.com/rust-lang/rust/issues/121330>.

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<T: ?Sized>(a: *mut T, b: *mut T) -> bool {
let _ = a == b;
//~^ WARN ambiguous wide pointer comparison

panic!();
}
}
25 changes: 25 additions & 0 deletions tests/ui/lint/ambiguous_wide_pointer_comparisons_suggestions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//@ run-rustfix
//@ rustfix-only-machine-applicable
//@ check-pass

// See <https://github.com/rust-lang/rust/issues/121330>.

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<T: ?Sized>(a: *mut T, b: *mut T) -> bool {
let _ = a == b;
//~^ WARN ambiguous wide pointer comparison

panic!();
}
}
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 49025fa

Please sign in to comment.