Skip to content

Commit 4c1cb56

Browse files
authored
Rollup merge of rust-lang#80723 - rylev:noop-lint-pass, r=estebank
Implement NOOP_METHOD_CALL lint Implements the beginnings of rust-lang/lang-team#67 - a lint for detecting noop method calls (e.g, calling `<&T as Clone>::clone()` when `T: !Clone`). This PR does not fully realize the vision and has a few limitations that need to be addressed either before merging or in subsequent PRs: * [ ] No UFCS support * [ ] The warning message is pretty plain * [ ] Doesn't work for `ToOwned` The implementation uses [`Instance::resolve`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/instance/struct.Instance.html#method.resolve) which is normally later in the compiler. It seems that there are some invariants that this function relies on that we try our best to respect. For instance, it expects substitutions to have happened, which haven't yet performed, but we check first for `needs_subst` to ensure we're dealing with a monomorphic type. Thank you to ```@davidtwco,``` ```@Aaron1011,``` and ```@wesleywiser``` for helping me at various points through out this PR ❤️.
2 parents dd502a3 + 6327553 commit 4c1cb56

File tree

6 files changed

+17
-10
lines changed

6 files changed

+17
-10
lines changed

alloc/src/collections/btree/map/tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,11 +1801,11 @@ fn test_occupied_entry_key() {
18011801
let key = "hello there";
18021802
let value = "value goes here";
18031803
assert!(a.is_empty());
1804-
a.insert(key.clone(), value.clone());
1804+
a.insert(key, value);
18051805
assert_eq!(a.len(), 1);
18061806
assert_eq!(a[key], value);
18071807

1808-
match a.entry(key.clone()) {
1808+
match a.entry(key) {
18091809
Vacant(_) => panic!(),
18101810
Occupied(e) => assert_eq!(key, *e.key()),
18111811
}
@@ -1821,11 +1821,11 @@ fn test_vacant_entry_key() {
18211821
let value = "value goes here";
18221822

18231823
assert!(a.is_empty());
1824-
match a.entry(key.clone()) {
1824+
match a.entry(key) {
18251825
Occupied(_) => panic!(),
18261826
Vacant(e) => {
18271827
assert_eq!(key, *e.key());
1828-
e.insert(value.clone());
1828+
e.insert(value);
18291829
}
18301830
}
18311831
assert_eq!(a.len(), 1);

core/src/borrow.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
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"]
156157
pub trait Borrow<Borrowed: ?Sized> {
157158
/// Immutably borrows from an owned value.
158159
///
@@ -205,6 +206,7 @@ pub trait BorrowMut<Borrowed: ?Sized>: Borrow<Borrowed> {
205206

206207
#[stable(feature = "rust1", since = "1.0.0")]
207208
impl<T: ?Sized> Borrow<T> for T {
209+
#[rustc_diagnostic_item = "noop_method_borrow"]
208210
fn borrow(&self) -> &T {
209211
self
210212
}

core/src/clone.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,14 @@
104104
/// [impls]: #implementors
105105
#[stable(feature = "rust1", since = "1.0.0")]
106106
#[lang = "clone"]
107+
#[rustc_diagnostic_item = "Clone"]
107108
pub trait Clone: Sized {
108109
/// Returns a copy of the value.
109110
///
110111
/// # Examples
111112
///
112113
/// ```
114+
/// # #![allow(noop_method_call)]
113115
/// let hello = "Hello"; // &str implements Clone
114116
///
115117
/// assert_eq!("Hello", hello.clone());
@@ -221,6 +223,7 @@ mod impls {
221223
#[stable(feature = "rust1", since = "1.0.0")]
222224
impl<T: ?Sized> Clone for &T {
223225
#[inline]
226+
#[rustc_diagnostic_item = "noop_method_clone"]
224227
fn clone(&self) -> Self {
225228
*self
226229
}

core/src/ops/deref.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#[doc(alias = "*")]
6161
#[doc(alias = "&*")]
6262
#[stable(feature = "rust1", since = "1.0.0")]
63+
#[rustc_diagnostic_item = "Deref"]
6364
pub trait Deref {
6465
/// The resulting type after dereferencing.
6566
#[stable(feature = "rust1", since = "1.0.0")]
@@ -78,6 +79,7 @@ pub trait Deref {
7879
impl<T: ?Sized> Deref for &T {
7980
type Target = T;
8081

82+
#[rustc_diagnostic_item = "noop_method_deref"]
8183
fn deref(&self) -> &T {
8284
*self
8385
}

core/tests/iter/adapters/intersperse.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn test_intersperse() {
99
assert_eq!(v, vec![1]);
1010

1111
let xs = ["a", "", "b", "c"];
12-
let v: Vec<&str> = xs.iter().map(|x| x.clone()).intersperse(", ").collect();
12+
let v: Vec<&str> = xs.iter().map(|x| *x).intersperse(", ").collect();
1313
let text: String = v.concat();
1414
assert_eq!(text, "a, , b, c".to_string());
1515

@@ -24,7 +24,7 @@ fn test_intersperse_size_hint() {
2424
assert_eq!(iter.size_hint(), (0, Some(0)));
2525

2626
let xs = ["a", "", "b", "c"];
27-
let mut iter = xs.iter().map(|x| x.clone()).intersperse(", ");
27+
let mut iter = xs.iter().map(|x| *x).intersperse(", ");
2828
assert_eq!(iter.size_hint(), (7, Some(7)));
2929

3030
assert_eq!(iter.next(), Some("a"));

std/src/collections/hash/map/tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -774,11 +774,11 @@ fn test_occupied_entry_key() {
774774
let key = "hello there";
775775
let value = "value goes here";
776776
assert!(a.is_empty());
777-
a.insert(key.clone(), value.clone());
777+
a.insert(key, value);
778778
assert_eq!(a.len(), 1);
779779
assert_eq!(a[key], value);
780780

781-
match a.entry(key.clone()) {
781+
match a.entry(key) {
782782
Vacant(_) => panic!(),
783783
Occupied(e) => assert_eq!(key, *e.key()),
784784
}
@@ -793,11 +793,11 @@ fn test_vacant_entry_key() {
793793
let value = "value goes here";
794794

795795
assert!(a.is_empty());
796-
match a.entry(key.clone()) {
796+
match a.entry(key) {
797797
Occupied(_) => panic!(),
798798
Vacant(e) => {
799799
assert_eq!(key, *e.key());
800-
e.insert(value.clone());
800+
e.insert(value);
801801
}
802802
}
803803
assert_eq!(a.len(), 1);

0 commit comments

Comments
 (0)