about summary refs log tree commit diff
diff options
context:
space:
mode:
authorvarkor <github@varkor.com>2019-04-30 23:17:58 +0100
committervarkor <github@varkor.com>2019-04-30 23:17:58 +0100
commit5e709b2910a029ec4f0c640233526e7ed335c26c (patch)
treea1c9408db292557e494a6e6c5fd041361a05e1b5
parent13962aff37bbe23e1c9063e2298318e1a9da8af2 (diff)
downloadrust-5e709b2910a029ec4f0c640233526e7ed335c26c.tar.gz
rust-5e709b2910a029ec4f0c640233526e7ed335c26c.zip
Refactor is_range_literal
-rw-r--r--src/librustc/hir/lowering.rs34
-rw-r--r--src/librustc_lint/types.rs7
2 files changed, 18 insertions, 23 deletions
diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs
index b84ee579a10..736736fe48b 100644
--- a/src/librustc/hir/lowering.rs
+++ b/src/librustc/hir/lowering.rs
@@ -5401,29 +5401,29 @@ fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body>) -> Vec<hir::BodyId> {
     body_ids
 }
 
-/// This function checks if the specified expression is a built-in range literal.
+/// Checks if the specified expression is a built-in range literal.
 /// (See: `LoweringContext::lower_expr()`).
 pub fn is_range_literal(sess: &Session, expr: &hir::Expr) -> bool {
     use hir::{Path, QPath, ExprKind, TyKind};
 
-    // We support `::std::ops::Range` and `::core::ops::Range` prefixes.
-    let is_range_path = |path: &Path| {
-        let mut segs = path.segments.iter().map(|seg| seg.ident.as_str());
+    // Returns whether the given path represents a (desugared) range,
+    // either in std or core, i.e. has either a `::std::ops::Range` or
+    // `::core::ops::Range` prefix.
+    fn is_range_path(path: &Path) -> bool {
+        let segs: Vec<_> = path.segments.iter().map(|seg| seg.ident.as_str().to_string()).collect();
+        let segs: Vec<_> = segs.iter().map(|seg| &**seg).collect();
 
-        if let (Some(root), Some(std_core), Some(ops), Some(range), None) =
-            (segs.next(), segs.next(), segs.next(), segs.next(), segs.next())
-        {
-            // "{{root}}" is the equivalent of `::` prefix in `Path`.
-            root == "{{root}}" && (std_core == "std" || std_core == "core")
-                && ops == "ops" && range.starts_with("Range")
+        // "{{root}}" is the equivalent of `::` prefix in `Path`.
+        if let ["{{root}}", std_core, "ops", range] = segs.as_slice() {
+            (*std_core == "std" || *std_core == "core") && range.starts_with("Range")
         } else {
             false
         }
     };
 
-    let span_is_range_literal = |span: &Span| {
-        // Check whether a span corresponding to a range expression
-        // is a range literal, rather than an explicit struct or `new()` call.
+    // Check whether a span corresponding to a range expression is a
+    // range literal, rather than an explicit struct or `new()` call.
+    fn is_range_literal(sess: &Session, span: &Span) -> bool {
         let source_map = sess.source_map();
         let end_point = source_map.end_point(*span);
 
@@ -5438,21 +5438,21 @@ pub fn is_range_literal(sess: &Session, expr: &hir::Expr) -> bool {
         // All built-in range literals but `..=` and `..` desugar to `Struct`s.
         ExprKind::Struct(ref qpath, _, _) => {
             if let QPath::Resolved(None, ref path) = **qpath {
-                return is_range_path(&path) && span_is_range_literal(&expr.span);
+                return is_range_path(&path) && is_range_literal(sess, &expr.span);
             }
         }
 
         // `..` desugars to its struct path.
         ExprKind::Path(QPath::Resolved(None, ref path)) => {
-            return is_range_path(&path) && span_is_range_literal(&expr.span);
+            return is_range_path(&path) && is_range_literal(sess, &expr.span);
         }
 
         // `..=` desugars into `::std::ops::RangeInclusive::new(...)`.
         ExprKind::Call(ref func, _) => {
             if let ExprKind::Path(QPath::TypeRelative(ref ty, ref segment)) = func.node {
                 if let TyKind::Path(QPath::Resolved(None, ref path)) = ty.node {
-                    let call_to_new = segment.ident.as_str() == "new";
-                    return is_range_path(&path) && span_is_range_literal(&expr.span) && call_to_new;
+                    let new_call = segment.ident.as_str() == "new";
+                    return is_range_path(&path) && is_range_literal(sess, &expr.span) && new_call;
                 }
             }
         }
diff --git a/src/librustc_lint/types.rs b/src/librustc_lint/types.rs
index 379b20ab760..5cafd3d676f 100644
--- a/src/librustc_lint/types.rs
+++ b/src/librustc_lint/types.rs
@@ -92,12 +92,7 @@ fn lint_overflowing_range_endpoint<'a, 'tcx>(
                     LitKind::Int(_, LitIntType::Unsuffixed) => "".to_owned(),
                     _ => bug!(),
                 };
-                let suggestion = format!(
-                    "{}..={}{}",
-                    start,
-                    lit_val - 1,
-                    suffix,
-                );
+                let suggestion = format!("{}..={}{}", start, lit_val - 1, suffix);
                 err.span_suggestion(
                     parent_expr.span,
                     &"use an inclusive range instead",