@@ -2,14 +2,14 @@ use clippy_config::msrvs::{self, Msrv};
22use clippy_config:: Conf ;
33use clippy_utils:: diagnostics:: span_lint_and_sugg;
44use clippy_utils:: source:: snippet;
5- use clippy_utils:: ty:: { is_type_diagnostic_item , is_type_lang_item} ;
5+ use clippy_utils:: ty:: { get_type_diagnostic_name , is_type_lang_item} ;
66use clippy_utils:: { match_def_path, paths, SpanlessEq } ;
77use rustc_errors:: Applicability ;
88use rustc_hir as hir;
99use rustc_hir:: def_id:: DefId ;
1010use rustc_hir:: ExprKind :: Assign ;
1111use rustc_lint:: { LateContext , LateLintPass } ;
12- use rustc_session:: { impl_lint_pass, RustcVersion } ;
12+ use rustc_session:: impl_lint_pass;
1313use rustc_span:: symbol:: sym;
1414use rustc_span:: Span ;
1515
@@ -20,16 +20,6 @@ const ACCEPTABLE_METHODS: [&[&str]; 5] = [
2020 & paths:: SLICE_INTO ,
2121 & paths:: VEC_DEQUE_ITER ,
2222] ;
23- const ACCEPTABLE_TYPES : [ ( rustc_span:: Symbol , Option < RustcVersion > ) ; 7 ] = [
24- ( sym:: BinaryHeap , Some ( msrvs:: BINARY_HEAP_RETAIN ) ) ,
25- ( sym:: BTreeSet , Some ( msrvs:: BTREE_SET_RETAIN ) ) ,
26- ( sym:: BTreeMap , Some ( msrvs:: BTREE_MAP_RETAIN ) ) ,
27- ( sym:: HashSet , Some ( msrvs:: HASH_SET_RETAIN ) ) ,
28- ( sym:: HashMap , Some ( msrvs:: HASH_MAP_RETAIN ) ) ,
29- ( sym:: Vec , None ) ,
30- ( sym:: VecDeque , None ) ,
31- ] ;
32- const MAP_TYPES : [ rustc_span:: Symbol ; 2 ] = [ sym:: BTreeMap , sym:: HashMap ] ;
3323
3424declare_clippy_lint ! {
3525 /// ### What it does
@@ -264,16 +254,22 @@ fn match_acceptable_def_path(cx: &LateContext<'_>, collect_def_id: DefId) -> boo
264254}
265255
266256fn match_acceptable_type ( cx : & LateContext < ' _ > , expr : & hir:: Expr < ' _ > , msrv : & Msrv ) -> bool {
267- let expr_ty = cx. typeck_results ( ) . expr_ty ( expr) . peel_refs ( ) ;
268- ACCEPTABLE_TYPES . iter ( ) . any ( |( ty, acceptable_msrv) | {
269- is_type_diagnostic_item ( cx, expr_ty, * ty)
270- && acceptable_msrv. map_or ( true , |acceptable_msrv| msrv. meets ( acceptable_msrv) )
271- } )
257+ let ty = cx. typeck_results ( ) . expr_ty ( expr) . peel_refs ( ) ;
258+ let required = match get_type_diagnostic_name ( cx, ty) {
259+ Some ( sym:: BinaryHeap ) => msrvs:: BINARY_HEAP_RETAIN ,
260+ Some ( sym:: BTreeSet ) => msrvs:: BTREE_SET_RETAIN ,
261+ Some ( sym:: BTreeMap ) => msrvs:: BTREE_MAP_RETAIN ,
262+ Some ( sym:: HashSet ) => msrvs:: HASH_SET_RETAIN ,
263+ Some ( sym:: HashMap ) => msrvs:: HASH_MAP_RETAIN ,
264+ Some ( sym:: Vec | sym:: VecDeque ) => return true ,
265+ _ => return false ,
266+ } ;
267+ msrv. meets ( required)
272268}
273269
274270fn match_map_type ( cx : & LateContext < ' _ > , expr : & hir:: Expr < ' _ > ) -> bool {
275- let expr_ty = cx. typeck_results ( ) . expr_ty ( expr) . peel_refs ( ) ;
276- MAP_TYPES . iter ( ) . any ( |ty| is_type_diagnostic_item ( cx, expr_ty , * ty ) )
271+ let ty = cx. typeck_results ( ) . expr_ty ( expr) . peel_refs ( ) ;
272+ matches ! ( get_type_diagnostic_name ( cx, ty ) , Some ( sym :: BTreeMap | sym :: HashMap ) )
277273}
278274
279275fn make_span_lint_and_sugg ( cx : & LateContext < ' _ > , span : Span , sugg : String ) {
0 commit comments