diff options
| author | bors <bors@rust-lang.org> | 2022-02-01 07:04:17 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-02-01 07:04:17 +0000 |
| commit | 93e8201ca75ebadc341c20c92ad24a4122c83991 (patch) | |
| tree | f1c0bbe3af9e7e5318afb05de365ec68d8826f08 /compiler/rustc_parse/src/parser/expr.rs | |
| parent | 25862ffc8d360b34dd8ec82a2f01750aaab976b7 (diff) | |
| parent | 81900f469239738d7166ad2022f60182817d57ca (diff) | |
| download | rust-93e8201ca75ebadc341c20c92ad24a4122c83991.tar.gz rust-93e8201ca75ebadc341c20c92ad24a4122c83991.zip | |
Auto merge of #93534 - ehuss:rollup-9ecozo9, r=ehuss
Rollup of 9 pull requests Successful merges: - #91343 (Fix suggestion to slice if scrutinee is a `Result` or `Option`) - #93019 (If an integer is entered with an upper-case base prefix (0Xbeef, 0O755, 0B1010), suggest to make it lowercase) - #93090 (`impl Display for io::ErrorKind`) - #93456 (Remove an unnecessary transmute from opaque::Encoder) - #93492 (Hide failed command unless in verbose mode) - #93504 (kmc-solid: Increase the default stack size) - #93513 (Allow any pretty printed line to have at least 60 chars) - #93532 (Update books) - #93533 (Update cargo) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_parse/src/parser/expr.rs')
| -rw-r--r-- | compiler/rustc_parse/src/parser/expr.rs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 693dd0051da..0115d498a7f 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -1700,6 +1700,19 @@ impl<'a> Parser<'a> { s.len() > 1 && s.starts_with(first_chars) && s[1..].chars().all(|c| c.is_ascii_digit()) } + // Try to lowercase the prefix if it's a valid base prefix. + fn fix_base_capitalisation(s: &str) -> Option<String> { + if let Some(stripped) = s.strip_prefix("B") { + Some(format!("0b{stripped}")) + } else if let Some(stripped) = s.strip_prefix("O") { + Some(format!("0o{stripped}")) + } else if let Some(stripped) = s.strip_prefix("X") { + Some(format!("0x{stripped}")) + } else { + None + } + } + let token::Lit { kind, suffix, .. } = lit; match err { // `NotLiteral` is not an error by itself, so we don't report @@ -1724,6 +1737,18 @@ impl<'a> Parser<'a> { self.struct_span_err(span, &msg) .help("valid widths are 8, 16, 32, 64 and 128") .emit(); + } else if let Some(fixed) = fix_base_capitalisation(suf) { + let msg = "invalid base prefix for number literal"; + + self.struct_span_err(span, &msg) + .note("base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase") + .span_suggestion( + span, + "try making the prefix lowercase", + fixed, + Applicability::MaybeIncorrect, + ) + .emit(); } else { let msg = format!("invalid suffix `{}` for number literal", suf); self.struct_span_err(span, &msg) |
