Skip to content

Commit

Permalink
Emit error when user writes incorrect format for display attribute (#…
Browse files Browse the repository at this point in the history
…127, #160)

Additionally:
- set Clippy's MSRV to 1.36.0
- refine Clippy options on CI
  • Loading branch information
magurotuna authored May 11, 2021
1 parent e707113 commit a5d0005
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: clippy
args: --all -- -D clippy::all -D warnings -A clippy::match_like_matches_macro
args: --all-features -- -D warnings

rustfmt:
name: rustfmt
Expand Down
1 change: 1 addition & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
msrv = "1.36.0"
64 changes: 37 additions & 27 deletions src/display.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt::Display, iter::FromIterator as _, str::FromStr as _};
use std::{fmt::Display, str::FromStr as _};

use proc_macro2::{Ident, Span, TokenStream};
use quote::{quote, quote_spanned};
Expand Down Expand Up @@ -115,7 +115,7 @@ fn trait_name_to_trait_bound(trait_name: &str) -> syn::TraitBound {
paren_token: None,
path: syn::Path {
leading_colon: Some(syn::Token![::](Span::call_site())),
segments: syn::punctuated::Punctuated::from_iter(path_segments_iterator),
segments: path_segments_iterator.collect(),
},
}
}
Expand Down Expand Up @@ -221,34 +221,44 @@ impl<'a, 'b> State<'a, 'b> {
attrs: &[syn::Attribute],
meta_key: &str,
) -> Result<Option<syn::Meta>> {
let mut iterator = attrs
.iter()
.filter_map(|attr| attr.parse_meta().ok())
.filter(|meta| {
let meta = match meta {
syn::Meta::List(meta) => meta,
_ => return false,
};

if !meta.path.is_ident(self.trait_attr) || meta.nested.is_empty() {
return false;
}
let mut metas = Vec::new();
for meta in attrs.iter().filter_map(|attr| attr.parse_meta().ok()) {
let meta_list = match &meta {
syn::Meta::List(meta) => meta,
_ => continue,
};

let meta = match &meta.nested[0] {
syn::NestedMeta::Meta(meta) => meta,
_ => return false,
};
if !meta_list.path.is_ident(self.trait_attr) {
continue;
}

let meta = match meta {
syn::Meta::NameValue(meta) => meta,
_ => return false,
};
use syn::{Meta, NestedMeta};
let meta_nv = match meta_list.nested.first() {
Some(NestedMeta::Meta(Meta::NameValue(meta_nv))) => meta_nv,
_ => {
// If the given attribute is not MetaNameValue, it most likely implies that the
// user is writing an incorrect format. For example:
// - `#[display()]`
// - `#[display("foo")]`
// - `#[display(foo)]`
return Err(Error::new(
meta.span(),
format!(
r#"The format for this attribute cannot be parsed. Correct format: `#[{}({} = "...")]`"#,
self.trait_attr, meta_key
),
));
}
};

meta.path.is_ident(meta_key)
});
if meta_nv.path.is_ident(meta_key) {
metas.push(meta);
}
}

let meta = iterator.next();
if iterator.next().is_none() {
let mut iter = metas.into_iter();
let meta = iter.next();
if iter.next().is_none() {
Ok(meta)
} else {
Err(Error::new(meta.span(), "Too many attributes specified"))
Expand Down Expand Up @@ -775,7 +785,7 @@ mod regex_maybe_placeholder_spec {
let fmt_string = "{}, {:?}, {{}}, {{{1:0$}}}";
let placeholders: Vec<_> = crate::parsing::all_placeholders(&fmt_string)
.into_iter()
.flat_map(|x| x)
.flatten()
.collect();
assert_eq!(placeholders, vec!["{}", "{:?}", "{1:0$}"]);
}
Expand Down
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,6 @@
//! [`Constructor`]: https://jeltef.github.io/derive_more/derive_more/constructor.html
//! [`IsVariant`]: https://jeltef.github.io/derive_more/derive_more/is_variant.html

// Suppress Clippy tips to use `matches!` macro, because minimal supported Rust version is 1.36.0.
// Remove this suppression once minimal supported Rust version is bumped up to 1.42.0 or above.
#![cfg_attr(nightly, allow(clippy::match_like_matches_macro))]
#![recursion_limit = "128"]

extern crate proc_macro;
Expand Down

0 comments on commit a5d0005

Please sign in to comment.