From f46806fb14d9ef9ba76b836019f9e2705b213c97 Mon Sep 17 00:00:00 2001 From: xizheyin Date: Wed, 7 May 2025 17:27:56 +0800 Subject: [PATCH 1/2] Add ui test suggest-remove-deref-issue-140166 Signed-off-by: xizheyin --- .../suggest-remove-deref-issue-140166.rs | 18 +++++++++++++ .../suggest-remove-deref-issue-140166.stderr | 26 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 tests/ui/traits/suggest-remove-deref-issue-140166.rs create mode 100644 tests/ui/traits/suggest-remove-deref-issue-140166.stderr diff --git a/tests/ui/traits/suggest-remove-deref-issue-140166.rs b/tests/ui/traits/suggest-remove-deref-issue-140166.rs new file mode 100644 index 0000000000000..1b832c7eba5c0 --- /dev/null +++ b/tests/ui/traits/suggest-remove-deref-issue-140166.rs @@ -0,0 +1,18 @@ +trait Trait {} + +struct Chars; +impl Trait for Chars {} + +struct FlatMap(T); +impl std::fmt::Debug for FlatMap { + fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + unimplemented!() + } +} + +fn lol() { + format_args!("{:?}", FlatMap(&Chars)); + //~^ ERROR the trait bound `&Chars: Trait` is not satisfied [E0277] +} + +fn main() {} diff --git a/tests/ui/traits/suggest-remove-deref-issue-140166.stderr b/tests/ui/traits/suggest-remove-deref-issue-140166.stderr new file mode 100644 index 0000000000000..8bdcf5981a50c --- /dev/null +++ b/tests/ui/traits/suggest-remove-deref-issue-140166.stderr @@ -0,0 +1,26 @@ +error[E0277]: the trait bound `&Chars: Trait` is not satisfied + --> $DIR/suggest-remove-deref-issue-140166.rs:14:26 + | +LL | format_args!("{:?}", FlatMap(&Chars)); + | ---- ^^^^^^^^^^^^^^^ the trait `Trait` is not implemented for `&Chars` + | | + | required by a bound introduced by this call + | +note: required for `FlatMap<&Chars>` to implement `Debug` + --> $DIR/suggest-remove-deref-issue-140166.rs:7:16 + | +LL | impl std::fmt::Debug for FlatMap { + | ----- ^^^^^^^^^^^^^^^ ^^^^^^^^^^ + | | + | unsatisfied trait bound introduced here +note: required by a bound in `core::fmt::rt::Argument::<'_>::new_debug` + --> $SRC_DIR/core/src/fmt/rt.rs:LL:COL +help: consider removing the leading `&`-reference + | +LL - format_args!("{:?}", FlatMap(&Chars)); +LL + format_args!("{:?}", latMap(&Chars)); + | + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0277`. From bd88f3e3e3eefd781d5cc1f4fbc0630f50e70882 Mon Sep 17 00:00:00 2001 From: xizheyin Date: Wed, 7 May 2025 17:31:42 +0800 Subject: [PATCH 2/2] Check `&` before suggest remove deref when trait_selection Signed-off-by: xizheyin --- .../src/error_reporting/traits/suggestions.rs | 6 ++++++ tests/ui/traits/suggest-remove-deref-issue-140166.stderr | 6 +----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs index de251ae2893c4..8801397b77541 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs @@ -1516,6 +1516,12 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { } else { expr.span.with_hi(expr.span.lo() + BytePos(1)) }; + + match self.tcx.sess.source_map().span_to_snippet(span) { + Ok(snippet) if snippet.starts_with("&") => {} + _ => break 'outer, + } + suggestions.push((span, String::new())); let ty::Ref(_, inner_ty, _) = suggested_ty.kind() else { diff --git a/tests/ui/traits/suggest-remove-deref-issue-140166.stderr b/tests/ui/traits/suggest-remove-deref-issue-140166.stderr index 8bdcf5981a50c..90f24d86d53e0 100644 --- a/tests/ui/traits/suggest-remove-deref-issue-140166.stderr +++ b/tests/ui/traits/suggest-remove-deref-issue-140166.stderr @@ -6,6 +6,7 @@ LL | format_args!("{:?}", FlatMap(&Chars)); | | | required by a bound introduced by this call | + = help: the trait `Trait` is implemented for `Chars` note: required for `FlatMap<&Chars>` to implement `Debug` --> $DIR/suggest-remove-deref-issue-140166.rs:7:16 | @@ -15,11 +16,6 @@ LL | impl std::fmt::Debug for FlatMap { | unsatisfied trait bound introduced here note: required by a bound in `core::fmt::rt::Argument::<'_>::new_debug` --> $SRC_DIR/core/src/fmt/rt.rs:LL:COL -help: consider removing the leading `&`-reference - | -LL - format_args!("{:?}", FlatMap(&Chars)); -LL + format_args!("{:?}", latMap(&Chars)); - | error: aborting due to 1 previous error