about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorRyan Levick <me@ryanlevick.com>2021-02-16 22:39:05 +0100
committerRyan Levick <me@ryanlevick.com>2021-03-03 11:23:29 +0100
commit1999a3147f5ab65cd556d45e631be5c18fbaebf4 (patch)
tree93caf2a0a440349e20cc3371ea67c8f50b9d2a55 /compiler
parentda3995f0ec3085de42dcce9e91dbb5662b2c99d3 (diff)
downloadrust-1999a3147f5ab65cd556d45e631be5c18fbaebf4.tar.gz
rust-1999a3147f5ab65cd556d45e631be5c18fbaebf4.zip
Fix borrow and deref
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_lint/src/noop_method_call.rs18
-rw-r--r--compiler/rustc_span/src/symbol.rs3
2 files changed, 9 insertions, 12 deletions
diff --git a/compiler/rustc_lint/src/noop_method_call.rs b/compiler/rustc_lint/src/noop_method_call.rs
index 335c3c575e7..3a1b9c85fd1 100644
--- a/compiler/rustc_lint/src/noop_method_call.rs
+++ b/compiler/rustc_lint/src/noop_method_call.rs
@@ -15,6 +15,7 @@ declare_lint! {
     ///
     /// ```rust
     /// # #![allow(unused)]
+    /// #![deny(noop_method_call)]
     /// struct Foo;
     /// let foo = &Foo;
     /// let clone: &Foo = foo.clone();
@@ -30,7 +31,7 @@ declare_lint! {
     /// calling `clone` on a `&T` where `T` does not implement clone, actually doesn't do anything
     /// as references are copy. This lint detects these calls and warns the user about them.
     pub NOOP_METHOD_CALL,
-    Warn,
+    Allow,
     "detects the use of well-known noop methods"
 }
 
@@ -50,7 +51,9 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
             Some((DefKind::AssocFn, did)) => match cx.tcx.trait_of_item(did) {
                 // Check that we're dealing with a trait method for one of the traits we care about.
                 Some(trait_id)
-                    if [sym::Clone].iter().any(|s| cx.tcx.is_diagnostic_item(*s, trait_id)) =>
+                    if [sym::Clone, sym::Deref, sym::Borrow]
+                        .iter()
+                        .any(|s| cx.tcx.is_diagnostic_item(*s, trait_id)) =>
                 {
                     (trait_id, did)
                 }
@@ -71,20 +74,11 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
             _ => return,
         };
         // (Re)check that it implements the noop diagnostic.
-        for (s, peel_ref) in [(sym::noop_method_clone, false)].iter() {
+        for s in [sym::noop_method_clone, sym::noop_method_deref, sym::noop_method_borrow].iter() {
             if cx.tcx.is_diagnostic_item(*s, i.def_id()) {
                 let method = &call.ident.name;
                 let receiver = &elements[0];
                 let receiver_ty = cx.typeck_results().expr_ty(receiver);
-                let receiver_ty = match receiver_ty.kind() {
-                    // Remove one borrow from the receiver if appropriate to positively verify that
-                    // the receiver `&self` type and the return type are the same, depending on the
-                    // involved trait being checked.
-                    ty::Ref(_, ty, _) if *peel_ref => ty,
-                    // When it comes to `Clone` we need to check the `receiver_ty` directly.
-                    // FIXME: we must come up with a better strategy for this.
-                    _ => receiver_ty,
-                };
                 let expr_ty = cx.typeck_results().expr_ty_adjusted(expr);
                 if receiver_ty != expr_ty {
                     // This lint will only trigger if the receiver type and resulting expression \
diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs
index f43b180e063..61f9a080a52 100644
--- a/compiler/rustc_span/src/symbol.rs
+++ b/compiler/rustc_span/src/symbol.rs
@@ -142,6 +142,7 @@ symbols! {
         Decodable,
         Decoder,
         Default,
+        Deref,
         Encodable,
         Encoder,
         Eq,
@@ -790,7 +791,9 @@ symbols! {
         none_error,
         nontemporal_store,
         nontrapping_dash_fptoint: "nontrapping-fptoint",
+        noop_method_borrow,
         noop_method_clone,
+        noop_method_deref,
         noreturn,
         nostack,
         not,