Skip to content

Commit 2ca3834

Browse files
Merge #11062
11062: fix: Don't say "a reference to" for `Copy` types in the generate getter assist r=Veykril a=patrick-gu 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. To determine whether the type is `Copy`, I have added an `is_copy` method to `ReferenceConversion`. Co-authored-by: patrick-gu <[email protected]>
2 parents 0add6e9 + 76b50f1 commit 2ca3834

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
@@ -571,6 +571,10 @@ impl ReferenceConversion {
571571
| ReferenceConversionType::Result => format!("self.{}.as_ref()", field_name),
572572
}
573573
}
574+
575+
pub(crate) fn is_copy(&self) -> bool {
576+
matches!(self.conversion, ReferenceConversionType::Copy)
577+
}
574578
}
575579

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

0 commit comments

Comments
 (0)