@@ -12,6 +12,7 @@ use rustc_hir::QPath;
1212use rustc_lint:: LateContext ;
1313use rustc_middle:: query:: Key ;
1414use rustc_middle:: ty;
15+ use rustc_middle:: ty:: Ty ;
1516use rustc_span:: sym;
1617use rustc_span:: Symbol ;
1718
@@ -21,7 +22,7 @@ use rustc_span::Symbol;
2122/// ^^^^^^^^^ ^^^^^^ true
2223/// `vec![1,2].drain(..).collect::<HashSet<_>>()`
2324/// ^^^^^^^^^ ^^^^^^^^^^ false
24- fn types_match_diagnostic_item ( cx : & LateContext < ' _ > , expr : ty :: Ty < ' _ > , recv : ty :: Ty < ' _ > , sym : Symbol ) -> bool {
25+ fn types_match_diagnostic_item ( cx : & LateContext < ' _ > , expr : Ty < ' _ > , recv : Ty < ' _ > , sym : Symbol ) -> bool {
2526 if let Some ( expr_adt_did) = expr. ty_adt_id ( )
2627 && let Some ( recv_adt_did) = recv. ty_adt_id ( )
2728 {
@@ -32,33 +33,21 @@ fn types_match_diagnostic_item(cx: &LateContext<'_>, expr: ty::Ty<'_>, recv: ty:
3233}
3334
3435/// Checks `std::{vec::Vec, collections::VecDeque}`.
35- fn check_vec (
36- cx : & LateContext < ' _ > ,
37- args : & [ Expr < ' _ > ] ,
38- expr : ty:: Ty < ' _ > ,
39- recv : ty:: Ty < ' _ > ,
40- recv_path : & Path < ' _ > ,
41- ) -> bool {
36+ fn check_vec ( cx : & LateContext < ' _ > , args : & [ Expr < ' _ > ] , expr : Ty < ' _ > , recv : Ty < ' _ > , recv_path : & Path < ' _ > ) -> bool {
4237 ( types_match_diagnostic_item ( cx, expr, recv, sym:: Vec )
4338 || types_match_diagnostic_item ( cx, expr, recv, sym:: VecDeque ) )
4439 && matches ! ( args, [ arg] if is_range_full( cx, arg, Some ( recv_path) ) )
4540}
4641
4742/// Checks `std::string::String`
48- fn check_string (
49- cx : & LateContext < ' _ > ,
50- args : & [ Expr < ' _ > ] ,
51- expr : ty:: Ty < ' _ > ,
52- recv : ty:: Ty < ' _ > ,
53- recv_path : & Path < ' _ > ,
54- ) -> bool {
43+ fn check_string ( cx : & LateContext < ' _ > , args : & [ Expr < ' _ > ] , expr : Ty < ' _ > , recv : Ty < ' _ > , recv_path : & Path < ' _ > ) -> bool {
5544 is_type_lang_item ( cx, expr, LangItem :: String )
5645 && is_type_lang_item ( cx, recv, LangItem :: String )
5746 && matches ! ( args, [ arg] if is_range_full( cx, arg, Some ( recv_path) ) )
5847}
5948
6049/// Checks `std::collections::{HashSet, HashMap, BinaryHeap}`.
61- fn check_collections ( cx : & LateContext < ' _ > , expr : ty :: Ty < ' _ > , recv : ty :: Ty < ' _ > ) -> Option < & ' static str > {
50+ fn check_collections ( cx : & LateContext < ' _ > , expr : Ty < ' _ > , recv : Ty < ' _ > ) -> Option < & ' static str > {
6251 types_match_diagnostic_item ( cx, expr, recv, sym:: HashSet )
6352 . then_some ( "HashSet" )
6453 . or_else ( || types_match_diagnostic_item ( cx, expr, recv, sym:: HashMap ) . then_some ( "HashMap" ) )
@@ -83,9 +72,10 @@ pub(super) fn check(cx: &LateContext<'_>, args: &[Expr<'_>], expr: &Expr<'_>, re
8372 expr. span ,
8473 & format ! ( "you seem to be trying to move all elements into a new `{typename}`" ) ,
8574 "consider using `mem::take`" ,
86- match recv_ty. kind ( ) {
87- ty:: Ref ( ..) => format ! ( "std::mem::take({recv})" ) ,
88- _ => format ! ( "std::mem::take(&mut {recv})" ) ,
75+ if let ty:: Ref ( ..) = recv_ty. kind ( ) {
76+ format ! ( "std::mem::take({recv})" )
77+ } else {
78+ format ! ( "std::mem::take(&mut {recv})" )
8979 } ,
9080 Applicability :: MachineApplicable ,
9181 ) ;
0 commit comments