Auto merge of #10255 - khayyamsaleem:issue_9520, r=Jarcho

prevents `len_without_is_empty` from yielding positive when `len` takes arguments besides `&self`

Fixes #9520

---

changelog: FP [`len_without_is_empty`]: No longer lints, if `len` as a non-default signature
[#10255](https://github.com/rust-lang/rust-clippy/pull/10255)
<!-- changelog_checked -->
This commit is contained in:
bors 2023-01-30 19:08:04 +00:00
commit 173fac0989
2 changed files with 47 additions and 0 deletions

View File

@ -135,6 +135,7 @@ fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'_>)
if item.ident.name == sym::len;
if let ImplItemKind::Fn(sig, _) = &item.kind;
if sig.decl.implicit_self.has_implicit_self();
if sig.decl.inputs.len() == 1;
if cx.effective_visibilities.is_exported(item.owner_id.def_id);
if matches!(sig.decl.output, FnRetTy::Return(_));
if let Some(imp) = get_parent_as_impl(cx.tcx, item.hir_id());

View File

@ -282,4 +282,50 @@ pub async fn is_empty(&self) -> bool {
}
}
// issue #9520
pub struct NonStandardLenAndIsEmptySignature;
impl NonStandardLenAndIsEmptySignature {
// don't lint
pub fn len(&self, something: usize) -> usize {
something
}
pub fn is_empty(&self, something: usize) -> bool {
something == 0
}
}
// test case for #9520 with generics in the function signature
pub trait TestResource {
type NonStandardSignatureWithGenerics: Copy;
fn lookup_content(&self, item: Self::NonStandardSignatureWithGenerics) -> Result<Option<&[u8]>, String>;
}
pub struct NonStandardSignatureWithGenerics(u32);
impl NonStandardSignatureWithGenerics {
pub fn is_empty<T, U>(self, resource: &T) -> bool
where
T: TestResource<NonStandardSignatureWithGenerics = U>,
U: Copy + From<NonStandardSignatureWithGenerics>,
{
if let Ok(Some(content)) = resource.lookup_content(self.into()) {
content.is_empty()
} else {
true
}
}
// test case for #9520 with generics in the function signature
pub fn len<T, U>(self, resource: &T) -> usize
where
T: TestResource<NonStandardSignatureWithGenerics = U>,
U: Copy + From<NonStandardSignatureWithGenerics>,
{
if let Ok(Some(content)) = resource.lookup_content(self.into()) {
content.len()
} else {
0_usize
}
}
}
fn main() {}