mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-24 21:05:12 +00:00
Remove lint pass on borrow and deref
This commit is contained in:
parent
6bf6652616
commit
da3995f0ec
@ -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];
|
||||
|
@ -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,
|
||||
|
@ -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.
|
||||
///
|
||||
@ -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
|
||||
}
|
||||
|
@ -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<T: ?Sized> Deref for &T {
|
||||
type Target = T;
|
||||
|
||||
#[rustc_diagnostic_item = "noop_method_deref"]
|
||||
fn deref(&self) -> &T {
|
||||
*self
|
||||
}
|
||||
|
@ -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 clone_type_ref = &CloneType(1u32);
|
||||
let clone_type_ref_clone: CloneType<u32> = clone_type_ref.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 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
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user