about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRyan Levick <me@ryanlevick.com>2021-02-16 15:12:19 +0100
committerRyan Levick <me@ryanlevick.com>2021-03-03 11:23:14 +0100
commitda3995f0ec3085de42dcce9e91dbb5662b2c99d3 (patch)
tree2c7481d1f267b94a86b97872b149cdbee15c71be
parent6bf6652616ca75dde30cbdd021942433ae519730 (diff)
downloadrust-da3995f0ec3085de42dcce9e91dbb5662b2c99d3.tar.gz
rust-da3995f0ec3085de42dcce9e91dbb5662b2c99d3.zip
Remove lint pass on borrow and deref
-rw-r--r--compiler/rustc_lint/src/noop_method_call.rs12
-rw-r--r--compiler/rustc_span/src/symbol.rs3
-rw-r--r--library/core/src/borrow.rs2
-rw-r--r--library/core/src/ops/deref.rs2
-rw-r--r--src/test/ui/lint/noop-method-call.rs46
-rw-r--r--src/test/ui/lint/noop-method-call.stderr34
6 files changed, 25 insertions, 74 deletions
diff --git a/compiler/rustc_lint/src/noop_method_call.rs b/compiler/rustc_lint/src/noop_method_call.rs
index b4ee1f9157c..335c3c575e7 100644
--- a/compiler/rustc_lint/src/noop_method_call.rs
+++ b/compiler/rustc_lint/src/noop_method_call.rs
@@ -50,9 +50,7 @@ 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, sym::Deref, sym::Borrow]
-                        .iter()
-                        .any(|s| cx.tcx.is_diagnostic_item(*s, trait_id)) =>
+                    if [sym::Clone].iter().any(|s| cx.tcx.is_diagnostic_item(*s, trait_id)) =>
                 {
                     (trait_id, did)
                 }
@@ -73,13 +71,7 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
             _ => return,
         };
         // (Re)check that it implements the noop diagnostic.
-        for (s, peel_ref) in [
-            (sym::noop_method_borrow, true),
-            (sym::noop_method_clone, false),
-            (sym::noop_method_deref, true),
-        ]
-        .iter()
-        {
+        for (s, peel_ref) in [(sym::noop_method_clone, false)].iter() {
             if cx.tcx.is_diagnostic_item(*s, i.def_id()) {
                 let method = &call.ident.name;
                 let receiver = &elements[0];
diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs
index 61f9a080a52..f43b180e063 100644
--- a/compiler/rustc_span/src/symbol.rs
+++ b/compiler/rustc_span/src/symbol.rs
@@ -142,7 +142,6 @@ symbols! {
         Decodable,
         Decoder,
         Default,
-        Deref,
         Encodable,
         Encoder,
         Eq,
@@ -791,9 +790,7 @@ symbols! {
         none_error,
         nontemporal_store,
         nontrapping_dash_fptoint: "nontrapping-fptoint",
-        noop_method_borrow,
         noop_method_clone,
-        noop_method_deref,
         noreturn,
         nostack,
         not,
diff --git a/library/core/src/borrow.rs b/library/core/src/borrow.rs
index a0cdf681f67..c9040cd0a16 100644
--- a/library/core/src/borrow.rs
+++ b/library/core/src/borrow.rs
@@ -153,7 +153,6 @@
 /// [`HashMap<K, V>`]: ../../std/collections/struct.HashMap.html
 /// [`String`]: ../../std/string/struct.String.html
 #[stable(feature = "rust1", since = "1.0.0")]
-#[rustc_diagnostic_item = "Borrow"]
 pub trait Borrow<Borrowed: ?Sized> {
     /// Immutably borrows from an owned value.
     ///
@@ -220,7 +219,6 @@ impl<T: ?Sized> BorrowMut<T> for T {
 
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T: ?Sized> Borrow<T> for &T {
-    #[rustc_diagnostic_item = "noop_method_borrow"]
     fn borrow(&self) -> &T {
         &**self
     }
diff --git a/library/core/src/ops/deref.rs b/library/core/src/ops/deref.rs
index 10e3ce67448..2419771eae2 100644
--- a/library/core/src/ops/deref.rs
+++ b/library/core/src/ops/deref.rs
@@ -60,7 +60,6 @@
 #[doc(alias = "*")]
 #[doc(alias = "&*")]
 #[stable(feature = "rust1", since = "1.0.0")]
-#[rustc_diagnostic_item = "Deref"]
 pub trait Deref {
     /// The resulting type after dereferencing.
     #[stable(feature = "rust1", since = "1.0.0")]
@@ -79,7 +78,6 @@ pub trait Deref {
 impl<T: ?Sized> Deref for &T {
     type Target = T;
 
-    #[rustc_diagnostic_item = "noop_method_deref"]
     fn deref(&self) -> &T {
         *self
     }
diff --git a/src/test/ui/lint/noop-method-call.rs b/src/test/ui/lint/noop-method-call.rs
index 70363a191e9..8e4b5bf4d12 100644
--- a/src/test/ui/lint/noop-method-call.rs
+++ b/src/test/ui/lint/noop-method-call.rs
@@ -2,51 +2,33 @@
 
 #![allow(unused)]
 
-use std::borrow::Borrow;
-use std::ops::Deref;
-
-struct Foo<T>(T);
+struct NonCloneType<T>(T);
 
 #[derive(Clone)]
-struct Bar<T>(T);
-
-struct DerefExample<T>(T);
-
-impl<T> Deref for DerefExample<T> {
-    type Target = T;
-    fn deref(&self) -> &Self::Target {
-        &self.0
-    }
-}
+struct CloneType<T>(T);
 
 fn main() {
-    let foo = &Foo(1u32);
-    let foo_clone: &Foo<u32> = foo.clone();
+    let non_clone_type_ref = &NonCloneType(1u32);
+    let non_clone_type_ref_clone: &NonCloneType<u32> = non_clone_type_ref.clone();
     //~^ WARNING call to `.clone()` on a reference in this situation does nothing
 
-    let bar = &Bar(1u32);
-    let bar_clone: Bar<u32> = bar.clone();
-
-    let deref = &&DerefExample(12u32);
-    let derefed: &DerefExample<u32> = deref.deref();
-    //~^ WARNING call to `.deref()` on a reference in this situation does nothing
-
-    let deref = &DerefExample(12u32);
-    let derefed: &u32 = deref.deref();
+    let clone_type_ref = &CloneType(1u32);
+    let clone_type_ref_clone: CloneType<u32> = clone_type_ref.clone();
 
-    let a = &&Foo(1u32);
-    let borrowed: &Foo<u32> = a.borrow();
-    //~^ WARNING call to `.borrow()` on a reference in this situation does nothing
+    // Calling clone on a double reference doesn't warn since the method call itself
+    // peels the outer reference off
+    let clone_type_ref = &&CloneType(1u32);
+    let clone_type_ref_clone: &CloneType<u32> = clone_type_ref.clone();
 
     let xs = ["a", "b", "c"];
     let _v: Vec<&str> = xs.iter().map(|x| x.clone()).collect(); // ok, but could use `*x` instead
 }
 
-fn generic<T>(foo: &Foo<T>) {
-    foo.clone();
+fn generic<T>(non_clone_type: &NonCloneType<T>) {
+    non_clone_type.clone();
 }
 
-fn non_generic(foo: &Foo<u32>) {
-    foo.clone();
+fn non_generic(non_clone_type: &NonCloneType<u32>) {
+    non_clone_type.clone();
     //~^ WARNING call to `.clone()` on a reference in this situation does nothing
 }
diff --git a/src/test/ui/lint/noop-method-call.stderr b/src/test/ui/lint/noop-method-call.stderr
index 316c47975e6..85a67f53840 100644
--- a/src/test/ui/lint/noop-method-call.stderr
+++ b/src/test/ui/lint/noop-method-call.stderr
@@ -1,35 +1,19 @@
 warning: call to `.clone()` on a reference in this situation does nothing
-  --> $DIR/noop-method-call.rs:24:35
+  --> $DIR/noop-method-call.rs:12:74
    |
-LL |     let foo_clone: &Foo<u32> = foo.clone();
-   |                                   ^^^^^^^^ unnecessary method call
+LL |     let non_clone_type_ref_clone: &NonCloneType<u32> = non_clone_type_ref.clone();
+   |                                                                          ^^^^^^^^ unnecessary method call
    |
    = note: `#[warn(noop_method_call)]` on by default
-   = note: the type `&Foo<u32>` which `clone` is being called on is the same as the type returned from `clone`, so the method call does not do anything and can be removed
-
-warning: call to `.deref()` on a reference in this situation does nothing
-  --> $DIR/noop-method-call.rs:31:44
-   |
-LL |     let derefed: &DerefExample<u32> = deref.deref();
-   |                                            ^^^^^^^^ unnecessary method call
-   |
-   = note: the type `&DerefExample<u32>` which `deref` is being called on is the same as the type returned from `deref`, so the method call does not do anything and can be removed
-
-warning: call to `.borrow()` on a reference in this situation does nothing
-  --> $DIR/noop-method-call.rs:38:32
-   |
-LL |     let borrowed: &Foo<u32> = a.borrow();
-   |                                ^^^^^^^^^ unnecessary method call
-   |
-   = note: the type `&Foo<u32>` which `borrow` is being called on is the same as the type returned from `borrow`, so the method call does not do anything and can be removed
+   = note: the type `&NonCloneType<u32>` which `clone` is being called on is the same as the type returned from `clone`, so the method call does not do anything and can be removed
 
 warning: call to `.clone()` on a reference in this situation does nothing
-  --> $DIR/noop-method-call.rs:50:8
+  --> $DIR/noop-method-call.rs:32:19
    |
-LL |     foo.clone();
-   |        ^^^^^^^^ unnecessary method call
+LL |     non_clone_type.clone();
+   |                   ^^^^^^^^ unnecessary method call
    |
-   = note: the type `&Foo<u32>` which `clone` is being called on is the same as the type returned from `clone`, so the method call does not do anything and can be removed
+   = note: the type `&NonCloneType<u32>` which `clone` is being called on is the same as the type returned from `clone`, so the method call does not do anything and can be removed
 
-warning: 4 warnings emitted
+warning: 2 warnings emitted