Skip to content

Commit 726ea01

Browse files
author
Filippo Costa
committed
Merge branch 'enum_attr_fix' of https://github.com/Luro02/codegen into Luro02-enum_attr_fix
2 parents 2907a76 + e19b365 commit 726ea01

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/type_def.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct TypeDef {
1313
vis: Option<String>,
1414
docs: Option<Docs>,
1515
derive: Vec<String>,
16-
allow: Option<String>,
16+
allow: Vec<String>,
1717
repr: Option<String>,
1818
bounds: Vec<Bound>,
1919
macros: Vec<String>,
@@ -27,7 +27,7 @@ impl TypeDef {
2727
vis: None,
2828
docs: None,
2929
derive: vec![],
30-
allow: None,
30+
allow: vec![],
3131
repr: None,
3232
bounds: vec![],
3333
macros: vec![],
@@ -61,7 +61,7 @@ impl TypeDef {
6161
}
6262

6363
pub fn allow(&mut self, allow: &str) {
64-
self.allow = Some(allow.to_string());
64+
self.allow.push(allow.to_string());
6565
}
6666

6767
pub fn repr(&mut self, repr: &str) {
@@ -107,8 +107,8 @@ impl TypeDef {
107107
Ok(())
108108
}
109109

110-
fn fmt_allow(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
111-
if let Some(ref allow) = self.allow {
110+
fn fmt_allow(&self, fmt: &mut Formatter) -> fmt::Result {
111+
for allow in &self.allow {
112112
write!(fmt, "#[allow({})]\n", allow)?;
113113
}
114114

tests/codegen.rs

+44
Original file line numberDiff line numberDiff line change
@@ -561,3 +561,47 @@ impl Foo for Bar {
561561

562562
assert_eq!(scope.to_string(), &expect[1..]);
563563
}
564+
565+
#[test]
566+
fn struct_with_multiple_allow() {
567+
let mut scope = Scope::new();
568+
569+
scope
570+
.new_struct("Foo")
571+
.allow("dead_code")
572+
.allow("clippy::all")
573+
.field("one", "u8")
574+
.field("two", "u8");
575+
576+
let expect = r#"
577+
#[allow(dead_code)]
578+
#[allow(clippy::all)]
579+
struct Foo {
580+
one: u8,
581+
two: u8,
582+
}"#;
583+
584+
assert_eq!(scope.to_string(), &expect[1..]);
585+
}
586+
587+
#[test]
588+
fn enum_with_multiple_allow() {
589+
let mut scope = Scope::new();
590+
591+
scope
592+
.new_enum("IpAddrKind")
593+
.allow("dead_code")
594+
.allow("clippy::all")
595+
.push_variant(Variant::new("V4"))
596+
.push_variant(Variant::new("V6"));
597+
598+
let expect = r#"
599+
#[allow(dead_code)]
600+
#[allow(clippy::all)]
601+
enum IpAddrKind {
602+
V4,
603+
V6,
604+
}"#;
605+
606+
assert_eq!(scope.to_string(), &expect[1..]);
607+
}

0 commit comments

Comments
 (0)