@@ -8,6 +8,7 @@ mod chars_next_cmp;
88mod chars_next_cmp_with_unwrap;
99mod clone_on_copy;
1010mod clone_on_ref_ptr;
11+ mod cloned_instead_of_copied;
1112mod expect_fun_call;
1213mod expect_used;
1314mod filetype_is_file;
@@ -73,6 +74,29 @@ use rustc_span::symbol::SymbolStr;
7374use rustc_span:: { sym, Span } ;
7475use rustc_typeck:: hir_ty_to_ty;
7576
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+
76100declare_clippy_lint ! {
77101 /// **What it does:** Checks for `.unwrap()` calls on `Option`s and on `Result`s.
78102 ///
@@ -1638,6 +1662,7 @@ impl_lint_pass!(Methods => [
16381662 CLONE_ON_COPY ,
16391663 CLONE_ON_REF_PTR ,
16401664 CLONE_DOUBLE_REF ,
1665+ CLONED_INSTEAD_OF_COPIED ,
16411666 INEFFICIENT_TO_STRING ,
16421667 NEW_RET_NO_SELF ,
16431668 SINGLE_CHAR_PATTERN ,
@@ -1909,6 +1934,7 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
19091934 ( "as_mut" , [ ] ) => useless_asref:: check ( cx, expr, "as_mut" , recv) ,
19101935 ( "as_ref" , [ ] ) => useless_asref:: check ( cx, expr, "as_ref" , recv) ,
19111936 ( "assume_init" , [ ] ) => uninit_assumed_init:: check ( cx, expr, recv) ,
1937+ ( "cloned" , [ ] ) => cloned_instead_of_copied:: check ( cx, expr, recv, span) ,
19121938 ( "collect" , [ ] ) => match method_call ! ( recv) {
19131939 Some ( ( "cloned" , [ recv2] , _) ) => iter_cloned_collect:: check ( cx, expr, recv2) ,
19141940 Some ( ( "map" , [ m_recv, m_arg] , _) ) => {
0 commit comments