Skip to content

Commit

Permalink
Remove lint pass on borrow and deref
Browse files Browse the repository at this point in the history
  • Loading branch information
rylev committed Mar 3, 2021
1 parent 6bf6652 commit da3995f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 74 deletions.
12 changes: 2 additions & 10 deletions compiler/rustc_lint/src/noop_method_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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];
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ symbols! {
Decodable,
Decoder,
Default,
Deref,
Encodable,
Encoder,
Eq,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions library/core/src/borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 0 additions & 2 deletions library/core/src/ops/deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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
}
Expand Down
46 changes: 14 additions & 32 deletions src/test/ui/lint/noop-method-call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
34 changes: 9 additions & 25 deletions src/test/ui/lint/noop-method-call.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit da3995f

Please sign in to comment.