11use clippy_utils:: diagnostics:: span_lint;
2- use clippy_utils:: ty:: { is_type_diagnostic_item , is_type_lang_item} ;
2+ use clippy_utils:: ty:: { get_type_diagnostic_name , is_type_lang_item} ;
33use clippy_utils:: visitors:: { for_each_expr, Visitable } ;
44use clippy_utils:: { get_enclosing_block, path_to_local_id} ;
55use core:: ops:: ControlFlow ;
66use rustc_hir:: { Body , ExprKind , HirId , LangItem , LetStmt , Node , PatKind } ;
77use rustc_lint:: { LateContext , LateLintPass } ;
88use rustc_session:: declare_lint_pass;
99use rustc_span:: symbol:: sym;
10- use rustc_span:: Symbol ;
1110
1211declare_clippy_lint ! {
1312 /// ### What it does
@@ -44,24 +43,11 @@ declare_clippy_lint! {
4443}
4544declare_lint_pass ! ( CollectionIsNeverRead => [ COLLECTION_IS_NEVER_READ ] ) ;
4645
47- // Add `String` here when it is added to diagnostic items
48- static COLLECTIONS : [ Symbol ; 9 ] = [
49- sym:: BTreeMap ,
50- sym:: BTreeSet ,
51- sym:: BinaryHeap ,
52- sym:: HashMap ,
53- sym:: HashSet ,
54- sym:: LinkedList ,
55- sym:: Option ,
56- sym:: Vec ,
57- sym:: VecDeque ,
58- ] ;
59-
6046impl < ' tcx > LateLintPass < ' tcx > for CollectionIsNeverRead {
6147 fn check_local ( & mut self , cx : & LateContext < ' tcx > , local : & ' tcx LetStmt < ' tcx > ) {
6248 // Look for local variables whose type is a container. Search surrounding block for read access.
6349 if let PatKind :: Binding ( _, local_id, _, _) = local. pat . kind
64- && match_acceptable_type ( cx, local, & COLLECTIONS )
50+ && match_acceptable_type ( cx, local)
6551 && let Some ( enclosing_block) = get_enclosing_block ( cx, local. hir_id )
6652 && has_no_read_access ( cx, local_id, enclosing_block)
6753 {
@@ -70,11 +56,22 @@ impl<'tcx> LateLintPass<'tcx> for CollectionIsNeverRead {
7056 }
7157}
7258
73- fn match_acceptable_type ( cx : & LateContext < ' _ > , local : & LetStmt < ' _ > , collections : & [ Symbol ] ) -> bool {
59+ fn match_acceptable_type ( cx : & LateContext < ' _ > , local : & LetStmt < ' _ > ) -> bool {
7460 let ty = cx. typeck_results ( ) . pat_ty ( local. pat ) ;
75- collections. iter ( ) . any ( |& sym| is_type_diagnostic_item ( cx, ty, sym) )
76- // String type is a lang item but not a diagnostic item for now so we need a separate check
77- || is_type_lang_item ( cx, ty, LangItem :: String )
61+ matches ! (
62+ get_type_diagnostic_name( cx, ty) ,
63+ Some (
64+ sym:: BTreeMap
65+ | sym:: BTreeSet
66+ | sym:: BinaryHeap
67+ | sym:: HashMap
68+ | sym:: HashSet
69+ | sym:: LinkedList
70+ | sym:: Option
71+ | sym:: Vec
72+ | sym:: VecDeque
73+ )
74+ ) || is_type_lang_item ( cx, ty, LangItem :: String )
7875}
7976
8077fn has_no_read_access < ' tcx , T : Visitable < ' tcx > > ( cx : & LateContext < ' tcx > , id : HirId , block : T ) -> bool {
0 commit comments