Skip to content

Commit aa3b04f

Browse files
committed
Auto merge of rust-lang#5933 - mikerite:fix-5927, r=matthiaskrgr
Fix false negative in `option_as_ref_deref` Closes rust-lang#5927 changelog: Fix false negative in `option_as_ref_deref`
2 parents 1a26dbf + 11efd75 commit aa3b04f

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3421,7 +3421,12 @@ fn lint_option_as_ref_deref<'tcx>(
34213421
];
34223422

34233423
let is_deref = match map_args[1].kind {
3424-
hir::ExprKind::Path(ref expr_qpath) => deref_aliases.iter().any(|path| match_qpath(expr_qpath, path)),
3424+
hir::ExprKind::Path(ref expr_qpath) => cx
3425+
.qpath_res(expr_qpath, map_args[1].hir_id)
3426+
.opt_def_id()
3427+
.map_or(false, |fun_def_id| {
3428+
deref_aliases.iter().any(|path| match_def_path(cx, fun_def_id, path))
3429+
}),
34253430
hir::ExprKind::Closure(_, _, body_id, _, _) => {
34263431
let closure_body = cx.tcx.hir().body(body_id);
34273432
let closure_expr = remove_blocks(&closure_body.value);

tests/ui/option_as_ref_deref.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,7 @@ fn main() {
3838

3939
let _ = opt.as_deref();
4040
let _ = opt.as_deref_mut();
41+
42+
// Issue #5927
43+
let _ = opt.as_deref();
4144
}

tests/ui/option_as_ref_deref.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,7 @@ fn main() {
4141

4242
let _ = opt.as_ref().map(|x| &**x);
4343
let _ = opt.as_mut().map(|x| &mut **x);
44+
45+
// Issue #5927
46+
let _ = opt.as_ref().map(std::ops::Deref::deref);
4447
}

tests/ui/option_as_ref_deref.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,11 @@ error: called `.as_mut().map(|x| &mut **x)` on an Option value. This can be done
100100
LL | let _ = opt.as_mut().map(|x| &mut **x);
101101
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `opt.as_deref_mut()`
102102

103-
error: aborting due to 16 previous errors
103+
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
104+
--> $DIR/option_as_ref_deref.rs:46:13
105+
|
106+
LL | let _ = opt.as_ref().map(std::ops::Deref::deref);
107+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.as_deref()`
108+
109+
error: aborting due to 17 previous errors
104110

0 commit comments

Comments
 (0)