1
1
use clippy_utils:: diagnostics:: span_lint;
2
- use clippy_utils:: ty:: is_type_diagnostic_item;
2
+ use clippy_utils:: ty:: { is_type_diagnostic_item, is_type_lang_item } ;
3
3
use clippy_utils:: visitors:: for_each_expr_with_closures;
4
4
use clippy_utils:: { get_enclosing_block, get_parent_node, path_to_local_id} ;
5
5
use core:: ops:: ControlFlow ;
6
- use rustc_hir:: { Block , ExprKind , HirId , Local , Node , PatKind } ;
6
+ use rustc_hir:: { Block , ExprKind , HirId , LangItem , Local , Node , PatKind } ;
7
7
use rustc_lint:: { LateContext , LateLintPass } ;
8
8
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
9
9
use rustc_span:: symbol:: sym;
@@ -44,24 +44,23 @@ declare_clippy_lint! {
44
44
}
45
45
declare_lint_pass ! ( CollectionIsNeverRead => [ COLLECTION_IS_NEVER_READ ] ) ;
46
46
47
- static COLLECTIONS : [ Symbol ; 10 ] = [
47
+ // Add `String` here when it is added to diagnostic items
48
+ static COLLECTIONS : [ Symbol ; 9 ] = [
48
49
sym:: BTreeMap ,
49
50
sym:: BTreeSet ,
50
51
sym:: BinaryHeap ,
51
52
sym:: HashMap ,
52
53
sym:: HashSet ,
53
54
sym:: LinkedList ,
54
55
sym:: Option ,
55
- sym:: String ,
56
56
sym:: Vec ,
57
57
sym:: VecDeque ,
58
58
] ;
59
59
60
60
impl < ' tcx > LateLintPass < ' tcx > for CollectionIsNeverRead {
61
61
fn check_local ( & mut self , cx : & LateContext < ' tcx > , local : & ' tcx Local < ' tcx > ) {
62
62
// Look for local variables whose type is a container. Search surrounding bock for read access.
63
- let ty = cx. typeck_results ( ) . pat_ty ( local. pat ) ;
64
- if COLLECTIONS . iter ( ) . any ( |& sym| is_type_diagnostic_item ( cx, ty, sym) )
63
+ if match_acceptable_type ( cx, local, & COLLECTIONS )
65
64
&& let PatKind :: Binding ( _, local_id, _, _) = local. pat . kind
66
65
&& let Some ( enclosing_block) = get_enclosing_block ( cx, local. hir_id )
67
66
&& has_no_read_access ( cx, local_id, enclosing_block)
@@ -71,6 +70,13 @@ impl<'tcx> LateLintPass<'tcx> for CollectionIsNeverRead {
71
70
}
72
71
}
73
72
73
+ fn match_acceptable_type ( cx : & LateContext < ' _ > , local : & Local < ' _ > , collections : & [ rustc_span:: Symbol ] ) -> bool {
74
+ 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 )
78
+ }
79
+
74
80
fn has_no_read_access < ' tcx > ( cx : & LateContext < ' tcx > , id : HirId , block : & ' tcx Block < ' tcx > ) -> bool {
75
81
let mut has_access = false ;
76
82
let mut has_read_access = false ;
0 commit comments