2021-03-15 12:18:50 -05:00
|
|
|
use hir::{known, HasSource, Name};
|
2021-03-11 12:55:22 -06:00
|
|
|
use syntax::{
|
2021-09-27 05:54:24 -05:00
|
|
|
ast::{self, HasName},
|
2021-03-15 14:46:59 -05:00
|
|
|
AstNode,
|
2021-03-11 12:55:22 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
assist_context::{AssistContext, Assists},
|
|
|
|
AssistId, AssistKind,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Assist: generate_is_empty_from_len
|
|
|
|
//
|
|
|
|
// Generates is_empty implementation from the len method.
|
|
|
|
//
|
|
|
|
// ```
|
2021-03-15 12:18:50 -05:00
|
|
|
// struct MyStruct { data: Vec<String> }
|
|
|
|
//
|
2021-03-11 12:55:22 -06:00
|
|
|
// impl MyStruct {
|
2022-03-17 08:03:02 -05:00
|
|
|
// #[must_use]
|
2021-03-11 12:55:22 -06:00
|
|
|
// p$0ub fn len(&self) -> usize {
|
|
|
|
// self.data.len()
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
2021-03-15 12:18:50 -05:00
|
|
|
// struct MyStruct { data: Vec<String> }
|
|
|
|
//
|
2021-03-11 12:55:22 -06:00
|
|
|
// impl MyStruct {
|
2022-03-17 08:03:02 -05:00
|
|
|
// #[must_use]
|
2021-03-11 12:55:22 -06:00
|
|
|
// pub fn len(&self) -> usize {
|
|
|
|
// self.data.len()
|
|
|
|
// }
|
|
|
|
//
|
2022-03-17 08:03:02 -05:00
|
|
|
// #[must_use]
|
2021-03-11 12:55:22 -06:00
|
|
|
// pub fn is_empty(&self) -> bool {
|
|
|
|
// self.len() == 0
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
2022-07-20 08:02:08 -05:00
|
|
|
pub(crate) fn generate_is_empty_from_len(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
2021-03-11 12:55:22 -06:00
|
|
|
let fn_node = ctx.find_node_at_offset::<ast::Fn>()?;
|
|
|
|
let fn_name = fn_node.name()?;
|
|
|
|
|
|
|
|
if fn_name.text() != "len" {
|
|
|
|
cov_mark::hit!(len_function_not_present);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if fn_node.param_list()?.params().next().is_some() {
|
|
|
|
cov_mark::hit!(len_function_with_parameters);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-03-15 12:18:50 -05:00
|
|
|
let impl_ = fn_node.syntax().ancestors().find_map(ast::Impl::cast)?;
|
2021-03-15 14:46:59 -05:00
|
|
|
let len_fn = get_impl_method(ctx, &impl_, &known::len)?;
|
|
|
|
if !len_fn.ret_type(ctx.sema.db).is_usize() {
|
|
|
|
cov_mark::hit!(len_fn_different_return_type);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-03-15 12:18:50 -05:00
|
|
|
if get_impl_method(ctx, &impl_, &known::is_empty).is_some() {
|
2021-03-11 12:55:22 -06:00
|
|
|
cov_mark::hit!(is_empty_already_implemented);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-03-15 14:46:59 -05:00
|
|
|
let node = len_fn.source(ctx.sema.db)?;
|
|
|
|
let range = node.syntax().value.text_range();
|
2021-03-11 12:55:22 -06:00
|
|
|
|
|
|
|
acc.add(
|
|
|
|
AssistId("generate_is_empty_from_len", AssistKind::Generate),
|
|
|
|
"Generate a is_empty impl from a len function",
|
|
|
|
range,
|
|
|
|
|builder| {
|
2021-03-15 12:18:50 -05:00
|
|
|
let code = r#"
|
|
|
|
|
2022-03-17 08:03:02 -05:00
|
|
|
#[must_use]
|
2021-03-15 12:18:50 -05:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.len() == 0
|
|
|
|
}"#
|
|
|
|
.to_string();
|
2021-03-11 12:55:22 -06:00
|
|
|
builder.insert(range.end(), code)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-15 12:18:50 -05:00
|
|
|
fn get_impl_method(
|
2022-07-20 08:02:08 -05:00
|
|
|
ctx: &AssistContext<'_>,
|
2021-03-15 12:18:50 -05:00
|
|
|
impl_: &ast::Impl,
|
|
|
|
fn_name: &Name,
|
|
|
|
) -> Option<hir::Function> {
|
2021-03-11 12:55:22 -06:00
|
|
|
let db = ctx.sema.db;
|
2021-03-15 12:18:50 -05:00
|
|
|
let impl_def: hir::Impl = ctx.sema.to_def(impl_)?;
|
2021-03-11 12:55:22 -06:00
|
|
|
|
2022-03-31 04:12:08 -05:00
|
|
|
let scope = ctx.sema.scope(impl_.syntax())?;
|
2021-03-29 10:46:33 -05:00
|
|
|
let ty = impl_def.self_ty(db);
|
2022-05-05 15:21:42 -05:00
|
|
|
ty.iterate_method_candidates(
|
|
|
|
db,
|
|
|
|
&scope,
|
|
|
|
&scope.visible_traits().0,
|
|
|
|
None,
|
|
|
|
Some(fn_name),
|
|
|
|
|func| Some(func),
|
|
|
|
)
|
2021-03-11 12:55:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn len_function_not_present() {
|
|
|
|
cov_mark::check!(len_function_not_present);
|
|
|
|
check_assist_not_applicable(
|
|
|
|
generate_is_empty_from_len,
|
|
|
|
r#"
|
2021-03-15 12:18:50 -05:00
|
|
|
struct MyStruct { data: Vec<String> }
|
|
|
|
|
2021-03-11 12:55:22 -06:00
|
|
|
impl MyStruct {
|
|
|
|
p$0ub fn test(&self) -> usize {
|
|
|
|
self.data.len()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn len_function_with_parameters() {
|
|
|
|
cov_mark::check!(len_function_with_parameters);
|
|
|
|
check_assist_not_applicable(
|
|
|
|
generate_is_empty_from_len,
|
|
|
|
r#"
|
2021-03-15 12:18:50 -05:00
|
|
|
struct MyStruct { data: Vec<String> }
|
|
|
|
|
2021-03-11 12:55:22 -06:00
|
|
|
impl MyStruct {
|
2022-03-17 08:03:02 -05:00
|
|
|
#[must_use]
|
2021-03-11 12:55:22 -06:00
|
|
|
p$0ub fn len(&self, _i: bool) -> usize {
|
|
|
|
self.data.len()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn is_empty_already_implemented() {
|
|
|
|
cov_mark::check!(is_empty_already_implemented);
|
|
|
|
check_assist_not_applicable(
|
|
|
|
generate_is_empty_from_len,
|
|
|
|
r#"
|
2021-03-15 12:18:50 -05:00
|
|
|
struct MyStruct { data: Vec<String> }
|
|
|
|
|
2021-03-11 12:55:22 -06:00
|
|
|
impl MyStruct {
|
2022-03-17 08:03:02 -05:00
|
|
|
#[must_use]
|
2021-03-11 12:55:22 -06:00
|
|
|
p$0ub fn len(&self) -> usize {
|
|
|
|
self.data.len()
|
|
|
|
}
|
|
|
|
|
2022-03-17 08:03:02 -05:00
|
|
|
#[must_use]
|
2021-03-11 12:55:22 -06:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.len() == 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-15 14:46:59 -05:00
|
|
|
#[test]
|
|
|
|
fn len_fn_different_return_type() {
|
|
|
|
cov_mark::check!(len_fn_different_return_type);
|
|
|
|
check_assist_not_applicable(
|
|
|
|
generate_is_empty_from_len,
|
|
|
|
r#"
|
|
|
|
struct MyStruct { data: Vec<String> }
|
|
|
|
|
|
|
|
impl MyStruct {
|
2022-03-17 08:03:02 -05:00
|
|
|
#[must_use]
|
2021-03-15 14:46:59 -05:00
|
|
|
p$0ub fn len(&self) -> u32 {
|
|
|
|
self.data.len()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-11 12:55:22 -06:00
|
|
|
#[test]
|
|
|
|
fn generate_is_empty() {
|
|
|
|
check_assist(
|
|
|
|
generate_is_empty_from_len,
|
|
|
|
r#"
|
2021-03-15 12:18:50 -05:00
|
|
|
struct MyStruct { data: Vec<String> }
|
|
|
|
|
2021-03-11 12:55:22 -06:00
|
|
|
impl MyStruct {
|
2022-03-17 08:03:02 -05:00
|
|
|
#[must_use]
|
2021-03-11 12:55:22 -06:00
|
|
|
p$0ub fn len(&self) -> usize {
|
|
|
|
self.data.len()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
2021-03-15 12:18:50 -05:00
|
|
|
struct MyStruct { data: Vec<String> }
|
|
|
|
|
2021-03-11 12:55:22 -06:00
|
|
|
impl MyStruct {
|
2022-03-17 08:03:02 -05:00
|
|
|
#[must_use]
|
2021-03-11 12:55:22 -06:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.data.len()
|
|
|
|
}
|
|
|
|
|
2022-03-17 08:03:02 -05:00
|
|
|
#[must_use]
|
2021-03-11 12:55:22 -06:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.len() == 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_functions_in_impl() {
|
|
|
|
check_assist(
|
|
|
|
generate_is_empty_from_len,
|
|
|
|
r#"
|
2021-03-15 12:18:50 -05:00
|
|
|
struct MyStruct { data: Vec<String> }
|
|
|
|
|
2021-03-11 12:55:22 -06:00
|
|
|
impl MyStruct {
|
2022-03-17 08:03:02 -05:00
|
|
|
#[must_use]
|
2021-03-11 12:55:22 -06:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self { data: 0 }
|
|
|
|
}
|
|
|
|
|
2022-03-17 08:03:02 -05:00
|
|
|
#[must_use]
|
2021-03-11 12:55:22 -06:00
|
|
|
p$0ub fn len(&self) -> usize {
|
|
|
|
self.data.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn work(&self) -> Option<usize> {
|
2021-03-15 12:18:50 -05:00
|
|
|
|
2021-03-11 12:55:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
2021-03-15 12:18:50 -05:00
|
|
|
struct MyStruct { data: Vec<String> }
|
|
|
|
|
2021-03-11 12:55:22 -06:00
|
|
|
impl MyStruct {
|
2022-03-17 08:03:02 -05:00
|
|
|
#[must_use]
|
2021-03-11 12:55:22 -06:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self { data: 0 }
|
|
|
|
}
|
|
|
|
|
2022-03-17 08:03:02 -05:00
|
|
|
#[must_use]
|
2021-03-11 12:55:22 -06:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.data.len()
|
|
|
|
}
|
|
|
|
|
2022-03-17 08:03:02 -05:00
|
|
|
#[must_use]
|
2021-03-11 12:55:22 -06:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.len() == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn work(&self) -> Option<usize> {
|
2021-03-15 12:18:50 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_impls() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
generate_is_empty_from_len,
|
|
|
|
r#"
|
|
|
|
struct MyStruct { data: Vec<String> }
|
|
|
|
|
|
|
|
impl MyStruct {
|
2022-03-17 08:03:02 -05:00
|
|
|
#[must_use]
|
2021-03-15 12:18:50 -05:00
|
|
|
p$0ub fn len(&self) -> usize {
|
|
|
|
self.data.len()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MyStruct {
|
2022-03-17 08:03:02 -05:00
|
|
|
#[must_use]
|
2021-03-15 12:18:50 -05:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.len() == 0
|
2021-03-11 12:55:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|