Skip to content

Commit d6b5613

Browse files
committed
fix(len_without_is_empty): allow is_empty(&self) with len(&mut self)
1 parent a27bd22 commit d6b5613

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

clippy_lints/src/len_zero.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ impl LenOutput {
495495
fn expected_is_empty_sig(len_output: LenOutput, len_self_kind: ImplicitSelfKind) -> String {
496496
let self_ref = match len_self_kind {
497497
ImplicitSelfKind::RefImm => "&",
498-
ImplicitSelfKind::RefMut => "&mut ",
498+
ImplicitSelfKind::RefMut => "&(mut) ",
499499
_ => "",
500500
};
501501
match len_output {
@@ -520,8 +520,12 @@ fn check_is_empty_sig<'tcx>(
520520
&& len_output.matches_is_empty_output(cx, *is_empty_output)
521521
{
522522
match (is_empty_self_arg.kind(), len_self_kind) {
523+
// if `len` takes `&self`, `is_empty` should do so as well
523524
(ty::Ref(_, _, Mutability::Not), ImplicitSelfKind::RefImm)
524-
| (ty::Ref(_, _, Mutability::Mut), ImplicitSelfKind::RefMut) => true,
525+
// if `len` takes `&mut self`, `is_empty` may take that _or_ `&self` (#16190)
526+
| (ty::Ref(_, _, Mutability::Mut | Mutability::Not), ImplicitSelfKind::RefMut) => true,
527+
// if len takes `self`, `is_empty` should do so as well
528+
// XXX: we might want to relax this to allow `&self` and `&mut self`
525529
(_, ImplicitSelfKind::Imm | ImplicitSelfKind::Mut) if !is_empty_self_arg.is_ref() => true,
526530
_ => false,
527531
}

tests/ui/len_without_is_empty.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,4 +473,16 @@ impl Alias2 {
473473
}
474474
}
475475

476+
// Issue #16190
477+
pub struct RefMutLenButRefIsEmpty;
478+
impl RefMutLenButRefIsEmpty {
479+
pub fn len(&mut self) -> usize {
480+
todo!()
481+
}
482+
483+
pub fn is_empty(&self) -> bool {
484+
todo!()
485+
}
486+
}
487+
476488
fn main() {}

0 commit comments

Comments
 (0)