Remove lint pass on borrow and deref
This commit is contained in:
parent
6bf6652616
commit
da3995f0ec
6 changed files with 25 additions and 74 deletions
|
@ -50,9 +50,7 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
|
||||||
Some((DefKind::AssocFn, did)) => match cx.tcx.trait_of_item(did) {
|
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.
|
// Check that we're dealing with a trait method for one of the traits we care about.
|
||||||
Some(trait_id)
|
Some(trait_id)
|
||||||
if [sym::Clone, sym::Deref, sym::Borrow]
|
if [sym::Clone].iter().any(|s| cx.tcx.is_diagnostic_item(*s, trait_id)) =>
|
||||||
.iter()
|
|
||||||
.any(|s| cx.tcx.is_diagnostic_item(*s, trait_id)) =>
|
|
||||||
{
|
{
|
||||||
(trait_id, did)
|
(trait_id, did)
|
||||||
}
|
}
|
||||||
|
@ -73,13 +71,7 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
|
||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
// (Re)check that it implements the noop diagnostic.
|
// (Re)check that it implements the noop diagnostic.
|
||||||
for (s, peel_ref) in [
|
for (s, peel_ref) in [(sym::noop_method_clone, false)].iter() {
|
||||||
(sym::noop_method_borrow, true),
|
|
||||||
(sym::noop_method_clone, false),
|
|
||||||
(sym::noop_method_deref, true),
|
|
||||||
]
|
|
||||||
.iter()
|
|
||||||
{
|
|
||||||
if cx.tcx.is_diagnostic_item(*s, i.def_id()) {
|
if cx.tcx.is_diagnostic_item(*s, i.def_id()) {
|
||||||
let method = &call.ident.name;
|
let method = &call.ident.name;
|
||||||
let receiver = &elements[0];
|
let receiver = &elements[0];
|
||||||
|
|
|
@ -142,7 +142,6 @@ symbols! {
|
||||||
Decodable,
|
Decodable,
|
||||||
Decoder,
|
Decoder,
|
||||||
Default,
|
Default,
|
||||||
Deref,
|
|
||||||
Encodable,
|
Encodable,
|
||||||
Encoder,
|
Encoder,
|
||||||
Eq,
|
Eq,
|
||||||
|
@ -791,9 +790,7 @@ symbols! {
|
||||||
none_error,
|
none_error,
|
||||||
nontemporal_store,
|
nontemporal_store,
|
||||||
nontrapping_dash_fptoint: "nontrapping-fptoint",
|
nontrapping_dash_fptoint: "nontrapping-fptoint",
|
||||||
noop_method_borrow,
|
|
||||||
noop_method_clone,
|
noop_method_clone,
|
||||||
noop_method_deref,
|
|
||||||
noreturn,
|
noreturn,
|
||||||
nostack,
|
nostack,
|
||||||
not,
|
not,
|
||||||
|
|
|
@ -153,7 +153,6 @@
|
||||||
/// [`HashMap<K, V>`]: ../../std/collections/struct.HashMap.html
|
/// [`HashMap<K, V>`]: ../../std/collections/struct.HashMap.html
|
||||||
/// [`String`]: ../../std/string/struct.String.html
|
/// [`String`]: ../../std/string/struct.String.html
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
#[rustc_diagnostic_item = "Borrow"]
|
|
||||||
pub trait Borrow<Borrowed: ?Sized> {
|
pub trait Borrow<Borrowed: ?Sized> {
|
||||||
/// Immutably borrows from an owned value.
|
/// Immutably borrows from an owned value.
|
||||||
///
|
///
|
||||||
|
@ -220,7 +219,6 @@ impl<T: ?Sized> BorrowMut<T> for T {
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl<T: ?Sized> Borrow<T> for &T {
|
impl<T: ?Sized> Borrow<T> for &T {
|
||||||
#[rustc_diagnostic_item = "noop_method_borrow"]
|
|
||||||
fn borrow(&self) -> &T {
|
fn borrow(&self) -> &T {
|
||||||
&**self
|
&**self
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,7 +60,6 @@
|
||||||
#[doc(alias = "*")]
|
#[doc(alias = "*")]
|
||||||
#[doc(alias = "&*")]
|
#[doc(alias = "&*")]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
#[rustc_diagnostic_item = "Deref"]
|
|
||||||
pub trait Deref {
|
pub trait Deref {
|
||||||
/// The resulting type after dereferencing.
|
/// The resulting type after dereferencing.
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
@ -79,7 +78,6 @@ pub trait Deref {
|
||||||
impl<T: ?Sized> Deref for &T {
|
impl<T: ?Sized> Deref for &T {
|
||||||
type Target = T;
|
type Target = T;
|
||||||
|
|
||||||
#[rustc_diagnostic_item = "noop_method_deref"]
|
|
||||||
fn deref(&self) -> &T {
|
fn deref(&self) -> &T {
|
||||||
*self
|
*self
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,51 +2,33 @@
|
||||||
|
|
||||||
#![allow(unused)]
|
#![allow(unused)]
|
||||||
|
|
||||||
use std::borrow::Borrow;
|
struct NonCloneType<T>(T);
|
||||||
use std::ops::Deref;
|
|
||||||
|
|
||||||
struct Foo<T>(T);
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Bar<T>(T);
|
struct CloneType<T>(T);
|
||||||
|
|
||||||
struct DerefExample<T>(T);
|
|
||||||
|
|
||||||
impl<T> Deref for DerefExample<T> {
|
|
||||||
type Target = T;
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let foo = &Foo(1u32);
|
let non_clone_type_ref = &NonCloneType(1u32);
|
||||||
let foo_clone: &Foo<u32> = foo.clone();
|
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
|
//~^ WARNING call to `.clone()` on a reference in this situation does nothing
|
||||||
|
|
||||||
let bar = &Bar(1u32);
|
let clone_type_ref = &CloneType(1u32);
|
||||||
let bar_clone: Bar<u32> = bar.clone();
|
let clone_type_ref_clone: CloneType<u32> = clone_type_ref.clone();
|
||||||
|
|
||||||
let deref = &&DerefExample(12u32);
|
// Calling clone on a double reference doesn't warn since the method call itself
|
||||||
let derefed: &DerefExample<u32> = deref.deref();
|
// peels the outer reference off
|
||||||
//~^ WARNING call to `.deref()` on a reference in this situation does nothing
|
let clone_type_ref = &&CloneType(1u32);
|
||||||
|
let clone_type_ref_clone: &CloneType<u32> = clone_type_ref.clone();
|
||||||
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
|
|
||||||
|
|
||||||
let xs = ["a", "b", "c"];
|
let xs = ["a", "b", "c"];
|
||||||
let _v: Vec<&str> = xs.iter().map(|x| x.clone()).collect(); // ok, but could use `*x` instead
|
let _v: Vec<&str> = xs.iter().map(|x| x.clone()).collect(); // ok, but could use `*x` instead
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generic<T>(foo: &Foo<T>) {
|
fn generic<T>(non_clone_type: &NonCloneType<T>) {
|
||||||
foo.clone();
|
non_clone_type.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn non_generic(foo: &Foo<u32>) {
|
fn non_generic(non_clone_type: &NonCloneType<u32>) {
|
||||||
foo.clone();
|
non_clone_type.clone();
|
||||||
//~^ WARNING call to `.clone()` on a reference in this situation does nothing
|
//~^ 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
|
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();
|
LL | let non_clone_type_ref_clone: &NonCloneType<u32> = non_clone_type_ref.clone();
|
||||||
| ^^^^^^^^ unnecessary method call
|
| ^^^^^^^^ unnecessary method call
|
||||||
|
|
|
|
||||||
= note: `#[warn(noop_method_call)]` on by default
|
= 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
|
= 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 `.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
|
|
||||||
|
|
||||||
warning: call to `.clone()` on a reference in this situation does nothing
|
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();
|
LL | non_clone_type.clone();
|
||||||
| ^^^^^^^^ unnecessary method call
|
| ^^^^^^^^ 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…
Add table
Add a link
Reference in a new issue