Skip to content

Commit 2ddea30

Browse files
committed
extract suggest_slice_pat
1 parent c54c9ef commit 2ddea30

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

src/libsyntax_expand/mbe/macro_rules.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,30 @@ crate fn annotate_err_with_kind(
6363
};
6464
}
6565

66+
/// Instead of e.g. `vec![a, b, c]` in a pattern context, suggest `[a, b, c]`.
67+
fn suggest_slice_pat(e: &mut DiagnosticBuilder<'_>, site_span: Span, parser: &Parser<'_>) {
68+
let mut suggestion = None;
69+
if let Ok(code) = parser.sess.source_map().span_to_snippet(site_span) {
70+
if let Some(bang) = code.find('!') {
71+
suggestion = Some(code[bang + 1..].to_string());
72+
}
73+
}
74+
if let Some(suggestion) = suggestion {
75+
e.span_suggestion(
76+
site_span,
77+
"use a slice pattern here instead",
78+
suggestion,
79+
Applicability::MachineApplicable,
80+
);
81+
} else {
82+
e.span_label(site_span, "use a slice pattern here instead");
83+
}
84+
e.help(
85+
"for more information, see https://doc.rust-lang.org/edition-guide/\
86+
rust-2018/slice-patterns.html"
87+
);
88+
}
89+
6690
impl<'a> ParserAnyMacro<'a> {
6791
crate fn make(mut self: Box<ParserAnyMacro<'a>>, kind: AstFragmentKind) -> AstFragment {
6892
let ParserAnyMacro { site_span, macro_ident, ref mut parser, arm_span } = *self;
@@ -92,27 +116,7 @@ impl<'a> ParserAnyMacro<'a> {
92116
}
93117
match kind {
94118
AstFragmentKind::Pat if macro_ident.name == sym::vec => {
95-
let mut suggestion = None;
96-
if let Ok(code) = parser.sess.source_map().span_to_snippet(site_span) {
97-
if let Some(bang) = code.find('!') {
98-
suggestion = Some(code[bang + 1..].to_string());
99-
}
100-
}
101-
if let Some(suggestion) = suggestion {
102-
e.span_suggestion(
103-
site_span,
104-
"use a slice pattern here instead",
105-
suggestion,
106-
Applicability::MachineApplicable,
107-
);
108-
} else {
109-
e.span_label(
110-
site_span,
111-
"use a slice pattern here instead",
112-
);
113-
}
114-
e.help("for more information, see https://doc.rust-lang.org/edition-guide/\
115-
rust-2018/slice-patterns.html");
119+
suggest_slice_pat(&mut e, site_span, parser);
116120
}
117121
_ => annotate_err_with_kind(&mut e, kind, site_span),
118122
};

0 commit comments

Comments
 (0)