Skip to content

Commit

Permalink
Auto merge of #4429 - jeremystucki:or_fun_call, r=flip1995
Browse files Browse the repository at this point in the history
Update 'or_fun_call' to ignore calls to len

Resolves #1653

changelog: Update `or_fun_call`: Allow calls to `len` for Slice, Array & Vec.
  • Loading branch information
bors committed May 25, 2020
2 parents ce86f90 + d9b8508 commit f7c486c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
15 changes: 15 additions & 0 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,21 @@ fn lint_or_fun_call<'a, 'tcx>(
or_has_args: bool,
span: Span,
) {
if let hir::ExprKind::MethodCall(ref path, _, ref args) = &arg.node {
if path.ident.as_str() == "len" {
let ty = walk_ptrs_ty(cx.tables.expr_ty(&args[0]));

match ty.sty {
ty::Slice(_) | ty::Array(_, _) => return,
_ => (),
}

if match_type(cx, ty, &paths::VEC) {
return;
}
}
}

// (path, fn_has_argument, methods, suffix)
let know_types: &[(&[_], _, &[_], _)] = &[
(&paths::BTREEMAP_ENTRY, false, &["or_insert"], "with"),
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/or_fun_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ fn test_or_with_ctors() {
let b = "b".to_string();
let _ = Some(Bar("a".to_string(), Duration::from_secs(1)))
.or(Some(Bar(b, Duration::from_secs(2))));

let vec = vec!["foo"];
let _ = opt.ok_or(vec.len());

let array = ["foo"];
let _ = opt.ok_or(array.len());

let slice = &["foo"][..];
let _ = opt.ok_or(slice.len());
}

// Issue 4514 - early return
Expand Down

0 comments on commit f7c486c

Please sign in to comment.