Fix false negative in option_as_ref_deref

This commit is contained in:
Michael Wright 2020-08-21 07:23:04 +02:00
parent 4104611913
commit 11efd75aeb
4 changed files with 19 additions and 2 deletions

View File

@ -3421,7 +3421,12 @@ fn lint_option_as_ref_deref<'tcx>(
];
let is_deref = match map_args[1].kind {
hir::ExprKind::Path(ref expr_qpath) => deref_aliases.iter().any(|path| match_qpath(expr_qpath, path)),
hir::ExprKind::Path(ref expr_qpath) => cx
.qpath_res(expr_qpath, map_args[1].hir_id)
.opt_def_id()
.map_or(false, |fun_def_id| {
deref_aliases.iter().any(|path| match_def_path(cx, fun_def_id, path))
}),
hir::ExprKind::Closure(_, _, body_id, _, _) => {
let closure_body = cx.tcx.hir().body(body_id);
let closure_expr = remove_blocks(&closure_body.value);

View File

@ -38,4 +38,7 @@ fn main() {
let _ = opt.as_deref();
let _ = opt.as_deref_mut();
// Issue #5927
let _ = opt.as_deref();
}

View File

@ -41,4 +41,7 @@ fn main() {
let _ = opt.as_ref().map(|x| &**x);
let _ = opt.as_mut().map(|x| &mut **x);
// Issue #5927
let _ = opt.as_ref().map(std::ops::Deref::deref);
}

View File

@ -100,5 +100,11 @@ error: called `.as_mut().map(|x| &mut **x)` on an Option value. This can be done
LL | let _ = opt.as_mut().map(|x| &mut **x);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `opt.as_deref_mut()`
error: aborting due to 16 previous errors
error: called `.as_ref().map(std::ops::Deref::deref)` on an Option value. This can be done more directly by calling `opt.as_deref()` instead
--> $DIR/option_as_ref_deref.rs:46:13
|
LL | let _ = opt.as_ref().map(std::ops::Deref::deref);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.as_deref()`
error: aborting due to 17 previous errors