Skip to content

Commit 58e3a4a

Browse files
committed
Add tests and support two more noop methods
1 parent 988e2f9 commit 58e3a4a

File tree

7 files changed

+89
-5
lines changed

7 files changed

+89
-5
lines changed

compiler/rustc_lint/src/noop_method_call.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,19 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
5454
// Resolve the trait method instance
5555
if let Ok(Some(i)) = ty::Instance::resolve(cx.tcx, param_env, did, substs) {
5656
// Check that it implements the noop diagnostic
57-
if cx.tcx.is_diagnostic_item(sym::ref_clone_method, i.def_id()) {
57+
tracing::debug!("Resolves to: {:?}", i.def_id());
58+
if [
59+
sym::noop_method_borrow,
60+
sym::noop_method_clone,
61+
sym::noop_method_deref,
62+
]
63+
.iter()
64+
.any(|s| cx.tcx.is_diagnostic_item(*s, i.def_id()))
65+
{
5866
let span = expr.span;
5967

6068
cx.struct_span_lint(NOOP_METHOD_CALL, span, |lint| {
61-
let message = "Call to noop method";
69+
let message = "call to noop method";
6270
lint.build(&message).emit()
6371
});
6472
}

compiler/rustc_span/src/symbol.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,9 @@ symbols! {
765765
none_error,
766766
nontemporal_store,
767767
nontrapping_dash_fptoint: "nontrapping-fptoint",
768+
noop_method_borrow,
769+
noop_method_clone,
770+
noop_method_deref,
768771
noreturn,
769772
nostack,
770773
not,
@@ -887,7 +890,6 @@ symbols! {
887890
receiver,
888891
recursion_limit,
889892
reexport_test_harness_main,
890-
ref_clone_method,
891893
reference,
892894
reflect,
893895
reg,

library/core/src/borrow.rs

+1
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ impl<T: ?Sized> BorrowMut<T> for T {
219219

220220
#[stable(feature = "rust1", since = "1.0.0")]
221221
impl<T: ?Sized> Borrow<T> for &T {
222+
#[rustc_diagnostic_item = "noop_method_borrow"]
222223
fn borrow(&self) -> &T {
223224
&**self
224225
}

library/core/src/clone.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@
104104
/// [impls]: #implementors
105105
#[stable(feature = "rust1", since = "1.0.0")]
106106
#[lang = "clone"]
107-
#[rustc_diagnostic_item = "Clone"]
108107
pub trait Clone: Sized {
109108
/// Returns a copy of the value.
110109
///
@@ -222,7 +221,7 @@ mod impls {
222221
#[stable(feature = "rust1", since = "1.0.0")]
223222
impl<T: ?Sized> Clone for &T {
224223
#[inline]
225-
#[rustc_diagnostic_item = "ref_clone_method"]
224+
#[rustc_diagnostic_item = "noop_method_clone"]
226225
fn clone(&self) -> Self {
227226
*self
228227
}

library/core/src/ops/deref.rs

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub trait Deref {
7777
impl<T: ?Sized> Deref for &T {
7878
type Target = T;
7979

80+
#[rustc_diagnostic_item = "noop_method_deref"]
8081
fn deref(&self) -> &T {
8182
*self
8283
}

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

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// check-pass
2+
3+
#![allow(unused)]
4+
5+
use std::borrow::Borrow;
6+
use std::ops::Deref;
7+
8+
struct Foo<T>(T);
9+
10+
#[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+
}
21+
22+
fn main() {
23+
let foo = &Foo(1u32);
24+
let foo_clone: &Foo<u32> = foo.clone(); //~ WARNING call to noop method
25+
26+
let bar = &Bar(1u32);
27+
let bar_clone: Bar<u32> = bar.clone();
28+
29+
let deref = &&DerefExample(12u32);
30+
let derefed: &DerefExample<u32> = deref.deref(); //~ WARNING call to noop method
31+
32+
let deref = &DerefExample(12u32);
33+
let derefed: &u32 = deref.deref();
34+
35+
let a = &&Foo(1u32);
36+
let borrowed: &Foo<u32> = a.borrow(); //~ WARNING call to noop method
37+
}
38+
39+
fn generic<T>(foo: &Foo<T>) {
40+
foo.clone();
41+
}
42+
43+
fn non_generic(foo: &Foo<u32>) {
44+
foo.clone(); //~ WARNING call to noop method
45+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
warning: call to noop method
2+
--> $DIR/noop-method-call.rs:24:32
3+
|
4+
LL | let foo_clone: &Foo<u32> = foo.clone();
5+
| ^^^^^^^^^^^
6+
|
7+
= note: `#[warn(noop_method_call)]` on by default
8+
9+
warning: call to noop method
10+
--> $DIR/noop-method-call.rs:30:39
11+
|
12+
LL | let derefed: &DerefExample<u32> = deref.deref();
13+
| ^^^^^^^^^^^^^
14+
15+
warning: call to noop method
16+
--> $DIR/noop-method-call.rs:36:31
17+
|
18+
LL | let borrowed: &Foo<u32> = a.borrow();
19+
| ^^^^^^^^^^
20+
21+
warning: call to noop method
22+
--> $DIR/noop-method-call.rs:44:5
23+
|
24+
LL | foo.clone();
25+
| ^^^^^^^^^^^
26+
27+
warning: 4 warnings emitted
28+

0 commit comments

Comments
 (0)