7709: Added the check for return type of len function.
This commit is contained in:
parent
c0a2b4e826
commit
714836959b
@ -1631,6 +1631,13 @@ pub fn is_mutable_reference(&self) -> bool {
|
|||||||
matches!(self.ty.value.interned(&Interner), TyKind::Ref(hir_ty::Mutability::Mut, ..))
|
matches!(self.ty.value.interned(&Interner), TyKind::Ref(hir_ty::Mutability::Mut, ..))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_usize(&self) -> bool {
|
||||||
|
matches!(
|
||||||
|
self.ty.value.interned(&Interner),
|
||||||
|
TyKind::Scalar(Scalar::Uint(hir_ty::primitive::UintTy::Usize))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn remove_ref(&self) -> Option<Type> {
|
pub fn remove_ref(&self) -> Option<Type> {
|
||||||
match &self.ty.value.interned(&Interner) {
|
match &self.ty.value.interned(&Interner) {
|
||||||
TyKind::Ref(.., ty) => Some(self.derived(ty.clone())),
|
TyKind::Ref(.., ty) => Some(self.derived(ty.clone())),
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use hir::{known, HasSource, Name};
|
use hir::{known, HasSource, Name};
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{self, NameOwner},
|
ast::{self, NameOwner},
|
||||||
AstNode, TextRange,
|
AstNode,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@ -51,12 +51,19 @@ pub(crate) fn generate_is_empty_from_len(acc: &mut Assists, ctx: &AssistContext)
|
|||||||
}
|
}
|
||||||
|
|
||||||
let impl_ = fn_node.syntax().ancestors().find_map(ast::Impl::cast)?;
|
let impl_ = fn_node.syntax().ancestors().find_map(ast::Impl::cast)?;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
if get_impl_method(ctx, &impl_, &known::is_empty).is_some() {
|
if get_impl_method(ctx, &impl_, &known::is_empty).is_some() {
|
||||||
cov_mark::hit!(is_empty_already_implemented);
|
cov_mark::hit!(is_empty_already_implemented);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let range = get_text_range_of_len_function(ctx, &impl_)?;
|
let node = len_fn.source(ctx.sema.db)?;
|
||||||
|
let range = node.syntax().value.text_range();
|
||||||
|
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("generate_is_empty_from_len", AssistKind::Generate),
|
AssistId("generate_is_empty_from_len", AssistKind::Generate),
|
||||||
@ -89,13 +96,6 @@ fn get_impl_method(
|
|||||||
ty.iterate_method_candidates(db, krate, &traits_in_scope, Some(fn_name), |_, func| Some(func))
|
ty.iterate_method_candidates(db, krate, &traits_in_scope, Some(fn_name), |_, func| Some(func))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_text_range_of_len_function(ctx: &AssistContext, impl_: &ast::Impl) -> Option<TextRange> {
|
|
||||||
let db = ctx.sema.db;
|
|
||||||
let func = get_impl_method(ctx, impl_, &known::len)?;
|
|
||||||
let node = func.source(db)?;
|
|
||||||
Some(node.syntax().value.text_range())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::tests::{check_assist, check_assist_not_applicable};
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
||||||
@ -157,6 +157,23 @@ pub fn is_empty(&self) -> bool {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[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 {
|
||||||
|
p$0ub fn len(&self) -> u32 {
|
||||||
|
self.data.len()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn generate_is_empty() {
|
fn generate_is_empty() {
|
||||||
check_assist(
|
check_assist(
|
||||||
|
Loading…
Reference in New Issue
Block a user