From 8873c1871aee825d8dfe929e63f7f5d7bbed7484 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 24 Jan 2025 22:08:39 +0000 Subject: [PATCH] Skip suggestions in `derive`d code Do not suggest ``` help: use parentheses to call these | 5 | (callback: Rc)(), | + +++ ``` Skip all "call function for this binop" suggestions when in a derive context. Fix #135989. --- .../rustc_hir_typeck/src/fn_ctxt/suggestions.rs | 3 +++ .../do-not-suggest-calling-fn-in-derive-macro.rs | 8 ++++++++ ...o-not-suggest-calling-fn-in-derive-macro.stderr | 14 ++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 tests/ui/deriving/do-not-suggest-calling-fn-in-derive-macro.rs create mode 100644 tests/ui/deriving/do-not-suggest-calling-fn-in-derive-macro.stderr diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 16294970f0531..928010c03c2ee 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -185,6 +185,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { rhs_ty: Ty<'tcx>, can_satisfy: impl FnOnce(Ty<'tcx>, Ty<'tcx>) -> bool, ) -> bool { + if lhs_expr.span.in_derive_expansion() || rhs_expr.span.in_derive_expansion() { + return false; + } let Some((_, lhs_output_ty, lhs_inputs)) = self.extract_callable_info(lhs_ty) else { return false; }; diff --git a/tests/ui/deriving/do-not-suggest-calling-fn-in-derive-macro.rs b/tests/ui/deriving/do-not-suggest-calling-fn-in-derive-macro.rs new file mode 100644 index 0000000000000..76b93c0c947cb --- /dev/null +++ b/tests/ui/deriving/do-not-suggest-calling-fn-in-derive-macro.rs @@ -0,0 +1,8 @@ +use std::rc::Rc; + +#[derive(PartialEq)] //~ NOTE in this expansion +pub struct Function { + callback: Rc, //~ ERROR binary operation `==` cannot be applied to type `Rc` +} + +fn main() {} diff --git a/tests/ui/deriving/do-not-suggest-calling-fn-in-derive-macro.stderr b/tests/ui/deriving/do-not-suggest-calling-fn-in-derive-macro.stderr new file mode 100644 index 0000000000000..40464a49c3438 --- /dev/null +++ b/tests/ui/deriving/do-not-suggest-calling-fn-in-derive-macro.stderr @@ -0,0 +1,14 @@ +error[E0369]: binary operation `==` cannot be applied to type `Rc` + --> $DIR/do-not-suggest-calling-fn-in-derive-macro.rs:5:5 + | +LL | #[derive(PartialEq)] + | --------- in this derive macro expansion +LL | pub struct Function { +LL | callback: Rc, + | ^^^^^^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0369`.