Skip to content

Commit

Permalink
Auto merge of #5933 - mikerite:fix-5927, r=matthiaskrgr
Browse files Browse the repository at this point in the history
Fix false negative in `option_as_ref_deref`

Closes #5927

changelog: Fix false negative in `option_as_ref_deref`
  • Loading branch information
bors committed Aug 21, 2020
2 parents 1a26dbf + 11efd75 commit aa3b04f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
7 changes: 6 additions & 1 deletion clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/option_as_ref_deref.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ fn main() {

let _ = opt.as_deref();
let _ = opt.as_deref_mut();

// Issue #5927
let _ = opt.as_deref();
}
3 changes: 3 additions & 0 deletions tests/ui/option_as_ref_deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
8 changes: 7 additions & 1 deletion tests/ui/option_as_ref_deref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit aa3b04f

Please sign in to comment.