Skip to content

Commit

Permalink
Some fixes and tests improvement [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
tyranron committed Jul 19, 2024
1 parent 256c041 commit 5d73864
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Latest Version](https://img.shields.io/crates/v/derive_more.svg)](https://crates.io/crates/derive_more)
[![Rust Documentation](https://docs.rs/derive_more/badge.svg)](https://docs.rs/derive_more)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/JelteF/derive_more/master/LICENSE)
[![Rust 1.70+](https://img.shields.io/badge/rustc-1.70+-lightgray.svg)](https://blog.rust-lang.org/2022/11/03/Rust-1.70.0.html)
[![Rust 1.70+](https://img.shields.io/badge/rustc-1.70+-lightgray.svg)](https://blog.rust-lang.org/2023/06/01/Rust-1.70.0.html)
[![Unsafe Forbidden](https://img.shields.io/badge/unsafe-forbidden-success.svg)](https://github.com/rust-secure-code/safety-dance)

Rust has lots of builtin traits that are implemented for its basic types, such
Expand Down
2 changes: 0 additions & 2 deletions clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# See full lints list at:
# https://rust-lang.github.io/rust-clippy/master/index.html

msrv = "1.70.0"

# Ensures consistent bracing for macro calls in the codebase.
# Extends default settings:
# https://github.com/rust-lang/rust-clippy/blob/master/clippy_lints/src/nonstandard_macro_braces.rs#L143-L184
Expand Down
21 changes: 13 additions & 8 deletions impl/src/fmt/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ impl<'a> Expansion<'a> {
/// greater than 1.
///
/// [`Display::fmt()`]: fmt::Display::fmt()
/// [`FmtAttribute`]: super::FmtAttribute
fn generate_body(&self) -> syn::Result<TokenStream> {
if self.shared_format.is_none() {
return self.generate_body_impl();
Expand All @@ -234,7 +233,8 @@ impl<'a> Expansion<'a> {
if !shared_format.args.is_empty() {
return Err(syn::Error::new(
shared_format.args.span(),
"shared format string does not support positional placeholders, use named placeholders instead",
"shared format string does not support positional placeholders, use named \
placeholders instead",
));
}
let mut tokens = TokenStream::new();
Expand All @@ -243,12 +243,15 @@ impl<'a> Expansion<'a> {
let fmt_string = shared_format.lit.value();
let maybe_format_string = parsing::format_string(&fmt_string);
let Some(format_string) = maybe_format_string else {
// If we could not parse the format string, we just use the original string so
// we get a nice error message. We also panic as a safety precaution in case our
// parsing fails to parse something that write! allows.
// If we could not parse the format string, we just use the original string, so we get
// a nice error message. We also panic as a safety precaution in case our parsing fails
// to parse something that `write!()` allows.
return Ok(quote! {
derive_more::core::write!(__derive_more_f, #shared_format);
unreachable!("derive_more could not parse shared format string, but rust could: {:?}", #fmt_string);
unreachable!(
"`derive_more` could not parse shared format string, but Rust could: {:?}",
#fmt_string,
);
});
};
for part in format_string.elements {
Expand All @@ -261,7 +264,8 @@ impl<'a> Expansion<'a> {
if format.spec.is_some() {
return Err(syn::Error::new(
shared_format.span(),
"shared format _variant placeholder cannot contain format specifiers",
"shared format `_variant` placeholder cannot contain format \
specifiers",
));
}
if !current_format.is_empty() {
Expand All @@ -279,7 +283,8 @@ impl<'a> Expansion<'a> {
{
return Err(syn::Error::new(
shared_format.span(),
"shared format string cannot contain positional placeholders, use named placeholders instead",
"shared format string cannot contain positional placeholders, use \
named placeholders instead",
));
}
current_format.push_str(raw);
Expand Down
12 changes: 6 additions & 6 deletions tests/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,7 @@ mod enums {

mod shared_format {
use super::*;

mod single {
use super::*;

Expand All @@ -1299,16 +1300,16 @@ mod enums {
#[test]
fn assert() {
assert_eq!(Enum::A(1).to_string(), "Variant: A 1");
assert_eq!(Enum::B { field: 2 }.to_string(), "Variant: B 2",);
assert_eq!(Enum::C.to_string(), "Variant: C",);
assert_eq!(Enum::B { field: 2 }.to_string(), "Variant: B 2");
assert_eq!(Enum::C.to_string(), "Variant: C");
}
}

mod multiple {
use super::*;

#[derive(Display)]
#[display("{_variant} Variant: {_variant} {_variant}")]
#[display("{} Variant: {} {}", _variant)]
enum Enum {
#[display("A {_0}")]
A(i32),
Expand All @@ -1326,15 +1327,14 @@ mod enums {
Enum::B { field: 2 }.to_string(),
"B 2 Variant: B 2 B 2",
);
assert_eq!(Enum::C.to_string(), "C Variant: C C",);
assert_eq!(Enum::C.to_string(), "C Variant: C C");
}
}

mod none {
use super::*;

/// Make sure that variant specific bounds are not added if _variant is
/// not used.
/// Make sure that variant-specific bounds are not added if `_variant` is not used.
struct NoDisplay;

#[derive(Display)]
Expand Down

0 comments on commit 5d73864

Please sign in to comment.