]> git.lizzy.rs Git - rust.git/commitdiff
Don't say "a reference to" for Copy types
authorpatrick-gu <55641350+patrick-gu@users.noreply.github.com>
Mon, 20 Dec 2021 01:27:24 +0000 (17:27 -0800)
committerpatrick-gu <55641350+patrick-gu@users.noreply.github.com>
Mon, 20 Dec 2021 01:28:31 +0000 (17:28 -0800)
This changes the generate getter assist to not say "a reference to" in the documentation stub if the type is Copy, as the getter does not return a reference.

crates/ide_assists/src/handlers/generate_getter.rs
crates/ide_assists/src/utils.rs

index 81cf72dd713be5d30b1dd7629705e2f71de384f1..653e448cbb0fed0042eee06524551786de0613ba 100644 (file)
@@ -112,8 +112,12 @@ pub(crate) fn generate_getter_impl(
             }
 
             let vis = strukt.visibility().map_or(String::new(), |v| format!("{} ", v));
-            let (ty, body) = if mutable {
-                (format!("&mut {}", field_ty), format!("&mut self.{}", field_name))
+            let (ty, body, description) = if mutable {
+                (
+                    format!("&mut {}", field_ty),
+                    format!("&mut self.{}", field_name),
+                    "a mutable reference to ",
+                )
             } else {
                 let famous_defs = &FamousDefs(&ctx.sema, ctx.sema.scope(field_ty.syntax()).krate());
                 ctx.sema
@@ -124,18 +128,25 @@ pub(crate) fn generate_getter_impl(
                         (
                             conversion.convert_type(ctx.db()),
                             conversion.getter(field_name.to_string()),
+                            if conversion.is_copy() { "" } else { "a reference to " },
+                        )
+                    })
+                    .unwrap_or_else(|| {
+                        (
+                            format!("&{}", field_ty),
+                            format!("&self.{}", field_name),
+                            "a reference to ",
                         )
                     })
-                    .unwrap_or_else(|| (format!("&{}", field_ty), format!("&self.{}", field_name)))
             };
 
             format_to!(
                 buf,
-                "    /// Get a {}reference to the {}'s {}.
+                "    /// Get {}the {}'s {}.
     {}fn {}(&{}self) -> {} {{
         {}
     }}",
-                mutable.then(|| "mutable ").unwrap_or_default(),
+                description,
                 to_lower_snake_case(&strukt_name.to_string()).replace('_', " "),
                 fn_name.trim_end_matches("_mut").replace('_', " "),
                 vis,
@@ -349,7 +360,7 @@ struct S { foo: $0bool }
 struct S { foo: bool }
 
 impl S {
-    /// Get a reference to the s's foo.
+    /// Get the s's foo.
     fn $0foo(&self) -> bool {
         self.foo
     }
index 03433fc42af028e6ca0bbde18ec98add264d6b85..fd248ad02171f8ed14064c31a710982408085c8f 100644 (file)
@@ -572,6 +572,10 @@ pub(crate) fn getter(&self, field_name: String) -> String {
             | ReferenceConversionType::Result => format!("self.{}.as_ref()", field_name),
         }
     }
+
+    pub(crate) fn is_copy(&self) -> bool {
+        matches!(self.conversion, ReferenceConversionType::Copy)
+    }
 }
 
 // FIXME: It should return a new hir::Type, but currently constructing new types is too cumbersome