@@ -8,6 +8,7 @@ mod chars_next_cmp;
8
8
mod chars_next_cmp_with_unwrap;
9
9
mod clone_on_copy;
10
10
mod clone_on_ref_ptr;
11
+ mod cloned_instead_of_copied;
11
12
mod expect_fun_call;
12
13
mod expect_used;
13
14
mod filetype_is_file;
@@ -73,6 +74,29 @@ use rustc_span::symbol::SymbolStr;
73
74
use rustc_span:: { sym, Span } ;
74
75
use rustc_typeck:: hir_ty_to_ty;
75
76
77
+ declare_clippy_lint ! {
78
+ /// **What it does:** Checks for usages of `cloned()` on an `Iterator` or `Option` where
79
+ /// `copied()` could be used instead.
80
+ ///
81
+ /// **Why is this bad?** `copied()` is better because it guarantees that the type being cloned
82
+ /// implements `Copy`.
83
+ ///
84
+ /// **Known problems:** None.
85
+ ///
86
+ /// **Example:**
87
+ ///
88
+ /// ```rust
89
+ /// [1, 2, 3].iter().cloned();
90
+ /// ```
91
+ /// Use instead:
92
+ /// ```rust
93
+ /// [1, 2, 3].iter().copied();
94
+ /// ```
95
+ pub CLONED_INSTEAD_OF_COPIED ,
96
+ pedantic,
97
+ "used `cloned` where `copied` could be used instead"
98
+ }
99
+
76
100
declare_clippy_lint ! {
77
101
/// **What it does:** Checks for `.unwrap()` calls on `Option`s and on `Result`s.
78
102
///
@@ -1638,6 +1662,7 @@ impl_lint_pass!(Methods => [
1638
1662
CLONE_ON_COPY ,
1639
1663
CLONE_ON_REF_PTR ,
1640
1664
CLONE_DOUBLE_REF ,
1665
+ CLONED_INSTEAD_OF_COPIED ,
1641
1666
INEFFICIENT_TO_STRING ,
1642
1667
NEW_RET_NO_SELF ,
1643
1668
SINGLE_CHAR_PATTERN ,
@@ -1909,6 +1934,7 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
1909
1934
( "as_mut" , [ ] ) => useless_asref:: check ( cx, expr, "as_mut" , recv) ,
1910
1935
( "as_ref" , [ ] ) => useless_asref:: check ( cx, expr, "as_ref" , recv) ,
1911
1936
( "assume_init" , [ ] ) => uninit_assumed_init:: check ( cx, expr, recv) ,
1937
+ ( "cloned" , [ ] ) => cloned_instead_of_copied:: check ( cx, expr, recv, span) ,
1912
1938
( "collect" , [ ] ) => match method_call ! ( recv) {
1913
1939
Some ( ( "cloned" , [ recv2] , _) ) => iter_cloned_collect:: check ( cx, expr, recv2) ,
1914
1940
Some ( ( "map" , [ m_recv, m_arg] , _) ) => {
0 commit comments