From 90d16e498499a84ba53a451e6e97847352f6a8a5 Mon Sep 17 00:00:00 2001 From: Juha Kukkonen Date: Fri, 15 Apr 2022 13:31:15 +0300 Subject: [PATCH] Fix hard error in doc-comment parsing (#86) * Fix hard error when if Ident cannot be found from ast Attribute with only one path segment. -> Now returns just false to skip the Attribute. --- utoipa-gen/src/doc_comment.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/utoipa-gen/src/doc_comment.rs b/utoipa-gen/src/doc_comment.rs index 21d81ea2..ac1c9ce2 100644 --- a/utoipa-gen/src/doc_comment.rs +++ b/utoipa-gen/src/doc_comment.rs @@ -1,5 +1,5 @@ use proc_macro2::{Ident, Span}; -use proc_macro_error::{abort_call_site, emit_warning, OptionExt, ResultExt}; +use proc_macro_error::{abort_call_site, emit_warning, ResultExt}; use syn::{Attribute, Lit, Meta}; const DOC_ATTRIBUTE_TYPE: &str = "doc"; @@ -18,14 +18,14 @@ impl CommentAttributes { } fn is_doc_attribute(attribute: &&Attribute) -> bool { - &*Self::get_attribute_ident(attribute).to_string() == DOC_ATTRIBUTE_TYPE + match Self::get_attribute_ident(attribute) { + Some(attribute) => attribute == DOC_ATTRIBUTE_TYPE, + None => false, + } } - fn get_attribute_ident(attribute: &Attribute) -> &Ident { - attribute - .path - .get_ident() - .expect_or_abort("Expected doc attribute with one path segment") + fn get_attribute_ident(attribute: &Attribute) -> Option<&Ident> { + attribute.path.get_ident() } fn as_string_vec<'a, I: Iterator>(attributes: I) -> Vec { @@ -38,7 +38,6 @@ impl CommentAttributes { fn parse_doc_comment(attribute: &Attribute) -> Option { let meta = attribute.parse_meta().unwrap_or_abort(); - // TODO find a correct span? match meta { Meta::NameValue(name_value) => { if let Lit::Str(doc_comment) = name_value.lit {