Skip to content

Commit 1a08b17

Browse files
committed
Use Span::from_inner and make changes to precision inner span clearer
1 parent 3330c7d commit 1a08b17

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

compiler/rustc_builtin_macros/src/format.rs

+14-16
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,9 @@ impl PositionalNamedArg {
6464
/// Determines what span to replace with the name of the named argument
6565
fn get_span_to_replace(&self, cx: &Context<'_, '_>) -> Option<Span> {
6666
if let Some(inner_span) = &self.inner_span_to_replace {
67-
return match self.ty {
68-
PositionalNamedArgType::Arg | PositionalNamedArgType::Width => Some(Span::new(
69-
cx.fmtsp.lo() + BytePos(inner_span.start.try_into().unwrap()),
70-
cx.fmtsp.lo() + BytePos(inner_span.end.try_into().unwrap()),
71-
self.positional_named_arg_span.ctxt(),
72-
self.positional_named_arg_span.parent(),
73-
)),
74-
PositionalNamedArgType::Precision => Some(Span::new(
75-
cx.fmtsp.lo() + BytePos(inner_span.start.try_into().unwrap()) + BytePos(1),
76-
cx.fmtsp.lo() + BytePos(inner_span.end.try_into().unwrap()),
77-
self.positional_named_arg_span.ctxt(),
78-
self.positional_named_arg_span.parent(),
79-
)),
80-
};
67+
return Some(
68+
cx.fmtsp.from_inner(InnerSpan { start: inner_span.start, end: inner_span.end }),
69+
);
8170
} else if self.ty == PositionalNamedArgType::Arg {
8271
// In the case of a named argument whose position is implicit, there will not be a span
8372
// to replace. Instead, we insert the name after the `{`, which is the first character
@@ -111,7 +100,7 @@ impl PositionalNamedArgsLint {
111100
format_argument_index: usize,
112101
ty: PositionalNamedArgType,
113102
cur_piece: usize,
114-
inner_span: Option<rustc_parse_format::InnerSpan>,
103+
mut inner_span_to_replace: Option<rustc_parse_format::InnerSpan>,
115104
names: &FxHashMap<Symbol, (usize, Span)>,
116105
) {
117106
let named_arg = names
@@ -120,10 +109,19 @@ impl PositionalNamedArgsLint {
120109
.map(|found| found.clone());
121110

122111
if let Some(named_arg) = named_arg {
112+
// In FormatSpec, `precision_span` starts at the leading `.`, which we want to keep in
113+
// the lint suggestion, so increment `start` by 1 when `PositionalArgumentType` is
114+
// `Precision`.
115+
if ty == PositionalNamedArgType::Precision {
116+
inner_span_to_replace = inner_span_to_replace.map(|mut is| {
117+
is.start += 1;
118+
is
119+
});
120+
}
123121
self.positional_named_args.push(PositionalNamedArg {
124122
ty,
125123
cur_piece,
126-
inner_span_to_replace: inner_span,
124+
inner_span_to_replace,
127125
replacement: named_arg.0.clone(),
128126
positional_named_arg_span: named_arg.1.1.clone(),
129127
});

0 commit comments

Comments
 (0)