Skip to content

Commit 5e709b2

Browse files
committed
Refactor is_range_literal
1 parent 13962af commit 5e709b2

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

src/librustc/hir/lowering.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -5401,29 +5401,29 @@ fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body>) -> Vec<hir::BodyId> {
54015401
body_ids
54025402
}
54035403

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.
54055405
/// (See: `LoweringContext::lower_expr()`).
54065406
pub fn is_range_literal(sess: &Session, expr: &hir::Expr) -> bool {
54075407
use hir::{Path, QPath, ExprKind, TyKind};
54085408

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();
54125415

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")
54195419
} else {
54205420
false
54215421
}
54225422
};
54235423

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 {
54275427
let source_map = sess.source_map();
54285428
let end_point = source_map.end_point(*span);
54295429

@@ -5438,21 +5438,21 @@ pub fn is_range_literal(sess: &Session, expr: &hir::Expr) -> bool {
54385438
// All built-in range literals but `..=` and `..` desugar to `Struct`s.
54395439
ExprKind::Struct(ref qpath, _, _) => {
54405440
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);
54425442
}
54435443
}
54445444

54455445
// `..` desugars to its struct path.
54465446
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);
54485448
}
54495449

54505450
// `..=` desugars into `::std::ops::RangeInclusive::new(...)`.
54515451
ExprKind::Call(ref func, _) => {
54525452
if let ExprKind::Path(QPath::TypeRelative(ref ty, ref segment)) = func.node {
54535453
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;
54565456
}
54575457
}
54585458
}

src/librustc_lint/types.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,7 @@ fn lint_overflowing_range_endpoint<'a, 'tcx>(
9292
LitKind::Int(_, LitIntType::Unsuffixed) => "".to_owned(),
9393
_ => bug!(),
9494
};
95-
let suggestion = format!(
96-
"{}..={}{}",
97-
start,
98-
lit_val - 1,
99-
suffix,
100-
);
95+
let suggestion = format!("{}..={}{}", start, lit_val - 1, suffix);
10196
err.span_suggestion(
10297
parent_expr.span,
10398
&"use an inclusive range instead",

0 commit comments

Comments
 (0)