diff options
| author | bors <bors@rust-lang.org> | 2023-01-30 19:08:04 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-01-30 19:08:04 +0000 |
| commit | 173fac098948b2004284a138a831ebc16799e3de (patch) | |
| tree | 06748276738a3c937e9a2ae2c6ffed6a9449fc46 | |
| parent | d92070a7b9bf450f3798dca3cad64fd055ffefd8 (diff) | |
| parent | 2fd94a4e01d9e8049df3651d0b29b3964cf235d8 (diff) | |
| download | rust-173fac098948b2004284a138a831ebc16799e3de.tar.gz rust-173fac098948b2004284a138a831ebc16799e3de.zip | |
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 -->
| -rw-r--r-- | clippy_lints/src/len_zero.rs | 1 | ||||
| -rw-r--r-- | tests/ui/len_without_is_empty.rs | 46 |
2 files changed, 47 insertions, 0 deletions
diff --git a/clippy_lints/src/len_zero.rs b/clippy_lints/src/len_zero.rs index 3c70c9cf19a..920ab7f0633 100644 --- a/clippy_lints/src/len_zero.rs +++ b/clippy_lints/src/len_zero.rs @@ -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()); diff --git a/tests/ui/len_without_is_empty.rs b/tests/ui/len_without_is_empty.rs index 78397c2af34..b5dec6c46bd 100644 --- a/tests/ui/len_without_is_empty.rs +++ b/tests/ui/len_without_is_empty.rs @@ -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() {} |
