Skip to content

format_foreign.rs: unwrap return Option value for fn position, as it always returns Some #118409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions compiler/rustc_builtin_macros/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,30 +672,22 @@ fn report_missing_placeholders(
if explained.contains(&sub) {
continue;
}
explained.insert(sub.clone());
explained.insert(sub);

if !found_foreign {
found_foreign = true;
show_doc_note = true;
}

if let Some(inner_sp) = pos {
let sp = fmt_span.from_inner(inner_sp);
let sp = fmt_span.from_inner(pos);

if success {
suggestions.push((sp, trn));
} else {
diag.span_note(
sp,
format!("format specifiers use curly braces, and {}", trn),
);
}
if success {
suggestions.push((sp, trn));
} else {
if success {
diag.help(format!("`{}` should be written as `{}`", sub, trn));
} else {
diag.note(format!("`{}` should use curly braces, and {}", sub, trn));
}
diag.span_note(
sp,
format!("format specifiers use curly braces, and {}", trn),
);
}
Comment on lines 686 to 699
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually dead code?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, looks to be. I wonder if it was ever tested?

}

Expand Down
24 changes: 11 additions & 13 deletions compiler/rustc_builtin_macros/src/format_foreign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ pub(crate) mod printf {
}
}

pub fn position(&self) -> Option<InnerSpan> {
pub fn position(&self) -> InnerSpan {
match self {
Substitution::Format(fmt) => Some(fmt.position),
&Substitution::Escape((start, end)) => Some(InnerSpan::new(start, end)),
Substitution::Format(fmt) => fmt.position,
&Substitution::Escape((start, end)) => InnerSpan::new(start, end),
}
}

Expand Down Expand Up @@ -302,10 +302,9 @@ pub(crate) mod printf {
fn next(&mut self) -> Option<Self::Item> {
let (mut sub, tail) = parse_next_substitution(self.s)?;
self.s = tail;
if let Some(InnerSpan { start, end }) = sub.position() {
sub.set_position(start + self.pos, end + self.pos);
self.pos += end;
}
let InnerSpan { start, end } = sub.position();
sub.set_position(start + self.pos, end + self.pos);
self.pos += end;
Some(sub)
}

Expand Down Expand Up @@ -629,9 +628,9 @@ pub mod shell {
}
}

pub fn position(&self) -> Option<InnerSpan> {
pub fn position(&self) -> InnerSpan {
let (Self::Ordinal(_, pos) | Self::Name(_, pos) | Self::Escape(pos)) = self;
Some(InnerSpan::new(pos.0, pos.1))
InnerSpan::new(pos.0, pos.1)
}

pub fn set_position(&mut self, start: usize, end: usize) {
Expand Down Expand Up @@ -664,10 +663,9 @@ pub mod shell {
fn next(&mut self) -> Option<Self::Item> {
let (mut sub, tail) = parse_next_substitution(self.s)?;
self.s = tail;
if let Some(InnerSpan { start, end }) = sub.position() {
sub.set_position(start + self.pos, end + self.pos);
self.pos += end;
}
let InnerSpan { start, end } = sub.position();
sub.set_position(start + self.pos, end + self.pos);
self.pos += end;
Some(sub)
}

Expand Down