return when captured argument is not a struct field

This commit is contained in:
Takayuki Maeda 2022-08-04 11:51:25 +09:00
parent a0a2ec3326
commit dcd70c0995
2 changed files with 20 additions and 20 deletions

View File

@ -1279,20 +1279,21 @@ pub fn expand_preparsed_format_args(
let captured_arg_span = let captured_arg_span =
fmt_span.from_inner(InnerSpan::new(err.span.start, err.span.end)); fmt_span.from_inner(InnerSpan::new(err.span.start, err.span.end));
let positional_args = args.iter().filter(|arg| !arg.named).collect::<Vec<_>>(); let positional_args = args.iter().filter(|arg| !arg.named).collect::<Vec<_>>();
let mut suggestions = vec![(captured_arg_span, positional_args.len().to_string())];
if let Ok(arg) = ecx.source_map().span_to_snippet(captured_arg_span) { if let Ok(arg) = ecx.source_map().span_to_snippet(captured_arg_span) {
let span = match positional_args.last() { let span = match positional_args.last() {
Some(arg) => arg.expr.span, Some(arg) => arg.expr.span,
None => fmt_sp, None => fmt_sp,
}; };
suggestions.push((span.shrink_to_hi(), format!(", {}", arg)))
}
e.multipart_suggestion_verbose( e.multipart_suggestion_verbose(
"consider using a positional formatting argument instead", "consider using a positional formatting argument instead",
suggestions, vec![
(captured_arg_span, positional_args.len().to_string()),
(span.shrink_to_hi(), format!(", {}", arg)),
],
Applicability::MachineApplicable, Applicability::MachineApplicable,
); );
} }
}
e.emit(); e.emit();
return DummyResult::raw_expr(sp, true); return DummyResult::raw_expr(sp, true);
} }

View File

@ -229,21 +229,17 @@ impl<'a> Iterator for Parser<'a> {
Some(String(self.string(pos + 1))) Some(String(self.string(pos + 1)))
} else { } else {
let arg = self.argument(lbrace_end); let arg = self.argument(lbrace_end);
match self.must_consume('}') { if let Some(rbrace_byte_idx) = self.must_consume('}') {
Some(rbrace_byte_idx) => {
let lbrace_inner_offset = self.to_span_index(pos); let lbrace_inner_offset = self.to_span_index(pos);
let rbrace_inner_offset = self.to_span_index(rbrace_byte_idx); let rbrace_inner_offset = self.to_span_index(rbrace_byte_idx);
if self.is_literal { if self.is_literal {
self.arg_places.push( self.arg_places.push(
lbrace_inner_offset lbrace_inner_offset.to(InnerOffset(rbrace_inner_offset.0 + 1)),
.to(InnerOffset(rbrace_inner_offset.0 + 1)),
); );
} }
} } else {
None => {
self.suggest_positional_arg_instead_of_captured_arg(arg); self.suggest_positional_arg_instead_of_captured_arg(arg);
} }
}
Some(NextArgument(arg)) Some(NextArgument(arg))
} }
} }
@ -767,6 +763,9 @@ impl<'a> Parser<'a> {
let byte_pos = self.to_span_index(end); let byte_pos = self.to_span_index(end);
let start = InnerOffset(byte_pos.0 + 1); let start = InnerOffset(byte_pos.0 + 1);
let field = self.argument(start); let field = self.argument(start);
if !self.consume('}') {
return;
}
if let ArgumentNamed(_) = arg.position { if let ArgumentNamed(_) = arg.position {
if let ArgumentNamed(_) = field.position { if let ArgumentNamed(_) = field.position {
self.errors.insert( self.errors.insert(