Skip to content

Commit 173fac0

Browse files
committed
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](#10255) <!-- changelog_checked -->
2 parents d92070a + 2fd94a4 commit 173fac0

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

clippy_lints/src/len_zero.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ impl<'tcx> LateLintPass<'tcx> for LenZero {
135135
if item.ident.name == sym::len;
136136
if let ImplItemKind::Fn(sig, _) = &item.kind;
137137
if sig.decl.implicit_self.has_implicit_self();
138+
if sig.decl.inputs.len() == 1;
138139
if cx.effective_visibilities.is_exported(item.owner_id.def_id);
139140
if matches!(sig.decl.output, FnRetTy::Return(_));
140141
if let Some(imp) = get_parent_as_impl(cx.tcx, item.hir_id());

tests/ui/len_without_is_empty.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,50 @@ impl AsyncLen {
282282
}
283283
}
284284

285+
// issue #9520
286+
pub struct NonStandardLenAndIsEmptySignature;
287+
impl NonStandardLenAndIsEmptySignature {
288+
// don't lint
289+
pub fn len(&self, something: usize) -> usize {
290+
something
291+
}
292+
293+
pub fn is_empty(&self, something: usize) -> bool {
294+
something == 0
295+
}
296+
}
297+
298+
// test case for #9520 with generics in the function signature
299+
pub trait TestResource {
300+
type NonStandardSignatureWithGenerics: Copy;
301+
fn lookup_content(&self, item: Self::NonStandardSignatureWithGenerics) -> Result<Option<&[u8]>, String>;
302+
}
303+
pub struct NonStandardSignatureWithGenerics(u32);
304+
impl NonStandardSignatureWithGenerics {
305+
pub fn is_empty<T, U>(self, resource: &T) -> bool
306+
where
307+
T: TestResource<NonStandardSignatureWithGenerics = U>,
308+
U: Copy + From<NonStandardSignatureWithGenerics>,
309+
{
310+
if let Ok(Some(content)) = resource.lookup_content(self.into()) {
311+
content.is_empty()
312+
} else {
313+
true
314+
}
315+
}
316+
317+
// test case for #9520 with generics in the function signature
318+
pub fn len<T, U>(self, resource: &T) -> usize
319+
where
320+
T: TestResource<NonStandardSignatureWithGenerics = U>,
321+
U: Copy + From<NonStandardSignatureWithGenerics>,
322+
{
323+
if let Ok(Some(content)) = resource.lookup_content(self.into()) {
324+
content.len()
325+
} else {
326+
0_usize
327+
}
328+
}
329+
}
330+
285331
fn main() {}

0 commit comments

Comments
 (0)