From 64f6e4f21ccd8e0ed1f4bb32abe525b4f6ab87c1 Mon Sep 17 00:00:00 2001 From: yukang Date: Mon, 20 Mar 2023 01:41:48 +0800 Subject: [PATCH] Fix bad suggestion for clone/is_some in field init shorthand --- .../src/fn_ctxt/suggestions.rs | 29 ++++++++++++------- tests/ui/suggestions/issue-108470.fixed | 29 +++++++++++++++++++ tests/ui/suggestions/issue-108470.rs | 29 +++++++++++++++++++ tests/ui/suggestions/issue-108470.stderr | 27 +++++++++++++++++ 4 files changed, 104 insertions(+), 10 deletions(-) create mode 100644 tests/ui/suggestions/issue-108470.fixed create mode 100644 tests/ui/suggestions/issue-108470.rs create mode 100644 tests/ui/suggestions/issue-108470.stderr diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 690d8a23826..aa8767fc85c 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -985,13 +985,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ) .must_apply_modulo_regions() { - diag.span_suggestion_verbose( - expr.span.shrink_to_hi(), - "consider using clone here", - ".clone()", - Applicability::MachineApplicable, - ); - return true; + let suggestion = match self.maybe_get_struct_pattern_shorthand_field(expr) { + Some(ident) => format!(": {}.clone()", ident), + None => ".clone()".to_string() + }; + + diag.span_suggestion_verbose( + expr.span.shrink_to_hi(), + "consider using clone here", + suggestion, + Applicability::MachineApplicable, + ); + return true; } false } @@ -1153,13 +1158,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return false; } - diag.span_suggestion( + let suggestion = match self.maybe_get_struct_pattern_shorthand_field(expr) { + Some(ident) => format!(": {}.is_some()", ident), + None => ".is_some()".to_string(), + }; + + diag.span_suggestion_verbose( expr.span.shrink_to_hi(), "use `Option::is_some` to test if the `Option` has a value", - ".is_some()", + suggestion, Applicability::MachineApplicable, ); - true } diff --git a/tests/ui/suggestions/issue-108470.fixed b/tests/ui/suggestions/issue-108470.fixed new file mode 100644 index 00000000000..9d15c4a8fcb --- /dev/null +++ b/tests/ui/suggestions/issue-108470.fixed @@ -0,0 +1,29 @@ +// run-rustfix +#![allow(dead_code)] + +struct Foo { + t: Thing +} + +#[derive(Clone)] +struct Thing; + +fn test_clone() { + let t = &Thing; + let _f = Foo { + t: t.clone() //~ ERROR mismatched types + }; +} + +struct Bar { + t: bool +} + +fn test_is_some() { + let t = Option::::Some(1); + let _f = Bar { + t: t.is_some() //~ ERROR mismatched types + }; +} + +fn main() {} diff --git a/tests/ui/suggestions/issue-108470.rs b/tests/ui/suggestions/issue-108470.rs new file mode 100644 index 00000000000..bda39085d4d --- /dev/null +++ b/tests/ui/suggestions/issue-108470.rs @@ -0,0 +1,29 @@ +// run-rustfix +#![allow(dead_code)] + +struct Foo { + t: Thing +} + +#[derive(Clone)] +struct Thing; + +fn test_clone() { + let t = &Thing; + let _f = Foo { + t //~ ERROR mismatched types + }; +} + +struct Bar { + t: bool +} + +fn test_is_some() { + let t = Option::::Some(1); + let _f = Bar { + t //~ ERROR mismatched types + }; +} + +fn main() {} diff --git a/tests/ui/suggestions/issue-108470.stderr b/tests/ui/suggestions/issue-108470.stderr new file mode 100644 index 00000000000..4e561eca734 --- /dev/null +++ b/tests/ui/suggestions/issue-108470.stderr @@ -0,0 +1,27 @@ +error[E0308]: mismatched types + --> $DIR/issue-108470.rs:14:9 + | +LL | t + | ^ expected `Thing`, found `&Thing` + | +help: consider using clone here + | +LL | t: t.clone() + | +++++++++++ + +error[E0308]: mismatched types + --> $DIR/issue-108470.rs:25:9 + | +LL | t + | ^ expected `bool`, found `Option` + | + = note: expected type `bool` + found enum `Option` +help: use `Option::is_some` to test if the `Option` has a value + | +LL | t: t.is_some() + | +++++++++++++ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0308`.