Unnamed repository; edit this file 'description' to name the repository.
Merge #11737 #11739
11737: Emit `#[must_use]` in `Generate new` assist r=lnicola a=lnicola Closes #11736 11739: Insert #[must_use] in generate_getter r=lnicola a=Walther Inserts `#[must_use]` for the generated getter methods. From discussion in https://github.com/rust-analyzer/rust-analyzer/issues/11736 and https://github.com/rust-analyzer/rust-analyzer/pull/11738 Co-authored-by: LaurenČ›iu Nicola <[email protected]> Co-authored-by: Walther <[email protected]>
bors[bot] 2022-03-17
parent 98143da · parent 7e05e10 · parent e5adbd8 · commit 502e30e
-rw-r--r--crates/ide_assists/src/handlers/generate_getter.rs17
-rw-r--r--crates/ide_assists/src/handlers/generate_new.rs21
-rw-r--r--crates/ide_assists/src/tests/generated.rs3
3 files changed, 40 insertions, 1 deletions
diff --git a/crates/ide_assists/src/handlers/generate_getter.rs b/crates/ide_assists/src/handlers/generate_getter.rs
index c6e979c072..2e81dd13f4 100644
--- a/crates/ide_assists/src/handlers/generate_getter.rs
+++ b/crates/ide_assists/src/handlers/generate_getter.rs
@@ -39,6 +39,7 @@ use crate::{
//
// impl Person {
// /// Get a reference to the person's name.
+// #[must_use]
// fn $0name(&self) -> &str {
// self.name.as_ref()
// }
@@ -65,6 +66,7 @@ pub(crate) fn generate_getter(acc: &mut Assists, ctx: &AssistContext) -> Option<
//
// impl Person {
// /// Get a mutable reference to the person's name.
+// #[must_use]
// fn $0name_mut(&mut self) -> &mut String {
// &mut self.name
// }
@@ -143,6 +145,7 @@ pub(crate) fn generate_getter_impl(
format_to!(
buf,
" /// Get {}the {}'s {}.
+ #[must_use]
{}fn {}(&{}self) -> {} {{
{}
}}",
@@ -195,6 +198,7 @@ struct Context {
impl Context {
/// Get a reference to the context's data.
+ #[must_use]
fn $0data(&self) -> &Data {
&self.data
}
@@ -216,6 +220,7 @@ struct Context {
impl Context {
/// Get a mutable reference to the context's data.
+ #[must_use]
fn $0data_mut(&mut self) -> &mut Data {
&mut self.data
}
@@ -249,6 +254,7 @@ struct Context {
}
impl Context {
+ #[must_use]
fn data_mut(&mut self) -> &mut Data {
&mut self.data
}
@@ -273,6 +279,7 @@ pub(crate) struct Context {
impl Context {
/// Get a reference to the context's data.
+ #[must_use]
pub(crate) fn $0data(&self) -> &Data {
&self.data
}
@@ -293,6 +300,7 @@ struct Context {
impl Context {
/// Get a reference to the context's data.
+ #[must_use]
fn data(&self) -> &Data {
&self.data
}
@@ -306,11 +314,13 @@ struct Context {
impl Context {
/// Get a reference to the context's data.
+ #[must_use]
fn data(&self) -> &Data {
&self.data
}
/// Get a reference to the context's count.
+ #[must_use]
fn $0count(&self) -> &usize {
&self.count
}
@@ -337,6 +347,7 @@ struct S { foo: String }
impl S {
/// Get a reference to the s's foo.
+ #[must_use]
fn $0foo(&self) -> &String {
&self.foo
}
@@ -361,6 +372,7 @@ struct S { foo: bool }
impl S {
/// Get the s's foo.
+ #[must_use]
fn $0foo(&self) -> bool {
self.foo
}
@@ -394,6 +406,7 @@ struct S { foo: String }
impl S {
/// Get a reference to the s's foo.
+ #[must_use]
fn $0foo(&self) -> &str {
self.foo.as_ref()
}
@@ -431,6 +444,7 @@ struct S { foo: Box<Sweets> }
impl S {
/// Get a reference to the s's foo.
+ #[must_use]
fn $0foo(&self) -> &Sweets {
self.foo.as_ref()
}
@@ -464,6 +478,7 @@ struct S { foo: Vec<()> }
impl S {
/// Get a reference to the s's foo.
+ #[must_use]
fn $0foo(&self) -> &[()] {
self.foo.as_ref()
}
@@ -487,6 +502,7 @@ struct S { foo: Option<Failure> }
impl S {
/// Get a reference to the s's foo.
+ #[must_use]
fn $0foo(&self) -> Option<&Failure> {
self.foo.as_ref()
}
@@ -510,6 +526,7 @@ struct Context {
impl Context {
/// Get a reference to the context's data.
+ #[must_use]
fn $0data(&self) -> Result<&bool, &i32> {
self.data.as_ref()
}
diff --git a/crates/ide_assists/src/handlers/generate_new.rs b/crates/ide_assists/src/handlers/generate_new.rs
index 6a1f710f6d..eb9f1d04e1 100644
--- a/crates/ide_assists/src/handlers/generate_new.rs
+++ b/crates/ide_assists/src/handlers/generate_new.rs
@@ -23,6 +23,7 @@ use crate::{
// }
//
// impl<T: Clone> Ctx<T> {
+// #[must_use]
// fn $0new(data: T) -> Self { Self { data } }
// }
// ```
@@ -54,7 +55,13 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
.format(", ");
let fields = field_list.fields().filter_map(|f| f.name()).format(", ");
- format_to!(buf, " {}fn new({}) -> Self {{ Self {{ {} }} }}", vis, params, fields);
+ format_to!(
+ buf,
+ " #[must_use]\n {}fn new({}) -> Self {{ Self {{ {} }} }}",
+ vis,
+ params,
+ fields
+ );
let start_offset = impl_def
.and_then(|impl_def| find_impl_block_start(impl_def, &mut buf))
@@ -90,6 +97,7 @@ struct Foo {$0}
struct Foo {}
impl Foo {
+ #[must_use]
fn $0new() -> Self { Self { } }
}
"#,
@@ -103,6 +111,7 @@ struct Foo<T: Clone> {$0}
struct Foo<T: Clone> {}
impl<T: Clone> Foo<T> {
+ #[must_use]
fn $0new() -> Self { Self { } }
}
"#,
@@ -116,6 +125,7 @@ struct Foo<'a, T: Foo<'a>> {$0}
struct Foo<'a, T: Foo<'a>> {}
impl<'a, T: Foo<'a>> Foo<'a, T> {
+ #[must_use]
fn $0new() -> Self { Self { } }
}
"#,
@@ -129,6 +139,7 @@ struct Foo { baz: String $0}
struct Foo { baz: String }
impl Foo {
+ #[must_use]
fn $0new(baz: String) -> Self { Self { baz } }
}
"#,
@@ -142,6 +153,7 @@ struct Foo { baz: String, qux: Vec<i32> $0}
struct Foo { baz: String, qux: Vec<i32> }
impl Foo {
+ #[must_use]
fn $0new(baz: String, qux: Vec<i32>) -> Self { Self { baz, qux } }
}
"#,
@@ -159,6 +171,7 @@ struct Foo { pub baz: String, pub qux: Vec<i32> $0}
struct Foo { pub baz: String, pub qux: Vec<i32> }
impl Foo {
+ #[must_use]
fn $0new(baz: String, qux: Vec<i32>) -> Self { Self { baz, qux } }
}
"#,
@@ -178,6 +191,7 @@ impl Foo {}
struct Foo {}
impl Foo {
+ #[must_use]
fn $0new() -> Self { Self { } }
}
"#,
@@ -195,6 +209,7 @@ impl Foo {
struct Foo {}
impl Foo {
+ #[must_use]
fn $0new() -> Self { Self { } }
fn qux(&self) {}
@@ -218,6 +233,7 @@ impl Foo {
struct Foo {}
impl Foo {
+ #[must_use]
fn $0new() -> Self { Self { } }
fn qux(&self) {}
@@ -240,6 +256,7 @@ pub struct Foo {$0}
pub struct Foo {}
impl Foo {
+ #[must_use]
pub fn $0new() -> Self { Self { } }
}
"#,
@@ -253,6 +270,7 @@ pub(crate) struct Foo {$0}
pub(crate) struct Foo {}
impl Foo {
+ #[must_use]
pub(crate) fn $0new() -> Self { Self { } }
}
"#,
@@ -348,6 +366,7 @@ pub struct Source<T> {
}
impl<T> Source<T> {
+ #[must_use]
pub fn $0new(file_id: HirFileId, ast: T) -> Self { Self { file_id, ast } }
pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> {
diff --git a/crates/ide_assists/src/tests/generated.rs b/crates/ide_assists/src/tests/generated.rs
index 485b807d05..53e5d08590 100644
--- a/crates/ide_assists/src/tests/generated.rs
+++ b/crates/ide_assists/src/tests/generated.rs
@@ -1036,6 +1036,7 @@ struct Person {
impl Person {
/// Get a reference to the person's name.
+ #[must_use]
fn $0name(&self) -> &str {
self.name.as_ref()
}
@@ -1060,6 +1061,7 @@ struct Person {
impl Person {
/// Get a mutable reference to the person's name.
+ #[must_use]
fn $0name_mut(&mut self) -> &mut String {
&mut self.name
}
@@ -1133,6 +1135,7 @@ struct Ctx<T: Clone> {
}
impl<T: Clone> Ctx<T> {
+ #[must_use]
fn $0new(data: T) -> Self { Self { data } }
}
"#####,