@@ -5401,29 +5401,29 @@ fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body>) -> Vec<hir::BodyId> {
5401
5401
body_ids
5402
5402
}
5403
5403
5404
- /// This function checks if the specified expression is a built-in range literal.
5404
+ /// Checks if the specified expression is a built-in range literal.
5405
5405
/// (See: `LoweringContext::lower_expr()`).
5406
5406
pub fn is_range_literal ( sess : & Session , expr : & hir:: Expr ) -> bool {
5407
5407
use hir:: { Path , QPath , ExprKind , TyKind } ;
5408
5408
5409
- // We support `::std::ops::Range` and `::core::ops::Range` prefixes.
5410
- let is_range_path = |path : & Path | {
5411
- let mut segs = path. segments . iter ( ) . map ( |seg| seg. ident . as_str ( ) ) ;
5409
+ // Returns whether the given path represents a (desugared) range,
5410
+ // either in std or core, i.e. has either a `::std::ops::Range` or
5411
+ // `::core::ops::Range` prefix.
5412
+ fn is_range_path ( path : & Path ) -> bool {
5413
+ let segs: Vec < _ > = path. segments . iter ( ) . map ( |seg| seg. ident . as_str ( ) . to_string ( ) ) . collect ( ) ;
5414
+ let segs: Vec < _ > = segs. iter ( ) . map ( |seg| & * * seg) . collect ( ) ;
5412
5415
5413
- if let ( Some ( root) , Some ( std_core) , Some ( ops) , Some ( range) , None ) =
5414
- ( segs. next ( ) , segs. next ( ) , segs. next ( ) , segs. next ( ) , segs. next ( ) )
5415
- {
5416
- // "{{root}}" is the equivalent of `::` prefix in `Path`.
5417
- root == "{{root}}" && ( std_core == "std" || std_core == "core" )
5418
- && ops == "ops" && range. starts_with ( "Range" )
5416
+ // "{{root}}" is the equivalent of `::` prefix in `Path`.
5417
+ if let [ "{{root}}" , std_core, "ops" , range] = segs. as_slice ( ) {
5418
+ ( * std_core == "std" || * std_core == "core" ) && range. starts_with ( "Range" )
5419
5419
} else {
5420
5420
false
5421
5421
}
5422
5422
} ;
5423
5423
5424
- let span_is_range_literal = | span : & Span | {
5425
- // Check whether a span corresponding to a range expression
5426
- // is a range literal, rather than an explicit struct or `new()` call.
5424
+ // Check whether a span corresponding to a range expression is a
5425
+ // range literal, rather than an explicit struct or `new()` call.
5426
+ fn is_range_literal ( sess : & Session , span : & Span ) -> bool {
5427
5427
let source_map = sess. source_map ( ) ;
5428
5428
let end_point = source_map. end_point ( * span) ;
5429
5429
@@ -5438,21 +5438,21 @@ pub fn is_range_literal(sess: &Session, expr: &hir::Expr) -> bool {
5438
5438
// All built-in range literals but `..=` and `..` desugar to `Struct`s.
5439
5439
ExprKind :: Struct ( ref qpath, _, _) => {
5440
5440
if let QPath :: Resolved ( None , ref path) = * * qpath {
5441
- return is_range_path ( & path) && span_is_range_literal ( & expr. span ) ;
5441
+ return is_range_path ( & path) && is_range_literal ( sess , & expr. span ) ;
5442
5442
}
5443
5443
}
5444
5444
5445
5445
// `..` desugars to its struct path.
5446
5446
ExprKind :: Path ( QPath :: Resolved ( None , ref path) ) => {
5447
- return is_range_path ( & path) && span_is_range_literal ( & expr. span ) ;
5447
+ return is_range_path ( & path) && is_range_literal ( sess , & expr. span ) ;
5448
5448
}
5449
5449
5450
5450
// `..=` desugars into `::std::ops::RangeInclusive::new(...)`.
5451
5451
ExprKind :: Call ( ref func, _) => {
5452
5452
if let ExprKind :: Path ( QPath :: TypeRelative ( ref ty, ref segment) ) = func. node {
5453
5453
if let TyKind :: Path ( QPath :: Resolved ( None , ref path) ) = ty. node {
5454
- let call_to_new = segment. ident . as_str ( ) == "new" ;
5455
- return is_range_path ( & path) && span_is_range_literal ( & expr. span ) && call_to_new ;
5454
+ let new_call = segment. ident . as_str ( ) == "new" ;
5455
+ return is_range_path ( & path) && is_range_literal ( sess , & expr. span ) && new_call ;
5456
5456
}
5457
5457
}
5458
5458
}
0 commit comments