Skip to content

Commit

Permalink
Auto merge of #10255 - khayyamsaleem:issue_9520, r=Jarcho
Browse files Browse the repository at this point in the history
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](#10255)
<!-- changelog_checked -->
  • Loading branch information
bors committed Jan 30, 2023
2 parents d92070a + 2fd94a4 commit 173fac0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
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() {}

0 comments on commit 173fac0

Please sign in to comment.