about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Wright <mikerite@lavabit.com>2020-08-21 07:23:04 +0200
committerMichael Wright <mikerite@lavabit.com>2020-08-21 07:23:04 +0200
commit11efd75aeb8637bd5444104a57f1051b86c0d62b (patch)
treea5239ebda6142fb842d837a9a7d8363d5401a24f
parent410461191393e2339e79b33fa8baf4650b905db0 (diff)
downloadrust-11efd75aeb8637bd5444104a57f1051b86c0d62b.tar.gz
rust-11efd75aeb8637bd5444104a57f1051b86c0d62b.zip
Fix false negative in `option_as_ref_deref`
-rw-r--r--clippy_lints/src/methods/mod.rs7
-rw-r--r--tests/ui/option_as_ref_deref.fixed3
-rw-r--r--tests/ui/option_as_ref_deref.rs3
-rw-r--r--tests/ui/option_as_ref_deref.stderr8
4 files changed, 19 insertions, 2 deletions
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs
index 2498c48f067..22942d9fb0c 100644
--- a/clippy_lints/src/methods/mod.rs
+++ b/clippy_lints/src/methods/mod.rs
@@ -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);
diff --git a/tests/ui/option_as_ref_deref.fixed b/tests/ui/option_as_ref_deref.fixed
index 076692e6445..07d7f0b45b0 100644
--- a/tests/ui/option_as_ref_deref.fixed
+++ b/tests/ui/option_as_ref_deref.fixed
@@ -38,4 +38,7 @@ fn main() {
 
     let _ = opt.as_deref();
     let _ = opt.as_deref_mut();
+
+    // Issue #5927
+    let _ = opt.as_deref();
 }
diff --git a/tests/ui/option_as_ref_deref.rs b/tests/ui/option_as_ref_deref.rs
index 3bf5f715f83..6ae059c9425 100644
--- a/tests/ui/option_as_ref_deref.rs
+++ b/tests/ui/option_as_ref_deref.rs
@@ -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);
 }
diff --git a/tests/ui/option_as_ref_deref.stderr b/tests/ui/option_as_ref_deref.stderr
index a106582a633..62f28232475 100644
--- a/tests/ui/option_as_ref_deref.stderr
+++ b/tests/ui/option_as_ref_deref.stderr
@@ -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