about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs29
-rw-r--r--tests/ui/suggestions/issue-108470.fixed29
-rw-r--r--tests/ui/suggestions/issue-108470.rs29
-rw-r--r--tests/ui/suggestions/issue-108470.stderr27
4 files changed, 104 insertions, 10 deletions
diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
index 18a49ef2f01..5fda4e191c2 100644
--- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
+++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
@@ -983,13 +983,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
     }
@@ -1150,13 +1155,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::<i32>::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::<i32>::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<i32>`
+   |
+   = note: expected type `bool`
+              found enum `Option<i32>`
+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`.