1
1
use 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} ;
3
3
use clippy_utils:: visitors:: { for_each_expr, Visitable } ;
4
4
use clippy_utils:: { get_enclosing_block, path_to_local_id} ;
5
5
use core:: ops:: ControlFlow ;
6
6
use rustc_hir:: { Body , ExprKind , HirId , LangItem , LetStmt , Node , PatKind } ;
7
7
use rustc_lint:: { LateContext , LateLintPass } ;
8
8
use rustc_session:: declare_lint_pass;
9
9
use rustc_span:: symbol:: sym;
10
- use rustc_span:: Symbol ;
11
10
12
11
declare_clippy_lint ! {
13
12
/// ### What it does
@@ -44,24 +43,11 @@ declare_clippy_lint! {
44
43
}
45
44
declare_lint_pass ! ( CollectionIsNeverRead => [ COLLECTION_IS_NEVER_READ ] ) ;
46
45
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
-
60
46
impl < ' tcx > LateLintPass < ' tcx > for CollectionIsNeverRead {
61
47
fn check_local ( & mut self , cx : & LateContext < ' tcx > , local : & ' tcx LetStmt < ' tcx > ) {
62
48
// Look for local variables whose type is a container. Search surrounding block for read access.
63
49
if let PatKind :: Binding ( _, local_id, _, _) = local. pat . kind
64
- && match_acceptable_type ( cx, local, & COLLECTIONS )
50
+ && match_acceptable_type ( cx, local)
65
51
&& let Some ( enclosing_block) = get_enclosing_block ( cx, local. hir_id )
66
52
&& has_no_read_access ( cx, local_id, enclosing_block)
67
53
{
@@ -70,11 +56,22 @@ impl<'tcx> LateLintPass<'tcx> for CollectionIsNeverRead {
70
56
}
71
57
}
72
58
73
- fn match_acceptable_type ( cx : & LateContext < ' _ > , local : & LetStmt < ' _ > , collections : & [ Symbol ] ) -> bool {
59
+ fn match_acceptable_type ( cx : & LateContext < ' _ > , local : & LetStmt < ' _ > ) -> bool {
74
60
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 )
78
75
}
79
76
80
77
fn has_no_read_access < ' tcx , T : Visitable < ' tcx > > ( cx : & LateContext < ' tcx > , id : HirId , block : T ) -> bool {
0 commit comments