Avoid inheriting formatter flags in some Display impls

The previous implementation would produce wrong unintentional output
when formatting with alignment or padding, such as {:<15}.
This commit is contained in:
David Tolnay 2021-07-29 13:33:16 -07:00
parent 7b76ea0881
commit c59a4022cd
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
3 changed files with 5 additions and 7 deletions

View File

@ -87,12 +87,11 @@ pub(crate) fn report(error: impl StdError) -> impl Display {
impl<E: StdError> Display for Report<E> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.0, formatter)?;
write!(formatter, "{}", self.0)?;
let mut error: &dyn StdError = &self.0;
while let Some(cause) = error.source() {
formatter.write_str("\n\nCaused by:\n ")?;
Display::fmt(cause, formatter)?;
write!(formatter, "\n\nCaused by:\n {}", cause)?;
error = cause;
}

View File

@ -308,9 +308,8 @@ struct CxxName<'a>(&'a Pair);
impl<'a> Display for CxxName<'a> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
for namespace in &self.0.namespace {
Display::fmt(namespace, formatter)?;
formatter.write_str("::")?;
write!(formatter, "{}::", namespace)?;
}
Display::fmt(&self.0.cxx, formatter)
write!(formatter, "{}", self.0.cxx)
}
}

View File

@ -208,7 +208,7 @@ impl Display for Discriminant {
if self.sign == Sign::Negative {
f.write_str("-")?;
}
Display::fmt(&self.magnitude, f)
write!(f, "{}", self.magnitude)
}
}