@@ -12,6 +12,7 @@ use rustc_hir::QPath;
12
12
use rustc_lint:: LateContext ;
13
13
use rustc_middle:: query:: Key ;
14
14
use rustc_middle:: ty;
15
+ use rustc_middle:: ty:: Ty ;
15
16
use rustc_span:: sym;
16
17
use rustc_span:: Symbol ;
17
18
@@ -21,7 +22,7 @@ use rustc_span::Symbol;
21
22
/// ^^^^^^^^^ ^^^^^^ true
22
23
/// `vec![1,2].drain(..).collect::<HashSet<_>>()`
23
24
/// ^^^^^^^^^ ^^^^^^^^^^ 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 {
25
26
if let Some ( expr_adt_did) = expr. ty_adt_id ( )
26
27
&& let Some ( recv_adt_did) = recv. ty_adt_id ( )
27
28
{
@@ -32,33 +33,21 @@ fn types_match_diagnostic_item(cx: &LateContext<'_>, expr: ty::Ty<'_>, recv: ty:
32
33
}
33
34
34
35
/// 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 {
42
37
( types_match_diagnostic_item ( cx, expr, recv, sym:: Vec )
43
38
|| types_match_diagnostic_item ( cx, expr, recv, sym:: VecDeque ) )
44
39
&& matches ! ( args, [ arg] if is_range_full( cx, arg, Some ( recv_path) ) )
45
40
}
46
41
47
42
/// 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 {
55
44
is_type_lang_item ( cx, expr, LangItem :: String )
56
45
&& is_type_lang_item ( cx, recv, LangItem :: String )
57
46
&& matches ! ( args, [ arg] if is_range_full( cx, arg, Some ( recv_path) ) )
58
47
}
59
48
60
49
/// 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 > {
62
51
types_match_diagnostic_item ( cx, expr, recv, sym:: HashSet )
63
52
. then_some ( "HashSet" )
64
53
. 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
83
72
expr. span ,
84
73
& format ! ( "you seem to be trying to move all elements into a new `{typename}`" ) ,
85
74
"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})" )
89
79
} ,
90
80
Applicability :: MachineApplicable ,
91
81
) ;
0 commit comments