@@ -218,51 +218,56 @@ pub struct Range<'a> {
218
218
219
219
impl < ' a > Range < ' a > {
220
220
/// Higher a `hir` range to something similar to `ast::ExprKind::Range`.
221
+ #[ allow( clippy:: similar_names) ]
221
222
pub fn hir ( expr : & ' a Expr < ' _ > ) -> Option < Range < ' a > > {
222
- /// Finds the field named `name` in the field. Always return `Some` for
223
- /// convenience.
224
- fn get_field < ' c > ( name : & str , fields : & ' c [ hir:: ExprField < ' _ > ] ) -> Option < & ' c Expr < ' c > > {
225
- let expr = & fields. iter ( ) . find ( |field| field. ident . name . as_str ( ) == name) ?. expr ;
226
- Some ( expr)
227
- }
228
-
229
223
match expr. kind {
230
- ExprKind :: Call ( path, args )
224
+ ExprKind :: Call ( path, [ arg1 , arg2 ] )
231
225
if matches ! (
232
226
path. kind,
233
227
ExprKind :: Path ( QPath :: LangItem ( hir:: LangItem :: RangeInclusiveNew , ..) )
234
228
) =>
235
229
{
236
230
Some ( Range {
237
- start : Some ( & args [ 0 ] ) ,
238
- end : Some ( & args [ 1 ] ) ,
231
+ start : Some ( arg1 ) ,
232
+ end : Some ( arg2 ) ,
239
233
limits : ast:: RangeLimits :: Closed ,
240
234
} )
241
235
} ,
242
- ExprKind :: Struct ( path, fields, None ) => match & path {
243
- QPath :: LangItem ( hir:: LangItem :: RangeFull , ..) => Some ( Range {
236
+ ExprKind :: Struct ( path, fields, None ) => match ( path, fields ) {
237
+ ( QPath :: LangItem ( hir:: LangItem :: RangeFull , ..) , [ ] ) => Some ( Range {
244
238
start : None ,
245
239
end : None ,
246
240
limits : ast:: RangeLimits :: HalfOpen ,
247
241
} ) ,
248
- QPath :: LangItem ( hir:: LangItem :: RangeFrom , ..) => Some ( Range {
249
- start : Some ( get_field ( "start" , fields) ?) ,
250
- end : None ,
251
- limits : ast:: RangeLimits :: HalfOpen ,
252
- } ) ,
253
- QPath :: LangItem ( hir:: LangItem :: Range , ..) => Some ( Range {
254
- start : Some ( get_field ( "start" , fields) ?) ,
255
- end : Some ( get_field ( "end" , fields) ?) ,
256
- limits : ast:: RangeLimits :: HalfOpen ,
257
- } ) ,
258
- QPath :: LangItem ( hir:: LangItem :: RangeToInclusive , ..) => Some ( Range {
259
- start : None ,
260
- end : Some ( get_field ( "end" , fields) ?) ,
261
- limits : ast:: RangeLimits :: Closed ,
262
- } ) ,
263
- QPath :: LangItem ( hir:: LangItem :: RangeTo , ..) => Some ( Range {
242
+ ( QPath :: LangItem ( hir:: LangItem :: RangeFrom , ..) , [ field] ) if field. ident . name == sym:: start => {
243
+ Some ( Range {
244
+ start : Some ( field. expr ) ,
245
+ end : None ,
246
+ limits : ast:: RangeLimits :: HalfOpen ,
247
+ } )
248
+ } ,
249
+ ( QPath :: LangItem ( hir:: LangItem :: Range , ..) , [ field1, field2] ) => {
250
+ let ( start, end) = match ( field1. ident . name , field2. ident . name ) {
251
+ ( sym:: start, sym:: end) => ( field1. expr , field2. expr ) ,
252
+ ( sym:: end, sym:: start) => ( field2. expr , field1. expr ) ,
253
+ _ => return None ,
254
+ } ;
255
+ Some ( Range {
256
+ start : Some ( start) ,
257
+ end : Some ( end) ,
258
+ limits : ast:: RangeLimits :: HalfOpen ,
259
+ } )
260
+ } ,
261
+ ( QPath :: LangItem ( hir:: LangItem :: RangeToInclusive , ..) , [ field] ) if field. ident . name == sym:: end => {
262
+ Some ( Range {
263
+ start : None ,
264
+ end : Some ( field. expr ) ,
265
+ limits : ast:: RangeLimits :: Closed ,
266
+ } )
267
+ } ,
268
+ ( QPath :: LangItem ( hir:: LangItem :: RangeTo , ..) , [ field] ) if field. ident . name == sym:: end => Some ( Range {
264
269
start : None ,
265
- end : Some ( get_field ( "end" , fields ) ? ) ,
270
+ end : Some ( field . expr ) ,
266
271
limits : ast:: RangeLimits :: HalfOpen ,
267
272
} ) ,
268
273
_ => None ,
0 commit comments