Skip to content

Commit 31b490d

Browse files
committed
Add enum for find_width_map_from_snippet
This makes the relationship between the vec and the boolean clearer.
1 parent e6c02aa commit 31b490d

File tree

1 file changed

+18
-7
lines changed
  • compiler/rustc_parse_format/src

1 file changed

+18
-7
lines changed

compiler/rustc_parse_format/src/lib.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ impl InnerWidthMapping {
5757
}
5858
}
5959

60+
/// Whether the input string is a literal. If yes, it contains the inner width mappings.
61+
#[derive(Clone, PartialEq, Eq)]
62+
enum InputStringKind {
63+
NotALiteral,
64+
Literal { width_mappings: Vec<InnerWidthMapping> },
65+
}
66+
6067
/// The type of format string that we are parsing.
6168
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
6269
pub enum ParseMode {
@@ -307,7 +314,11 @@ impl<'a> Parser<'a> {
307314
append_newline: bool,
308315
mode: ParseMode,
309316
) -> Parser<'a> {
310-
let (width_map, is_literal) = find_width_map_from_snippet(s, snippet, style);
317+
let input_string_kind = find_width_map_from_snippet(s, snippet, style);
318+
let (width_map, is_literal) = match input_string_kind {
319+
InputStringKind::Literal { width_mappings } => (width_mappings, true),
320+
InputStringKind::NotALiteral => (Vec::new(), false),
321+
};
311322
Parser {
312323
mode,
313324
input: s,
@@ -848,14 +859,14 @@ fn find_width_map_from_snippet(
848859
input: &str,
849860
snippet: Option<string::String>,
850861
str_style: Option<usize>,
851-
) -> (Vec<InnerWidthMapping>, bool) {
862+
) -> InputStringKind {
852863
let snippet = match snippet {
853864
Some(ref s) if s.starts_with('"') || s.starts_with("r\"") || s.starts_with("r#") => s,
854-
_ => return (vec![], false),
865+
_ => return InputStringKind::NotALiteral,
855866
};
856867

857868
if str_style.is_some() {
858-
return (vec![], true);
869+
return InputStringKind::Literal { width_mappings: Vec::new() };
859870
}
860871

861872
// Strip quotes.
@@ -868,15 +879,15 @@ fn find_width_map_from_snippet(
868879
// Alternatively, we could just count the trailing newlines and only trim one from the input if they don't match up.
869880
let input_no_nl = input.trim_end_matches('\n');
870881
let Ok(unescaped) = unescape_string(snippet) else {
871-
return (vec![], false);
882+
return InputStringKind::NotALiteral;
872883
};
873884

874885
let unescaped_no_nl = unescaped.trim_end_matches('\n');
875886

876887
if unescaped_no_nl != input_no_nl {
877888
// The source string that we're pointing at isn't our input, so spans pointing at it will be incorrect.
878889
// This can for example happen with proc macros that respan generated literals.
879-
return (vec![], false);
890+
return InputStringKind::NotALiteral;
880891
}
881892

882893
let mut s = snippet.char_indices();
@@ -958,7 +969,7 @@ fn find_width_map_from_snippet(
958969
}
959970
}
960971

961-
(width_mappings, true)
972+
InputStringKind::Literal { width_mappings }
962973
}
963974

964975
fn unescape_string(string: &str) -> Result<string::String, unescape::EscapeError> {

0 commit comments

Comments
 (0)