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.
This commit is contained in:
bors 2020-05-25 20:15:34 +00:00
commit dd06d2983f
3 changed files with 33 additions and 0 deletions

View File

@ -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.kind {
if path.ident.as_str() == "len" {
let ty = walk_ptrs_ty(cx.tables.expr_ty(&args[0]));
match ty.kind {
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"),

View File

@ -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

View File

@ -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