Skip to content

Commit 76b50f1

Browse files
committed
Don't say "a reference to" for Copy types
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.
1 parent d9b2291 commit 76b50f1

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

crates/ide_assists/src/handlers/generate_getter.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,12 @@ pub(crate) fn generate_getter_impl(
112112
}
113113

114114
let vis = strukt.visibility().map_or(String::new(), |v| format!("{} ", v));
115-
let (ty, body) = if mutable {
116-
(format!("&mut {}", field_ty), format!("&mut self.{}", field_name))
115+
let (ty, body, description) = if mutable {
116+
(
117+
format!("&mut {}", field_ty),
118+
format!("&mut self.{}", field_name),
119+
"a mutable reference to ",
120+
)
117121
} else {
118122
let famous_defs = &FamousDefs(&ctx.sema, ctx.sema.scope(field_ty.syntax()).krate());
119123
ctx.sema
@@ -124,18 +128,25 @@ pub(crate) fn generate_getter_impl(
124128
(
125129
conversion.convert_type(ctx.db()),
126130
conversion.getter(field_name.to_string()),
131+
if conversion.is_copy() { "" } else { "a reference to " },
132+
)
133+
})
134+
.unwrap_or_else(|| {
135+
(
136+
format!("&{}", field_ty),
137+
format!("&self.{}", field_name),
138+
"a reference to ",
127139
)
128140
})
129-
.unwrap_or_else(|| (format!("&{}", field_ty), format!("&self.{}", field_name)))
130141
};
131142

132143
format_to!(
133144
buf,
134-
" /// Get a {}reference to the {}'s {}.
145+
" /// Get {}the {}'s {}.
135146
{}fn {}(&{}self) -> {} {{
136147
{}
137148
}}",
138-
mutable.then(|| "mutable ").unwrap_or_default(),
149+
description,
139150
to_lower_snake_case(&strukt_name.to_string()).replace('_', " "),
140151
fn_name.trim_end_matches("_mut").replace('_', " "),
141152
vis,
@@ -349,7 +360,7 @@ struct S { foo: $0bool }
349360
struct S { foo: bool }
350361
351362
impl S {
352-
/// Get a reference to the s's foo.
363+
/// Get the s's foo.
353364
fn $0foo(&self) -> bool {
354365
self.foo
355366
}

crates/ide_assists/src/utils.rs

+4
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,10 @@ impl ReferenceConversion {
572572
| ReferenceConversionType::Result => format!("self.{}.as_ref()", field_name),
573573
}
574574
}
575+
576+
pub(crate) fn is_copy(&self) -> bool {
577+
matches!(self.conversion, ReferenceConversionType::Copy)
578+
}
575579
}
576580

577581
// FIXME: It should return a new hir::Type, but currently constructing new types is too cumbersome

0 commit comments

Comments
 (0)