diff --git a/compiler/rustc_lint/src/noop_method_call.rs b/compiler/rustc_lint/src/noop_method_call.rs index b4ee1f9157c82..335c3c575e766 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 61f9a080a5217..f43b180e06321 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 a0cdf681f67e1..c9040cd0a1670 100644 --- a/library/core/src/borrow.rs +++ b/library/core/src/borrow.rs @@ -153,7 +153,6 @@ /// [`HashMap`]: ../../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 { /// Immutably borrows from an owned value. /// @@ -220,7 +219,6 @@ impl BorrowMut for T { #[stable(feature = "rust1", since = "1.0.0")] impl Borrow 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 10e3ce67448c8..2419771eae212 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 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 70363a191e9e9..8e4b5bf4d12c7 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); +struct NonCloneType(T); #[derive(Clone)] -struct Bar(T); - -struct DerefExample(T); - -impl Deref for DerefExample { - type Target = T; - fn deref(&self) -> &Self::Target { - &self.0 - } -} +struct CloneType(T); fn main() { - let foo = &Foo(1u32); - let foo_clone: &Foo = foo.clone(); + let non_clone_type_ref = &NonCloneType(1u32); + let non_clone_type_ref_clone: &NonCloneType = 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 = bar.clone(); - - let deref = &&DerefExample(12u32); - let derefed: &DerefExample = 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 = clone_type_ref.clone(); - let a = &&Foo(1u32); - let borrowed: &Foo = 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 = 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(foo: &Foo) { - foo.clone(); +fn generic(non_clone_type: &NonCloneType) { + non_clone_type.clone(); } -fn non_generic(foo: &Foo) { - foo.clone(); +fn non_generic(non_clone_type: &NonCloneType) { + 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 316c47975e6de..85a67f538407c 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 = foo.clone(); - | ^^^^^^^^ unnecessary method call +LL | let non_clone_type_ref_clone: &NonCloneType = non_clone_type_ref.clone(); + | ^^^^^^^^ unnecessary method call | = note: `#[warn(noop_method_call)]` on by default - = note: the type `&Foo` 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 = deref.deref(); - | ^^^^^^^^ unnecessary method call - | - = note: the type `&DerefExample` 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 = a.borrow(); - | ^^^^^^^^^ unnecessary method call - | - = note: the type `&Foo` 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` 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` 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` 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