-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
289 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use crate::utils::span_lint_and_sugg; | ||
use if_chain::if_chain; | ||
use rustc_errors::Applicability; | ||
use rustc_hir as hir; | ||
use rustc_hir::ExprKind; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty::TyS; | ||
use rustc_span::symbol::Symbol; | ||
|
||
use super::IMPLICIT_CLONE; | ||
use clippy_utils::is_diagnostic_assoc_item; | ||
|
||
pub fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, trait_diagnostic: Symbol) { | ||
if_chain! { | ||
if let ExprKind::MethodCall(method_path, _, [arg], _) = &expr.kind; | ||
let return_type = cx.typeck_results().expr_ty(&expr); | ||
let input_type = cx.typeck_results().expr_ty(arg).peel_refs(); | ||
if let Some(expr_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id); | ||
if let Some(ty_name) = input_type.ty_adt_def().map(|adt_def| cx.tcx.item_name(adt_def.did)); | ||
if TyS::same_type(return_type, input_type); | ||
if is_diagnostic_assoc_item(cx, expr_def_id, trait_diagnostic); | ||
then { | ||
span_lint_and_sugg( | ||
cx,IMPLICIT_CLONE,method_path.ident.span, | ||
&format!("implicitly cloning a `{}` by calling `{}` on its dereferenced type", ty_name, method_path.ident.name), | ||
"consider using", | ||
"clone".to_string(), | ||
Applicability::MachineApplicable | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#[allow(clippy::unnecessary_operation)] | ||
#[allow(clippy::implicit_clone)] | ||
|
||
fn main() { | ||
let x = &Baz; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#![warn(clippy::implicit_clone)] | ||
#![allow(clippy::redundant_clone)] | ||
use std::borrow::Borrow; | ||
use std::ffi::{OsStr, OsString}; | ||
use std::path::PathBuf; | ||
|
||
fn return_owned_from_slice(slice: &[u32]) -> Vec<u32> { | ||
slice.to_owned() | ||
} | ||
|
||
pub fn own_same<T>(v: T) -> T | ||
where | ||
T: ToOwned<Owned = T>, | ||
{ | ||
v.to_owned() | ||
} | ||
|
||
pub fn own_same_from_ref<T>(v: &T) -> T | ||
where | ||
T: ToOwned<Owned = T>, | ||
{ | ||
v.to_owned() | ||
} | ||
|
||
pub fn own_different<T, U>(v: T) -> U | ||
where | ||
T: ToOwned<Owned = U>, | ||
{ | ||
v.to_owned() | ||
} | ||
|
||
#[derive(Copy, Clone)] | ||
struct Kitten {} | ||
impl Kitten { | ||
// badly named method | ||
fn to_vec(self) -> Kitten { | ||
Kitten {} | ||
} | ||
} | ||
impl Borrow<BorrowedKitten> for Kitten { | ||
fn borrow(&self) -> &BorrowedKitten { | ||
static VALUE: BorrowedKitten = BorrowedKitten {}; | ||
&VALUE | ||
} | ||
} | ||
|
||
struct BorrowedKitten {} | ||
impl ToOwned for BorrowedKitten { | ||
type Owned = Kitten; | ||
fn to_owned(&self) -> Kitten { | ||
Kitten {} | ||
} | ||
} | ||
|
||
mod weird { | ||
#[allow(clippy::ptr_arg)] | ||
pub fn to_vec(v: &Vec<u32>) -> Vec<u32> { | ||
v.clone() | ||
} | ||
} | ||
|
||
fn main() { | ||
let vec = vec![5]; | ||
let _ = return_owned_from_slice(&vec); | ||
let _ = vec.to_owned(); | ||
let _ = vec.to_vec(); | ||
|
||
let vec_ref = &vec; | ||
let _ = return_owned_from_slice(&vec_ref); | ||
let _ = vec_ref.to_owned(); | ||
let _ = vec_ref.to_vec(); | ||
|
||
// we expect no lint for this | ||
let _ = weird::to_vec(&vec); | ||
|
||
// we expect no lints for this | ||
let slice: &[u32] = &[1, 2, 3, 4, 5]; | ||
let _ = return_owned_from_slice(slice); | ||
let _ = slice.to_owned(); | ||
let _ = slice.to_vec(); | ||
|
||
let str = "hello world".to_string(); | ||
let _ = str.to_owned(); | ||
|
||
// testing w/ an arbitrary type | ||
let kitten = Kitten {}; | ||
let _ = kitten.to_owned(); | ||
let _ = own_same_from_ref(&kitten); | ||
// this shouln't lint | ||
let _ = kitten.to_vec(); | ||
|
||
// we expect no lints for this | ||
let borrowed = BorrowedKitten {}; | ||
let _ = borrowed.to_owned(); | ||
|
||
let pathbuf = PathBuf::new(); | ||
let _ = pathbuf.to_owned(); | ||
let _ = pathbuf.to_path_buf(); | ||
|
||
let os_string = OsString::from("foo"); | ||
let _ = os_string.to_owned(); | ||
let _ = os_string.to_os_string(); | ||
|
||
// we expect no lints for this | ||
let os_str = OsStr::new("foo"); | ||
let _ = os_str.to_owned(); | ||
let _ = os_str.to_os_string(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
error: implicitly cloning a `Vec` by calling `to_owned` on its dereferenced type | ||
--> $DIR/implicit_clone.rs:65:17 | ||
| | ||
LL | let _ = vec.to_owned(); | ||
| ^^^^^^^^ help: consider using: `clone` | ||
| | ||
= note: `-D clippy::implicit-clone` implied by `-D warnings` | ||
|
||
error: implicitly cloning a `Vec` by calling `to_vec` on its dereferenced type | ||
--> $DIR/implicit_clone.rs:66:17 | ||
| | ||
LL | let _ = vec.to_vec(); | ||
| ^^^^^^ help: consider using: `clone` | ||
|
||
error: implicitly cloning a `Vec` by calling `to_owned` on its dereferenced type | ||
--> $DIR/implicit_clone.rs:70:21 | ||
| | ||
LL | let _ = vec_ref.to_owned(); | ||
| ^^^^^^^^ help: consider using: `clone` | ||
|
||
error: implicitly cloning a `Vec` by calling `to_vec` on its dereferenced type | ||
--> $DIR/implicit_clone.rs:71:21 | ||
| | ||
LL | let _ = vec_ref.to_vec(); | ||
| ^^^^^^ help: consider using: `clone` | ||
|
||
error: implicitly cloning a `String` by calling `to_owned` on its dereferenced type | ||
--> $DIR/implicit_clone.rs:83:17 | ||
| | ||
LL | let _ = str.to_owned(); | ||
| ^^^^^^^^ help: consider using: `clone` | ||
|
||
error: implicitly cloning a `Kitten` by calling `to_owned` on its dereferenced type | ||
--> $DIR/implicit_clone.rs:87:20 | ||
| | ||
LL | let _ = kitten.to_owned(); | ||
| ^^^^^^^^ help: consider using: `clone` | ||
|
||
error: implicitly cloning a `PathBuf` by calling `to_owned` on its dereferenced type | ||
--> $DIR/implicit_clone.rs:97:21 | ||
| | ||
LL | let _ = pathbuf.to_owned(); | ||
| ^^^^^^^^ help: consider using: `clone` | ||
|
||
error: implicitly cloning a `PathBuf` by calling `to_path_buf` on its dereferenced type | ||
--> $DIR/implicit_clone.rs:98:21 | ||
| | ||
LL | let _ = pathbuf.to_path_buf(); | ||
| ^^^^^^^^^^^ help: consider using: `clone` | ||
|
||
error: implicitly cloning a `OsString` by calling `to_owned` on its dereferenced type | ||
--> $DIR/implicit_clone.rs:101:23 | ||
| | ||
LL | let _ = os_string.to_owned(); | ||
| ^^^^^^^^ help: consider using: `clone` | ||
|
||
error: implicitly cloning a `OsString` by calling `to_os_string` on its dereferenced type | ||
--> $DIR/implicit_clone.rs:102:23 | ||
| | ||
LL | let _ = os_string.to_os_string(); | ||
| ^^^^^^^^^^^^ help: consider using: `clone` | ||
|
||
error: aborting due to 10 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.