diff --git a/src/lib.rs b/src/lib.rs index a4ca8429..25a6dec7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -494,8 +494,7 @@ macro_rules! __impl_bitflags { f.write_str(" | ")?; } first = false; - f.write_str("0x")?; - $crate::_core::fmt::LowerHex::fmt(&extra_bits, f)?; + $crate::_core::write!(f, "{:#x}", extra_bits)?; } if first { f.write_str("(empty)")?; diff --git a/tests/compile-fail/trait/custom_impl.rs b/tests/compile-fail/trait/custom_impl.rs index 66875898..80be2f91 100644 --- a/tests/compile-fail/trait/custom_impl.rs +++ b/tests/compile-fail/trait/custom_impl.rs @@ -62,4 +62,4 @@ impl BitFlags for BootlegFlags { } } -fn main() { } +fn main() {} diff --git a/tests/compile-pass/impls/convert.rs b/tests/compile-pass/impls/convert.rs index 1f02982a..393ed8f8 100644 --- a/tests/compile-pass/impls/convert.rs +++ b/tests/compile-pass/impls/convert.rs @@ -12,6 +12,4 @@ impl From for Flags { } } -fn main() { - -} +fn main() {} diff --git a/tests/compile-pass/impls/fmt.rs b/tests/compile-pass/impls/fmt.rs new file mode 100644 index 00000000..567fd448 --- /dev/null +++ b/tests/compile-pass/impls/fmt.rs @@ -0,0 +1,14 @@ +use bitflags::bitflags; + +bitflags! { + struct Flags: u8 { + const TWO = 0x2; + } +} + +fn main() { + // bug #267 (https://github.com/bitflags/bitflags/issues/267) + let flags = unsafe { Flags::from_bits_unchecked(0b11) }; + assert_eq!(format!("{:?}", flags), "TWO | 0x1"); + assert_eq!(format!("{:#?}", flags), "TWO | 0x1"); +} diff --git a/tests/compile-pass/redefinition/stringify.rs b/tests/compile-pass/redefinition/macros.rs similarity index 52% rename from tests/compile-pass/redefinition/stringify.rs rename to tests/compile-pass/redefinition/macros.rs index b04f2f6a..a9835124 100644 --- a/tests/compile-pass/redefinition/stringify.rs +++ b/tests/compile-pass/redefinition/macros.rs @@ -7,6 +7,11 @@ macro_rules! stringify { ($($t:tt)*) => { "..." }; } +#[allow(unused_macros)] +macro_rules! write { + ($($t:tt)*) => { "..." }; +} + bitflags! { struct Test: u8 { const A = 1; @@ -14,6 +19,6 @@ bitflags! { } fn main() { - // Just make sure we don't call the redefined `stringify` macro - assert_eq!(format!("{:?}", Test::A), "A"); + // Just make sure we don't call the redefined `stringify` or `write` macro + assert_eq!(format!("{:?}", unsafe { Test::from_bits_unchecked(0b11) }), "A | 0x2"); }