Skip to content

Commit a2cc8c5

Browse files
committed
cleanup requested changes
1 parent a1b4cca commit a2cc8c5

File tree

5 files changed

+40
-38
lines changed

5 files changed

+40
-38
lines changed

clippy_lints/src/methods/implicit_clone.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,24 @@ use rustc_middle::ty::TyS;
88
use rustc_span::symbol::Symbol;
99

1010
use super::IMPLICIT_CLONE;
11-
use clippy_utils::{is_expr_container_diagnostic_item, is_type_diagnostic_item};
11+
use clippy_utils::is_diagnostic_assoc_item;
1212

13-
pub fn check(
14-
cx: &LateContext<'_>,
15-
expr: &hir::Expr<'_>,
16-
trait_diagnostic: Symbol,
17-
expected_type_diagnostic: Option<Symbol>,
18-
) {
13+
pub fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, trait_diagnostic: Symbol) {
1914
if_chain! {
2015
if let ExprKind::MethodCall(method_path, _, [arg], _) = &expr.kind;
2116
let return_type = cx.typeck_results().expr_ty(&expr);
2217
let input_type = cx.typeck_results().expr_ty(arg).peel_refs();
18+
if let Some(expr_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
2319
if let Some(ty_name) = input_type.ty_adt_def().map(|adt_def| cx.tcx.item_name(adt_def.did));
2420
if TyS::same_type(return_type, input_type);
25-
if is_expr_container_diagnostic_item(cx, expr, trait_diagnostic) &&
26-
expected_type_diagnostic.map_or(true, |diagnostic| is_type_diagnostic_item(cx,return_type,diagnostic));
21+
if is_diagnostic_assoc_item(cx, expr_def_id, trait_diagnostic);
2722
then {
2823
span_lint_and_sugg(
2924
cx,IMPLICIT_CLONE,method_path.ident.span,
30-
&format!("`{}` called on a `{}`", method_path.ident.name, ty_name),
25+
&format!("implicitly cloning a `{}` by calling `{}` on its dereferenced type", ty_name, method_path.ident.name),
3126
"consider using",
3227
"clone".to_string(),
33-
Applicability::MaybeIncorrect
28+
Applicability::MachineApplicable
3429
);
3530
}
3631
}

clippy_lints/src/methods/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,7 +1537,7 @@ declare_clippy_lint! {
15371537
/// ```
15381538
pub IMPLICIT_CLONE,
15391539
style,
1540-
"calling clone implicitly"
1540+
"implicitly cloning a value by invoking a function on its dereferenced type"
15411541
}
15421542

15431543
pub struct Methods {
@@ -1698,10 +1698,10 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
16981698
["ok_or_else", ..] => unnecessary_lazy_eval::lint(cx, expr, arg_lists[0], "ok_or"),
16991699
["collect", "map"] => lint_map_collect(cx, expr, arg_lists[1], arg_lists[0]),
17001700
["for_each", "inspect"] => inspect_for_each::lint(cx, expr, method_spans[1]),
1701-
["to_owned", ..] => implicit_clone::check(cx, expr, sym::ToOwned, None),
1702-
["to_os_string", ..] => implicit_clone::check(cx, expr, sym::OsStr, Some(sym::OsString)),
1703-
["to_path_buf", ..] => implicit_clone::check(cx, expr, sym::Path, Some(sym::PathBuf)),
1704-
["to_vec", ..] => implicit_clone::check(cx, expr, sym::slice, Some(sym::vec_type)),
1701+
["to_owned", ..] => implicit_clone::check(cx, expr, sym::ToOwned),
1702+
["to_os_string", ..] => implicit_clone::check(cx, expr, sym::OsStr),
1703+
["to_path_buf", ..] => implicit_clone::check(cx, expr, sym::Path),
1704+
["to_vec", ..] => implicit_clone::check(cx, expr, sym::slice),
17051705
_ => {},
17061706
}
17071707

clippy_utils/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,7 @@ pub fn match_trait_method(cx: &LateContext<'_>, expr: &Expr<'_>, path: &[&str])
239239

240240
/// Checks if the method call given in `expr` belongs to a trait or other container with a given
241241
/// diagnostic item
242-
pub fn is_expr_container_diagnostic_item(cx: &LateContext<'_>, expr: &Expr<'_>, diag_item: Symbol) -> bool {
243-
let def_id = cx.typeck_results().type_dependent_def_id(expr.hir_id).unwrap();
242+
pub fn is_diagnostic_assoc_item(cx: &LateContext<'_>, def_id: DefId, diag_item: Symbol) -> bool {
244243
cx.tcx
245244
.opt_associated_item(def_id)
246245
.and_then(|associated_item| match associated_item.container {

tests/ui/implicit_clone.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ where
3131

3232
#[derive(Copy, Clone)]
3333
struct Kitten {}
34+
impl Kitten {
35+
// badly named method
36+
fn to_vec(self) -> Kitten {
37+
Kitten {}
38+
}
39+
}
3440
impl Borrow<BorrowedKitten> for Kitten {
3541
fn borrow(&self) -> &BorrowedKitten {
3642
static VALUE: BorrowedKitten = BorrowedKitten {};
@@ -80,6 +86,8 @@ fn main() {
8086
let kitten = Kitten {};
8187
let _ = kitten.to_owned();
8288
let _ = own_same_from_ref(&kitten);
89+
// this shouln't lint
90+
let _ = kitten.to_vec();
8391

8492
// we expect no lints for this
8593
let borrowed = BorrowedKitten {};

tests/ui/implicit_clone.stderr

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,61 @@
1-
error: `to_owned` called on a `Vec`
2-
--> $DIR/implicit_clone.rs:59:17
1+
error: implicitly cloning a `Vec` by calling `to_owned` on its dereferenced type
2+
--> $DIR/implicit_clone.rs:65:17
33
|
44
LL | let _ = vec.to_owned();
55
| ^^^^^^^^ help: consider using: `clone`
66
|
77
= note: `-D clippy::implicit-clone` implied by `-D warnings`
88

9-
error: `to_vec` called on a `Vec`
10-
--> $DIR/implicit_clone.rs:60:17
9+
error: implicitly cloning a `Vec` by calling `to_vec` on its dereferenced type
10+
--> $DIR/implicit_clone.rs:66:17
1111
|
1212
LL | let _ = vec.to_vec();
1313
| ^^^^^^ help: consider using: `clone`
1414

15-
error: `to_owned` called on a `Vec`
16-
--> $DIR/implicit_clone.rs:64:21
15+
error: implicitly cloning a `Vec` by calling `to_owned` on its dereferenced type
16+
--> $DIR/implicit_clone.rs:70:21
1717
|
1818
LL | let _ = vec_ref.to_owned();
1919
| ^^^^^^^^ help: consider using: `clone`
2020

21-
error: `to_vec` called on a `Vec`
22-
--> $DIR/implicit_clone.rs:65:21
21+
error: implicitly cloning a `Vec` by calling `to_vec` on its dereferenced type
22+
--> $DIR/implicit_clone.rs:71:21
2323
|
2424
LL | let _ = vec_ref.to_vec();
2525
| ^^^^^^ help: consider using: `clone`
2626

27-
error: `to_owned` called on a `String`
28-
--> $DIR/implicit_clone.rs:77:17
27+
error: implicitly cloning a `String` by calling `to_owned` on its dereferenced type
28+
--> $DIR/implicit_clone.rs:83:17
2929
|
3030
LL | let _ = str.to_owned();
3131
| ^^^^^^^^ help: consider using: `clone`
3232

33-
error: `to_owned` called on a `Kitten`
34-
--> $DIR/implicit_clone.rs:81:20
33+
error: implicitly cloning a `Kitten` by calling `to_owned` on its dereferenced type
34+
--> $DIR/implicit_clone.rs:87:20
3535
|
3636
LL | let _ = kitten.to_owned();
3737
| ^^^^^^^^ help: consider using: `clone`
3838

39-
error: `to_owned` called on a `PathBuf`
40-
--> $DIR/implicit_clone.rs:89:21
39+
error: implicitly cloning a `PathBuf` by calling `to_owned` on its dereferenced type
40+
--> $DIR/implicit_clone.rs:97:21
4141
|
4242
LL | let _ = pathbuf.to_owned();
4343
| ^^^^^^^^ help: consider using: `clone`
4444

45-
error: `to_path_buf` called on a `PathBuf`
46-
--> $DIR/implicit_clone.rs:90:21
45+
error: implicitly cloning a `PathBuf` by calling `to_path_buf` on its dereferenced type
46+
--> $DIR/implicit_clone.rs:98:21
4747
|
4848
LL | let _ = pathbuf.to_path_buf();
4949
| ^^^^^^^^^^^ help: consider using: `clone`
5050

51-
error: `to_owned` called on a `OsString`
52-
--> $DIR/implicit_clone.rs:93:23
51+
error: implicitly cloning a `OsString` by calling `to_owned` on its dereferenced type
52+
--> $DIR/implicit_clone.rs:101:23
5353
|
5454
LL | let _ = os_string.to_owned();
5555
| ^^^^^^^^ help: consider using: `clone`
5656

57-
error: `to_os_string` called on a `OsString`
58-
--> $DIR/implicit_clone.rs:94:23
57+
error: implicitly cloning a `OsString` by calling `to_os_string` on its dereferenced type
58+
--> $DIR/implicit_clone.rs:102:23
5959
|
6060
LL | let _ = os_string.to_os_string();
6161
| ^^^^^^^^^^^^ help: consider using: `clone`

0 commit comments

Comments
 (0)