Skip to content

Commit 26c0f97

Browse files
committed
[len_without_is_empty]: follow type alias
1 parent b9906ac commit 26c0f97

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

clippy_lints/src/len_zero.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,14 @@ fn check_for_is_empty(
424424
item_name: Symbol,
425425
item_kind: &str,
426426
) {
427+
// Implementor may be a type alias, in which case we need to get the `DefId` of the aliased type to
428+
// find the correct inherent impls.
429+
let impl_ty = if let Some(adt) = cx.tcx.type_of(impl_ty).skip_binder().ty_adt_def() {
430+
adt.did()
431+
} else {
432+
return;
433+
};
434+
427435
let is_empty = Symbol::intern("is_empty");
428436
let is_empty = cx
429437
.tcx

tests/ui/len_without_is_empty.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,4 +436,27 @@ impl DifferingErrors {
436436
}
437437
}
438438

439+
// Issue #11165
440+
pub struct Aliased1;
441+
pub type Alias1 = Aliased1;
442+
443+
impl Alias1 {
444+
pub fn len(&self) -> usize {
445+
todo!()
446+
}
447+
448+
pub fn is_empty(&self) -> bool {
449+
todo!()
450+
}
451+
}
452+
453+
pub struct Aliased2;
454+
pub type Alias2 = Aliased2;
455+
impl Alias2 {
456+
pub fn len(&self) -> usize {
457+
//~^ ERROR: type `Alias2` has a public `len` method, but no `is_empty` method
458+
todo!()
459+
}
460+
}
461+
439462
fn main() {}

tests/ui/len_without_is_empty.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,11 @@ error: struct `AsyncResultLenWithoutIsEmpty` has a public `len` method, but no `
139139
LL | pub async fn len(&self) -> Result<usize, ()> {
140140
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
141141

142-
error: aborting due to 15 previous errors
142+
error: type `Alias2` has a public `len` method, but no `is_empty` method
143+
--> $DIR/len_without_is_empty.rs:456:5
144+
|
145+
LL | pub fn len(&self) -> usize {
146+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
147+
148+
error: aborting due to 16 previous errors
143149

0 commit comments

Comments
 (0)