Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clippy_lints/src/len_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ impl<'tcx> LateLintPass<'tcx> for LenZero {
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());
Expand Down
46 changes: 46 additions & 0 deletions tests/ui/len_without_is_empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,50 @@ impl AsyncLen {
}
}

// 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() {}