@@ -63,6 +63,30 @@ crate fn annotate_err_with_kind(
63
63
} ;
64
64
}
65
65
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
+
66
90
impl < ' a > ParserAnyMacro < ' a > {
67
91
crate fn make ( mut self : Box < ParserAnyMacro < ' a > > , kind : AstFragmentKind ) -> AstFragment {
68
92
let ParserAnyMacro { site_span, macro_ident, ref mut parser, arm_span } = * self ;
@@ -92,27 +116,7 @@ impl<'a> ParserAnyMacro<'a> {
92
116
}
93
117
match kind {
94
118
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) ;
116
120
}
117
121
_ => annotate_err_with_kind( & mut e, kind, site_span) ,
118
122
} ;
0 commit comments