Skip to content

Commit 0cf0691

Browse files
committed
Auto merge of #51149 - zackmdavis:․․․_to_․․=, r=nikomatsakis
lint to favor `..=` over `...` range patterns; migrate to `..=` throughout codebase We probably need an RFC to actually deprecate the `...` syntax, but here's a candidate implementation for the lint considered in #51043. (My local build is super flaky, but hopefully I got all of the test revisions.)
2 parents 84804c3 + 64365e4 commit 0cf0691

File tree

77 files changed

+373
-240
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+373
-240
lines changed

src/libcore/ascii.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,15 @@ pub fn escape_default(c: u8) -> EscapeDefault {
108108
b'\\' => ([b'\\', b'\\', 0, 0], 2),
109109
b'\'' => ([b'\\', b'\'', 0, 0], 2),
110110
b'"' => ([b'\\', b'"', 0, 0], 2),
111-
b'\x20' ... b'\x7e' => ([c, 0, 0, 0], 1),
111+
b'\x20' ..= b'\x7e' => ([c, 0, 0, 0], 1),
112112
_ => ([b'\\', b'x', hexify(c >> 4), hexify(c & 0xf)], 4),
113113
};
114114

115115
return EscapeDefault { range: 0..len, data };
116116

117117
fn hexify(b: u8) -> u8 {
118118
match b {
119-
0 ... 9 => b'0' + b,
119+
0 ..= 9 => b'0' + b,
120120
_ => b'a' + b - 10,
121121
}
122122
}

src/libcore/char/decode.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl<I: Iterator<Item = u8>> Iterator for DecodeUtf8<I> {
6464
}
6565
}
6666
macro_rules! continuation_byte {
67-
() => { continuation_byte!(0x80...0xBF) };
67+
() => { continuation_byte!(0x80..=0xBF) };
6868
($range: pat) => {
6969
match self.0.peek() {
7070
Some(&byte @ $range) => {
@@ -77,43 +77,43 @@ impl<I: Iterator<Item = u8>> Iterator for DecodeUtf8<I> {
7777
}
7878

7979
match first_byte {
80-
0x00...0x7F => {
80+
0x00..=0x7F => {
8181
first_byte!(0b1111_1111);
8282
}
83-
0xC2...0xDF => {
83+
0xC2..=0xDF => {
8484
first_byte!(0b0001_1111);
8585
continuation_byte!();
8686
}
8787
0xE0 => {
8888
first_byte!(0b0000_1111);
89-
continuation_byte!(0xA0...0xBF); // 0x80...0x9F here are overlong
89+
continuation_byte!(0xA0..=0xBF); // 0x80..=0x9F here are overlong
9090
continuation_byte!();
9191
}
92-
0xE1...0xEC | 0xEE...0xEF => {
92+
0xE1..=0xEC | 0xEE..=0xEF => {
9393
first_byte!(0b0000_1111);
9494
continuation_byte!();
9595
continuation_byte!();
9696
}
9797
0xED => {
9898
first_byte!(0b0000_1111);
99-
continuation_byte!(0x80...0x9F); // 0xA0..0xBF here are surrogates
99+
continuation_byte!(0x80..=0x9F); // 0xA0..0xBF here are surrogates
100100
continuation_byte!();
101101
}
102102
0xF0 => {
103103
first_byte!(0b0000_0111);
104-
continuation_byte!(0x90...0xBF); // 0x80..0x8F here are overlong
104+
continuation_byte!(0x90..=0xBF); // 0x80..0x8F here are overlong
105105
continuation_byte!();
106106
continuation_byte!();
107107
}
108-
0xF1...0xF3 => {
108+
0xF1..=0xF3 => {
109109
first_byte!(0b0000_0111);
110110
continuation_byte!();
111111
continuation_byte!();
112112
continuation_byte!();
113113
}
114114
0xF4 => {
115115
first_byte!(0b0000_0111);
116-
continuation_byte!(0x80...0x8F); // 0x90..0xBF here are beyond char::MAX
116+
continuation_byte!(0x80..=0x8F); // 0x90..0xBF here are beyond char::MAX
117117
continuation_byte!();
118118
continuation_byte!();
119119
}

src/libcore/char/methods.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ impl char {
125125
panic!("to_digit: radix is too high (maximum 36)");
126126
}
127127
let val = match self {
128-
'0' ... '9' => self as u32 - '0' as u32,
129-
'a' ... 'z' => self as u32 - 'a' as u32 + 10,
130-
'A' ... 'Z' => self as u32 - 'A' as u32 + 10,
128+
'0' ..= '9' => self as u32 - '0' as u32,
129+
'a' ..= 'z' => self as u32 - 'a' as u32 + 10,
130+
'A' ..= 'Z' => self as u32 - 'A' as u32 + 10,
131131
_ => return None,
132132
};
133133
if val < radix { Some(val) }
@@ -305,7 +305,7 @@ impl char {
305305
'\r' => EscapeDefaultState::Backslash('r'),
306306
'\n' => EscapeDefaultState::Backslash('n'),
307307
'\\' | '\'' | '"' => EscapeDefaultState::Backslash(self),
308-
'\x20' ... '\x7e' => EscapeDefaultState::Char(self),
308+
'\x20' ..= '\x7e' => EscapeDefaultState::Char(self),
309309
_ => EscapeDefaultState::Unicode(self.escape_unicode())
310310
};
311311
EscapeDefault { state: init_state }
@@ -543,7 +543,7 @@ impl char {
543543
#[inline]
544544
pub fn is_alphabetic(self) -> bool {
545545
match self {
546-
'a'...'z' | 'A'...'Z' => true,
546+
'a'..='z' | 'A'..='Z' => true,
547547
c if c > '\x7f' => derived_property::Alphabetic(c),
548548
_ => false,
549549
}
@@ -599,7 +599,7 @@ impl char {
599599
#[inline]
600600
pub fn is_lowercase(self) -> bool {
601601
match self {
602-
'a'...'z' => true,
602+
'a'..='z' => true,
603603
c if c > '\x7f' => derived_property::Lowercase(c),
604604
_ => false,
605605
}
@@ -627,7 +627,7 @@ impl char {
627627
#[inline]
628628
pub fn is_uppercase(self) -> bool {
629629
match self {
630-
'A'...'Z' => true,
630+
'A'..='Z' => true,
631631
c if c > '\x7f' => derived_property::Uppercase(c),
632632
_ => false,
633633
}
@@ -654,7 +654,7 @@ impl char {
654654
#[inline]
655655
pub fn is_whitespace(self) -> bool {
656656
match self {
657-
' ' | '\x09'...'\x0d' => true,
657+
' ' | '\x09'..='\x0d' => true,
658658
c if c > '\x7f' => property::White_Space(c),
659659
_ => false,
660660
}
@@ -737,7 +737,7 @@ impl char {
737737
#[inline]
738738
pub fn is_numeric(self) -> bool {
739739
match self {
740-
'0'...'9' => true,
740+
'0'..='9' => true,
741741
c if c > '\x7f' => general_category::N(c),
742742
_ => false,
743743
}

src/libcore/fmt/num.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,19 @@ macro_rules! radix {
121121
fn digit(x: u8) -> u8 {
122122
match x {
123123
$($x => $conv,)+
124-
x => panic!("number not in the range 0..{}: {}", Self::BASE - 1, x),
124+
x => panic!("number not in the range 0..={}: {}", Self::BASE - 1, x),
125125
}
126126
}
127127
}
128128
}
129129
}
130130

131-
radix! { Binary, 2, "0b", x @ 0 ... 1 => b'0' + x }
132-
radix! { Octal, 8, "0o", x @ 0 ... 7 => b'0' + x }
133-
radix! { LowerHex, 16, "0x", x @ 0 ... 9 => b'0' + x,
134-
x @ 10 ... 15 => b'a' + (x - 10) }
135-
radix! { UpperHex, 16, "0x", x @ 0 ... 9 => b'0' + x,
136-
x @ 10 ... 15 => b'A' + (x - 10) }
131+
radix! { Binary, 2, "0b", x @ 0 ..= 1 => b'0' + x }
132+
radix! { Octal, 8, "0o", x @ 0 ..= 7 => b'0' + x }
133+
radix! { LowerHex, 16, "0x", x @ 0 ..= 9 => b'0' + x,
134+
x @ 10 ..= 15 => b'a' + (x - 10) }
135+
radix! { UpperHex, 16, "0x", x @ 0 ..= 9 => b'0' + x,
136+
x @ 10 ..= 15 => b'A' + (x - 10) }
137137

138138
macro_rules! int_base {
139139
($Trait:ident for $T:ident as $U:ident -> $Radix:ident) => {

src/libcore/slice/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,7 @@ impl<T> [T] {
12301230
/// assert_eq!(s.binary_search(&4), Err(7));
12311231
/// assert_eq!(s.binary_search(&100), Err(13));
12321232
/// let r = s.binary_search(&1);
1233-
/// assert!(match r { Ok(1...4) => true, _ => false, });
1233+
/// assert!(match r { Ok(1..=4) => true, _ => false, });
12341234
/// ```
12351235
#[stable(feature = "rust1", since = "1.0.0")]
12361236
pub fn binary_search(&self, x: &T) -> Result<usize, usize>
@@ -1268,7 +1268,7 @@ impl<T> [T] {
12681268
/// assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(13));
12691269
/// let seek = 1;
12701270
/// let r = s.binary_search_by(|probe| probe.cmp(&seek));
1271-
/// assert!(match r { Ok(1...4) => true, _ => false, });
1271+
/// assert!(match r { Ok(1..=4) => true, _ => false, });
12721272
/// ```
12731273
#[stable(feature = "rust1", since = "1.0.0")]
12741274
#[inline]
@@ -1325,7 +1325,7 @@ impl<T> [T] {
13251325
/// assert_eq!(s.binary_search_by_key(&4, |&(a,b)| b), Err(7));
13261326
/// assert_eq!(s.binary_search_by_key(&100, |&(a,b)| b), Err(13));
13271327
/// let r = s.binary_search_by_key(&1, |&(a,b)| b);
1328-
/// assert!(match r { Ok(1...4) => true, _ => false, });
1328+
/// assert!(match r { Ok(1..=4) => true, _ => false, });
13291329
/// ```
13301330
#[stable(feature = "slice_binary_search_by_key", since = "1.10.0")]
13311331
#[inline]

src/libcore/str/lossy.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ impl<'a> Iterator for Utf8LossyChunksIter<'a> {
101101
}
102102
3 => {
103103
match (byte, safe_get(self.source, i)) {
104-
(0xE0, 0xA0 ... 0xBF) => (),
105-
(0xE1 ... 0xEC, 0x80 ... 0xBF) => (),
106-
(0xED, 0x80 ... 0x9F) => (),
107-
(0xEE ... 0xEF, 0x80 ... 0xBF) => (),
104+
(0xE0, 0xA0 ..= 0xBF) => (),
105+
(0xE1 ..= 0xEC, 0x80 ..= 0xBF) => (),
106+
(0xED, 0x80 ..= 0x9F) => (),
107+
(0xEE ..= 0xEF, 0x80 ..= 0xBF) => (),
108108
_ => {
109109
error!();
110110
}
@@ -117,9 +117,9 @@ impl<'a> Iterator for Utf8LossyChunksIter<'a> {
117117
}
118118
4 => {
119119
match (byte, safe_get(self.source, i)) {
120-
(0xF0, 0x90 ... 0xBF) => (),
121-
(0xF1 ... 0xF3, 0x80 ... 0xBF) => (),
122-
(0xF4, 0x80 ... 0x8F) => (),
120+
(0xF0, 0x90 ..= 0xBF) => (),
121+
(0xF1 ..= 0xF3, 0x80 ..= 0xBF) => (),
122+
(0xF4, 0x80 ..= 0x8F) => (),
123123
_ => {
124124
error!();
125125
}

src/libcore/str/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1484,10 +1484,10 @@ fn run_utf8_validation(v: &[u8]) -> Result<(), Utf8Error> {
14841484
},
14851485
3 => {
14861486
match (first, next!()) {
1487-
(0xE0 , 0xA0 ... 0xBF) |
1488-
(0xE1 ... 0xEC, 0x80 ... 0xBF) |
1489-
(0xED , 0x80 ... 0x9F) |
1490-
(0xEE ... 0xEF, 0x80 ... 0xBF) => {}
1487+
(0xE0 , 0xA0 ..= 0xBF) |
1488+
(0xE1 ..= 0xEC, 0x80 ..= 0xBF) |
1489+
(0xED , 0x80 ..= 0x9F) |
1490+
(0xEE ..= 0xEF, 0x80 ..= 0xBF) => {}
14911491
_ => err!(Some(1))
14921492
}
14931493
if next!() & !CONT_MASK != TAG_CONT_U8 {
@@ -1496,9 +1496,9 @@ fn run_utf8_validation(v: &[u8]) -> Result<(), Utf8Error> {
14961496
}
14971497
4 => {
14981498
match (first, next!()) {
1499-
(0xF0 , 0x90 ... 0xBF) |
1500-
(0xF1 ... 0xF3, 0x80 ... 0xBF) |
1501-
(0xF4 , 0x80 ... 0x8F) => {}
1499+
(0xF0 , 0x90 ..= 0xBF) |
1500+
(0xF1 ..= 0xF3, 0x80 ..= 0xBF) |
1501+
(0xF4 , 0x80 ..= 0x8F) => {}
15021502
_ => err!(Some(1))
15031503
}
15041504
if next!() & !CONT_MASK != TAG_CONT_U8 {

src/libcore/tests/slice.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ fn test_binary_search() {
6060
assert_eq!(b.binary_search(&0), Err(0));
6161
assert_eq!(b.binary_search(&1), Ok(0));
6262
assert_eq!(b.binary_search(&2), Err(1));
63-
assert!(match b.binary_search(&3) { Ok(1...3) => true, _ => false });
64-
assert!(match b.binary_search(&3) { Ok(1...3) => true, _ => false });
63+
assert!(match b.binary_search(&3) { Ok(1..=3) => true, _ => false });
64+
assert!(match b.binary_search(&3) { Ok(1..=3) => true, _ => false });
6565
assert_eq!(b.binary_search(&4), Err(4));
6666
assert_eq!(b.binary_search(&5), Err(4));
6767
assert_eq!(b.binary_search(&6), Err(4));

src/librustc/hir/lowering.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3356,7 +3356,7 @@ impl<'a> LoweringContext<'a> {
33563356
PatKind::Ref(ref inner, mutbl) => {
33573357
hir::PatKind::Ref(self.lower_pat(inner), self.lower_mutability(mutbl))
33583358
}
3359-
PatKind::Range(ref e1, ref e2, ref end) => hir::PatKind::Range(
3359+
PatKind::Range(ref e1, ref e2, Spanned { node: ref end, .. }) => hir::PatKind::Range(
33603360
P(self.lower_expr(e1)),
33613361
P(self.lower_expr(e2)),
33623362
self.lower_range_end(end),

src/librustc_apfloat/ieee.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1753,9 +1753,9 @@ impl<S: Semantics> IeeeFloat<S> {
17531753
} else {
17541754
loss = Some(match hex_value {
17551755
0 => Loss::ExactlyZero,
1756-
1...7 => Loss::LessThanHalf,
1756+
1..=7 => Loss::LessThanHalf,
17571757
8 => Loss::ExactlyHalf,
1758-
9...15 => Loss::MoreThanHalf,
1758+
9..=15 => Loss::MoreThanHalf,
17591759
_ => unreachable!(),
17601760
});
17611761
}

src/librustc_codegen_utils/symbol_names.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ pub fn sanitize(result: &mut String, s: &str) -> bool {
424424
'-' | ':' => result.push('.'),
425425

426426
// These are legal symbols
427-
'a'...'z' | 'A'...'Z' | '0'...'9' | '_' | '.' | '$' => result.push(c),
427+
'a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '.' | '$' => result.push(c),
428428

429429
_ => {
430430
result.push('$');

src/librustc_lint/builtin.rs

+38
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use std::collections::HashSet;
4343

4444
use syntax::ast;
4545
use syntax::attr;
46+
use syntax::codemap::Spanned;
4647
use syntax::edition::Edition;
4748
use syntax::feature_gate::{AttributeGate, AttributeType, Stability, deprecated_attributes};
4849
use syntax_pos::{BytePos, Span, SyntaxContext};
@@ -1669,6 +1670,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TrivialConstraints {
16691670
}
16701671
}
16711672

1673+
16721674
/// Does nothing as a lint pass, but registers some `Lint`s
16731675
/// which are used by other parts of the compiler.
16741676
#[derive(Copy, Clone)]
@@ -1701,3 +1703,39 @@ impl LintPass for SoftLints {
17011703
)
17021704
}
17031705
}
1706+
1707+
1708+
declare_lint! {
1709+
pub ELLIPSIS_INCLUSIVE_RANGE_PATTERNS,
1710+
Allow,
1711+
"`...` range patterns are deprecated"
1712+
}
1713+
1714+
1715+
pub struct EllipsisInclusiveRangePatterns;
1716+
1717+
impl LintPass for EllipsisInclusiveRangePatterns {
1718+
fn get_lints(&self) -> LintArray {
1719+
lint_array!(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS)
1720+
}
1721+
}
1722+
1723+
impl EarlyLintPass for EllipsisInclusiveRangePatterns {
1724+
fn check_pat(&mut self, cx: &EarlyContext, pat: &ast::Pat) {
1725+
use self::ast::{PatKind, RangeEnd, RangeSyntax};
1726+
1727+
if let PatKind::Range(
1728+
_, _, Spanned { span, node: RangeEnd::Included(RangeSyntax::DotDotDot) }
1729+
) = pat.node {
1730+
let msg = "`...` range patterns are deprecated";
1731+
let mut err = cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, span, msg);
1732+
err.span_suggestion_short_with_applicability(
1733+
span, "use `..=` for an inclusive range", "..=".to_owned(),
1734+
// FIXME: outstanding problem with precedence in ref patterns:
1735+
// https://github.com/rust-lang/rust/issues/51043#issuecomment-392252285
1736+
Applicability::MaybeIncorrect
1737+
);
1738+
err.emit()
1739+
}
1740+
}
1741+
}

src/librustc_lint/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
100100
AnonymousParameters,
101101
UnusedDocComment,
102102
BadRepr,
103+
EllipsisInclusiveRangePatterns,
103104
);
104105

105106
add_early_builtin_with_new!(sess,
@@ -176,7 +177,8 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
176177
"rust_2018_idioms",
177178
BARE_TRAIT_OBJECTS,
178179
UNREACHABLE_PUB,
179-
UNUSED_EXTERN_CRATES);
180+
UNUSED_EXTERN_CRATES,
181+
ELLIPSIS_INCLUSIVE_RANGE_PATTERNS);
180182

181183
// Guidelines for creating a future incompatibility lint:
182184
//

src/librustc_mir/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,9 @@ For example:
306306
```compile_fail
307307
match 5u32 {
308308
// This range is ok, albeit pointless.
309-
1 ... 1 => {}
309+
1 ..= 1 => {}
310310
// This range is empty, and the compiler can tell.
311-
1000 ... 5 => {}
311+
1000 ..= 5 => {}
312312
}
313313
```
314314
"##,

src/librustc_mir/hair/pattern/check_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ fn check_exhaustive<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
481481
let joined_patterns = match witnesses.len() {
482482
0 => bug!(),
483483
1 => format!("`{}`", witnesses[0]),
484-
2...LIMIT => {
484+
2..=LIMIT => {
485485
let (tail, head) = witnesses.split_last().unwrap();
486486
let head: Vec<_> = head.iter().map(|w| w.to_string()).collect();
487487
format!("`{}` and `{}`", head.join("`, `"), tail)

0 commit comments

Comments
 (0)