3
3
4
4
#![ deny( clippy:: missing_docs_in_private_items) ]
5
5
6
- use crate :: utils:: { is_expn_of, match_def_path, match_qpath , paths} ;
6
+ use crate :: utils:: { is_expn_of, match_def_path, paths} ;
7
7
use if_chain:: if_chain;
8
8
use rustc_ast:: ast;
9
9
use rustc_hir as hir;
10
10
use rustc_lint:: LateContext ;
11
- use rustc_middle:: ty;
12
11
13
12
/// Converts a hir binary operator to the corresponding `ast` type.
14
13
#[ must_use]
@@ -47,7 +46,7 @@ pub struct Range<'a> {
47
46
}
48
47
49
48
/// Higher a `hir` range to something similar to `ast::ExprKind::Range`.
50
- pub fn range < ' a , ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' a hir:: Expr < ' _ > ) -> Option < Range < ' a > > {
49
+ pub fn range < ' a > ( expr : & ' a hir:: Expr < ' _ > ) -> Option < Range < ' a > > {
51
50
/// Finds the field named `name` in the field. Always return `Some` for
52
51
/// convenience.
53
52
fn get_field < ' c > ( name : & str , fields : & ' c [ hir:: Field < ' _ > ] ) -> Option < & ' c hir:: Expr < ' c > > {
@@ -56,94 +55,43 @@ pub fn range<'a, 'tcx>(cx: &LateContext<'tcx>, expr: &'a hir::Expr<'_>) -> Optio
56
55
Some ( expr)
57
56
}
58
57
59
- let def_path = match cx. typeck_results ( ) . expr_ty ( expr) . kind {
60
- ty:: Adt ( def, _) => cx. tcx . def_path ( def. did ) ,
61
- _ => return None ,
62
- } ;
63
-
64
- // sanity checks for std::ops::RangeXXXX
65
- if def_path. data . len ( ) != 3 {
66
- return None ;
67
- }
68
- if def_path. data . get ( 0 ) ?. data . as_symbol ( ) != sym ! ( ops) {
69
- return None ;
70
- }
71
- if def_path. data . get ( 1 ) ?. data . as_symbol ( ) != sym ! ( range) {
72
- return None ;
73
- }
74
- let type_name = def_path. data . get ( 2 ) ?. data . as_symbol ( ) ;
75
- let range_types = [
76
- "RangeFrom" ,
77
- "RangeFull" ,
78
- "RangeInclusive" ,
79
- "Range" ,
80
- "RangeTo" ,
81
- "RangeToInclusive" ,
82
- ] ;
83
- if !range_types. contains ( & & * type_name. as_str ( ) ) {
84
- return None ;
85
- }
86
-
87
- // The range syntax is expanded to literal paths starting with `core` or `std`
88
- // depending on
89
- // `#[no_std]`. Testing both instead of resolving the paths.
90
-
91
58
match expr. kind {
92
- hir:: ExprKind :: Path ( ref path) => {
93
- if match_qpath ( path, & paths:: RANGE_FULL_STD ) || match_qpath ( path, & paths:: RANGE_FULL ) {
94
- Some ( Range {
59
+ hir:: ExprKind :: Call ( ref path, ref args) if matches ! (
60
+ path. kind,
61
+ hir:: ExprKind :: Path ( hir:: QPath :: LangItem ( hir:: LangItem :: RangeInclusiveNew , _) )
62
+ ) => Some ( Range {
63
+ start : Some ( & args[ 0 ] ) ,
64
+ end : Some ( & args[ 1 ] ) ,
65
+ limits : ast:: RangeLimits :: Closed ,
66
+ } ) ,
67
+ hir:: ExprKind :: Struct ( ref path, ref fields, None ) => {
68
+ match path {
69
+ hir:: QPath :: LangItem ( hir:: LangItem :: RangeFull , _) => Some ( Range {
95
70
start : None ,
96
71
end : None ,
97
72
limits : ast:: RangeLimits :: HalfOpen ,
98
- } )
99
- } else {
100
- None
101
- }
102
- } ,
103
- hir:: ExprKind :: Call ( ref path, ref args) => {
104
- if let hir:: ExprKind :: Path ( ref path) = path. kind {
105
- if match_qpath ( path, & paths:: RANGE_INCLUSIVE_STD_NEW ) || match_qpath ( path, & paths:: RANGE_INCLUSIVE_NEW )
106
- {
107
- Some ( Range {
108
- start : Some ( & args[ 0 ] ) ,
109
- end : Some ( & args[ 1 ] ) ,
110
- limits : ast:: RangeLimits :: Closed ,
111
- } )
112
- } else {
113
- None
114
- }
115
- } else {
116
- None
117
- }
118
- } ,
119
- hir:: ExprKind :: Struct ( ref path, ref fields, None ) => {
120
- if match_qpath ( path, & paths:: RANGE_FROM_STD ) || match_qpath ( path, & paths:: RANGE_FROM ) {
121
- Some ( Range {
73
+ } ) ,
74
+ hir:: QPath :: LangItem ( hir:: LangItem :: RangeFrom , _) => Some ( Range {
122
75
start : Some ( get_field ( "start" , fields) ?) ,
123
76
end : None ,
124
77
limits : ast:: RangeLimits :: HalfOpen ,
125
- } )
126
- } else if match_qpath ( path, & paths:: RANGE_STD ) || match_qpath ( path, & paths:: RANGE ) {
127
- Some ( Range {
78
+ } ) ,
79
+ hir:: QPath :: LangItem ( hir:: LangItem :: Range , _) => Some ( Range {
128
80
start : Some ( get_field ( "start" , fields) ?) ,
129
81
end : Some ( get_field ( "end" , fields) ?) ,
130
82
limits : ast:: RangeLimits :: HalfOpen ,
131
- } )
132
- } else if match_qpath ( path, & paths:: RANGE_TO_INCLUSIVE_STD ) || match_qpath ( path, & paths:: RANGE_TO_INCLUSIVE )
133
- {
134
- Some ( Range {
83
+ } ) ,
84
+ hir:: QPath :: LangItem ( hir:: LangItem :: RangeToInclusive , _) => Some ( Range {
135
85
start : None ,
136
86
end : Some ( get_field ( "end" , fields) ?) ,
137
87
limits : ast:: RangeLimits :: Closed ,
138
- } )
139
- } else if match_qpath ( path, & paths:: RANGE_TO_STD ) || match_qpath ( path, & paths:: RANGE_TO ) {
140
- Some ( Range {
88
+ } ) ,
89
+ hir:: QPath :: LangItem ( hir:: LangItem :: RangeTo , _) => Some ( Range {
141
90
start : None ,
142
91
end : Some ( get_field ( "end" , fields) ?) ,
143
92
limits : ast:: RangeLimits :: HalfOpen ,
144
- } )
145
- } else {
146
- None
93
+ } ) ,
94
+ _ => None ,
147
95
}
148
96
} ,
149
97
_ => None ,
0 commit comments