Skip to content

Commit da3995f

Browse files
committed
Remove lint pass on borrow and deref
1 parent 6bf6652 commit da3995f

File tree

6 files changed

+25
-74
lines changed

6 files changed

+25
-74
lines changed

compiler/rustc_lint/src/noop_method_call.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
5050
Some((DefKind::AssocFn, did)) => match cx.tcx.trait_of_item(did) {
5151
// Check that we're dealing with a trait method for one of the traits we care about.
5252
Some(trait_id)
53-
if [sym::Clone, sym::Deref, sym::Borrow]
54-
.iter()
55-
.any(|s| cx.tcx.is_diagnostic_item(*s, trait_id)) =>
53+
if [sym::Clone].iter().any(|s| cx.tcx.is_diagnostic_item(*s, trait_id)) =>
5654
{
5755
(trait_id, did)
5856
}
@@ -73,13 +71,7 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
7371
_ => return,
7472
};
7573
// (Re)check that it implements the noop diagnostic.
76-
for (s, peel_ref) in [
77-
(sym::noop_method_borrow, true),
78-
(sym::noop_method_clone, false),
79-
(sym::noop_method_deref, true),
80-
]
81-
.iter()
82-
{
74+
for (s, peel_ref) in [(sym::noop_method_clone, false)].iter() {
8375
if cx.tcx.is_diagnostic_item(*s, i.def_id()) {
8476
let method = &call.ident.name;
8577
let receiver = &elements[0];

compiler/rustc_span/src/symbol.rs

-3
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ symbols! {
142142
Decodable,
143143
Decoder,
144144
Default,
145-
Deref,
146145
Encodable,
147146
Encoder,
148147
Eq,
@@ -791,9 +790,7 @@ symbols! {
791790
none_error,
792791
nontemporal_store,
793792
nontrapping_dash_fptoint: "nontrapping-fptoint",
794-
noop_method_borrow,
795793
noop_method_clone,
796-
noop_method_deref,
797794
noreturn,
798795
nostack,
799796
not,

library/core/src/borrow.rs

-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@
153153
/// [`HashMap<K, V>`]: ../../std/collections/struct.HashMap.html
154154
/// [`String`]: ../../std/string/struct.String.html
155155
#[stable(feature = "rust1", since = "1.0.0")]
156-
#[rustc_diagnostic_item = "Borrow"]
157156
pub trait Borrow<Borrowed: ?Sized> {
158157
/// Immutably borrows from an owned value.
159158
///
@@ -220,7 +219,6 @@ impl<T: ?Sized> BorrowMut<T> for T {
220219

221220
#[stable(feature = "rust1", since = "1.0.0")]
222221
impl<T: ?Sized> Borrow<T> for &T {
223-
#[rustc_diagnostic_item = "noop_method_borrow"]
224222
fn borrow(&self) -> &T {
225223
&**self
226224
}

library/core/src/ops/deref.rs

-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
#[doc(alias = "*")]
6161
#[doc(alias = "&*")]
6262
#[stable(feature = "rust1", since = "1.0.0")]
63-
#[rustc_diagnostic_item = "Deref"]
6463
pub trait Deref {
6564
/// The resulting type after dereferencing.
6665
#[stable(feature = "rust1", since = "1.0.0")]
@@ -79,7 +78,6 @@ pub trait Deref {
7978
impl<T: ?Sized> Deref for &T {
8079
type Target = T;
8180

82-
#[rustc_diagnostic_item = "noop_method_deref"]
8381
fn deref(&self) -> &T {
8482
*self
8583
}

src/test/ui/lint/noop-method-call.rs

+14-32
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,33 @@
22

33
#![allow(unused)]
44

5-
use std::borrow::Borrow;
6-
use std::ops::Deref;
7-
8-
struct Foo<T>(T);
5+
struct NonCloneType<T>(T);
96

107
#[derive(Clone)]
11-
struct Bar<T>(T);
12-
13-
struct DerefExample<T>(T);
14-
15-
impl<T> Deref for DerefExample<T> {
16-
type Target = T;
17-
fn deref(&self) -> &Self::Target {
18-
&self.0
19-
}
20-
}
8+
struct CloneType<T>(T);
219

2210
fn main() {
23-
let foo = &Foo(1u32);
24-
let foo_clone: &Foo<u32> = foo.clone();
11+
let non_clone_type_ref = &NonCloneType(1u32);
12+
let non_clone_type_ref_clone: &NonCloneType<u32> = non_clone_type_ref.clone();
2513
//~^ WARNING call to `.clone()` on a reference in this situation does nothing
2614

27-
let bar = &Bar(1u32);
28-
let bar_clone: Bar<u32> = bar.clone();
29-
30-
let deref = &&DerefExample(12u32);
31-
let derefed: &DerefExample<u32> = deref.deref();
32-
//~^ WARNING call to `.deref()` on a reference in this situation does nothing
33-
34-
let deref = &DerefExample(12u32);
35-
let derefed: &u32 = deref.deref();
15+
let clone_type_ref = &CloneType(1u32);
16+
let clone_type_ref_clone: CloneType<u32> = clone_type_ref.clone();
3617

37-
let a = &&Foo(1u32);
38-
let borrowed: &Foo<u32> = a.borrow();
39-
//~^ WARNING call to `.borrow()` on a reference in this situation does nothing
18+
// Calling clone on a double reference doesn't warn since the method call itself
19+
// peels the outer reference off
20+
let clone_type_ref = &&CloneType(1u32);
21+
let clone_type_ref_clone: &CloneType<u32> = clone_type_ref.clone();
4022

4123
let xs = ["a", "b", "c"];
4224
let _v: Vec<&str> = xs.iter().map(|x| x.clone()).collect(); // ok, but could use `*x` instead
4325
}
4426

45-
fn generic<T>(foo: &Foo<T>) {
46-
foo.clone();
27+
fn generic<T>(non_clone_type: &NonCloneType<T>) {
28+
non_clone_type.clone();
4729
}
4830

49-
fn non_generic(foo: &Foo<u32>) {
50-
foo.clone();
31+
fn non_generic(non_clone_type: &NonCloneType<u32>) {
32+
non_clone_type.clone();
5133
//~^ WARNING call to `.clone()` on a reference in this situation does nothing
5234
}
+9-25
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,19 @@
11
warning: call to `.clone()` on a reference in this situation does nothing
2-
--> $DIR/noop-method-call.rs:24:35
2+
--> $DIR/noop-method-call.rs:12:74
33
|
4-
LL | let foo_clone: &Foo<u32> = foo.clone();
5-
| ^^^^^^^^ unnecessary method call
4+
LL | let non_clone_type_ref_clone: &NonCloneType<u32> = non_clone_type_ref.clone();
5+
| ^^^^^^^^ unnecessary method call
66
|
77
= note: `#[warn(noop_method_call)]` on by default
8-
= 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
9-
10-
warning: call to `.deref()` on a reference in this situation does nothing
11-
--> $DIR/noop-method-call.rs:31:44
12-
|
13-
LL | let derefed: &DerefExample<u32> = deref.deref();
14-
| ^^^^^^^^ unnecessary method call
15-
|
16-
= 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
17-
18-
warning: call to `.borrow()` on a reference in this situation does nothing
19-
--> $DIR/noop-method-call.rs:38:32
20-
|
21-
LL | let borrowed: &Foo<u32> = a.borrow();
22-
| ^^^^^^^^^ unnecessary method call
23-
|
24-
= 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
8+
= 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
259

2610
warning: call to `.clone()` on a reference in this situation does nothing
27-
--> $DIR/noop-method-call.rs:50:8
11+
--> $DIR/noop-method-call.rs:32:19
2812
|
29-
LL | foo.clone();
30-
| ^^^^^^^^ unnecessary method call
13+
LL | non_clone_type.clone();
14+
| ^^^^^^^^ unnecessary method call
3115
|
32-
= 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
16+
= 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
3317

34-
warning: 4 warnings emitted
18+
warning: 2 warnings emitted
3519

0 commit comments

Comments
 (0)