Skip to content

Commit dd06d29

Browse files
committed
Auto merge of #4429 - jeremystucki:or_fun_call, r=flip1995
Update 'or_fun_call' to ignore calls to len Resolves #1653 changelog: Update `or_fun_call`: Allow calls to `len` for Slice, Array & Vec.
2 parents acd484e + d9f5532 commit dd06d29

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,21 @@ fn lint_or_fun_call<'a, 'tcx>(
16141614
or_has_args: bool,
16151615
span: Span,
16161616
) {
1617+
if let hir::ExprKind::MethodCall(ref path, _, ref args) = &arg.kind {
1618+
if path.ident.as_str() == "len" {
1619+
let ty = walk_ptrs_ty(cx.tables.expr_ty(&args[0]));
1620+
1621+
match ty.kind {
1622+
ty::Slice(_) | ty::Array(_, _) => return,
1623+
_ => (),
1624+
}
1625+
1626+
if match_type(cx, ty, &paths::VEC) {
1627+
return;
1628+
}
1629+
}
1630+
}
1631+
16171632
// (path, fn_has_argument, methods, suffix)
16181633
let know_types: &[(&[_], _, &[_], _)] = &[
16191634
(&paths::BTREEMAP_ENTRY, false, &["or_insert"], "with"),

tests/ui/or_fun_call.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ fn test_or_with_ctors() {
9595
let b = "b".to_string();
9696
let _ = Some(Bar("a".to_string(), Duration::from_secs(1)))
9797
.or(Some(Bar(b, Duration::from_secs(2))));
98+
99+
let vec = vec!["foo"];
100+
let _ = opt.ok_or(vec.len());
101+
102+
let array = ["foo"];
103+
let _ = opt.ok_or(array.len());
104+
105+
let slice = &["foo"][..];
106+
let _ = opt.ok_or(slice.len());
98107
}
99108

100109
// Issue 4514 - early return

tests/ui/or_fun_call.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ fn test_or_with_ctors() {
9595
let b = "b".to_string();
9696
let _ = Some(Bar("a".to_string(), Duration::from_secs(1)))
9797
.or(Some(Bar(b, Duration::from_secs(2))));
98+
99+
let vec = vec!["foo"];
100+
let _ = opt.ok_or(vec.len());
101+
102+
let array = ["foo"];
103+
let _ = opt.ok_or(array.len());
104+
105+
let slice = &["foo"][..];
106+
let _ = opt.ok_or(slice.len());
98107
}
99108

100109
// Issue 4514 - early return

0 commit comments

Comments
 (0)