@@ -25,6 +25,38 @@ declare_clippy_lint! {
25
25
"invalid path"
26
26
}
27
27
28
+ declare_lint_pass ! ( InvalidPaths => [ INVALID_PATHS ] ) ;
29
+
30
+ impl < ' tcx > LateLintPass < ' tcx > for InvalidPaths {
31
+ fn check_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx Item < ' _ > ) {
32
+ let local_def_id = & cx. tcx . parent_module ( item. hir_id ( ) ) ;
33
+ let mod_name = & cx. tcx . item_name ( local_def_id. to_def_id ( ) ) ;
34
+ if_chain ! {
35
+ if mod_name. as_str( ) == "paths" ;
36
+ if let hir:: ItemKind :: Const ( ty, body_id) = item. kind;
37
+ let ty = hir_ty_to_ty( cx. tcx, ty) ;
38
+ if let ty:: Array ( el_ty, _) = & ty. kind( ) ;
39
+ if let ty:: Ref ( _, el_ty, _) = & el_ty. kind( ) ;
40
+ if el_ty. is_str( ) ;
41
+ let body = cx. tcx. hir( ) . body( body_id) ;
42
+ let typeck_results = cx. tcx. typeck_body( body_id) ;
43
+ if let Some ( Constant :: Vec ( path) ) = constant_simple( cx, typeck_results, body. value) ;
44
+ let path: Vec <& str > = path. iter( ) . map( |x| {
45
+ if let Constant :: Str ( s) = x {
46
+ s. as_str( )
47
+ } else {
48
+ // We checked the type of the constant above
49
+ unreachable!( )
50
+ }
51
+ } ) . collect( ) ;
52
+ if !check_path( cx, & path[ ..] ) ;
53
+ then {
54
+ span_lint( cx, INVALID_PATHS , item. span, "invalid path" ) ;
55
+ }
56
+ }
57
+ }
58
+ }
59
+
28
60
// This is not a complete resolver for paths. It works on all the paths currently used in the paths
29
61
// module. That's all it does and all it needs to do.
30
62
pub fn check_path ( cx : & LateContext < ' _ > , path : & [ & str ] ) -> bool {
@@ -71,35 +103,3 @@ pub fn check_path(cx: &LateContext<'_>, path: &[&str]) -> bool {
71
103
72
104
false
73
105
}
74
-
75
- declare_lint_pass ! ( InvalidPaths => [ INVALID_PATHS ] ) ;
76
-
77
- impl < ' tcx > LateLintPass < ' tcx > for InvalidPaths {
78
- fn check_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx Item < ' _ > ) {
79
- let local_def_id = & cx. tcx . parent_module ( item. hir_id ( ) ) ;
80
- let mod_name = & cx. tcx . item_name ( local_def_id. to_def_id ( ) ) ;
81
- if_chain ! {
82
- if mod_name. as_str( ) == "paths" ;
83
- if let hir:: ItemKind :: Const ( ty, body_id) = item. kind;
84
- let ty = hir_ty_to_ty( cx. tcx, ty) ;
85
- if let ty:: Array ( el_ty, _) = & ty. kind( ) ;
86
- if let ty:: Ref ( _, el_ty, _) = & el_ty. kind( ) ;
87
- if el_ty. is_str( ) ;
88
- let body = cx. tcx. hir( ) . body( body_id) ;
89
- let typeck_results = cx. tcx. typeck_body( body_id) ;
90
- if let Some ( Constant :: Vec ( path) ) = constant_simple( cx, typeck_results, body. value) ;
91
- let path: Vec <& str > = path. iter( ) . map( |x| {
92
- if let Constant :: Str ( s) = x {
93
- s. as_str( )
94
- } else {
95
- // We checked the type of the constant above
96
- unreachable!( )
97
- }
98
- } ) . collect( ) ;
99
- if !check_path( cx, & path[ ..] ) ;
100
- then {
101
- span_lint( cx, INVALID_PATHS , item. span, "invalid path" ) ;
102
- }
103
- }
104
- }
105
- }
0 commit comments