diff --git a/src/block.rs b/src/block.rs index 6bb321e..25d5bbe 100644 --- a/src/block.rs +++ b/src/block.rs @@ -14,7 +14,7 @@ pub struct Block { impl Block { /// Returns an empty code block. pub fn new(before: &str) -> Self { - Block { + Self { before: Some(before.to_string()), after: None, body: vec![], @@ -31,7 +31,7 @@ impl Block { } /// Push a nested block to this block. - pub fn push_block(&mut self, block: Block) -> &mut Self { + pub fn push_block(&mut self, block: Self) -> &mut Self { self.body.push(Body::Block(block)); self } @@ -54,7 +54,7 @@ impl Block { write!(fmt, " ")?; } - write!(fmt, "{{\n")?; + writeln!(fmt, "{{")?; fmt.indent(|fmt| { for b in &self.body { @@ -70,7 +70,7 @@ impl Block { write!(fmt, "{}", after)?; } - write!(fmt, "\n")?; + writeln!(fmt)?; Ok(()) } } diff --git a/src/body.rs b/src/body.rs index 4c6efbe..80135f9 100644 --- a/src/body.rs +++ b/src/body.rs @@ -12,8 +12,8 @@ pub enum Body { impl Body { pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { match &self { - Body::String(s) => write!(fmt, "{}\n", s), - Body::Block(b) => b.fmt(fmt), + Self::String(s) => write!(fmt, "{}\n", s), + Self::Block(b) => b.fmt(fmt), } } } diff --git a/src/docs.rs b/src/docs.rs index d03dc3a..815f244 100644 --- a/src/docs.rs +++ b/src/docs.rs @@ -9,14 +9,14 @@ pub struct Docs { impl Docs { pub fn new(docs: &str) -> Self { - Docs { + Self { docs: docs.to_string(), } } pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { for line in self.docs.lines() { - write!(fmt, "/// {}\n", line)?; + writeln!(fmt, "/// {}", line)?; } Ok(()) diff --git a/src/enum.rs b/src/enum.rs index 2636f62..01da7bb 100644 --- a/src/enum.rs +++ b/src/enum.rs @@ -16,14 +16,14 @@ pub struct Enum { impl Enum { /// Return a enum definition with the provided name. pub fn new(name: &str) -> Self { - Enum { + Self { type_def: TypeDef::new(name), variants: vec![], } } /// Returns a reference to the type. - pub fn ty(&self) -> &Type { + pub const fn ty(&self) -> &Type { &self.type_def.ty } diff --git a/src/field.rs b/src/field.rs index 769158f..8f61a89 100644 --- a/src/field.rs +++ b/src/field.rs @@ -22,7 +22,7 @@ impl Field { where T: Into, { - Field { + Self { name: name.into(), ty: ty.into(), documentation: Vec::new(), diff --git a/src/fields.rs b/src/fields.rs index 6878818..976c4b0 100644 --- a/src/fields.rs +++ b/src/fields.rs @@ -16,10 +16,10 @@ pub enum Fields { impl Fields { pub fn push_named(&mut self, field: Field) -> &mut Self { match *self { - Fields::Empty => { - *self = Fields::Named(vec![field]); + Self::Empty => { + *self = Self::Named(vec![field]); } - Fields::Named(ref mut fields) => { + Self::Named(fields) => { fields.push(field); } _ => panic!("field list is named"), @@ -44,11 +44,11 @@ impl Fields { where T: Into, { - match *self { - Fields::Empty => { - *self = Fields::Tuple(vec![ty.into()]); + match self { + Self::Empty => { + *self = Self::Tuple(vec![ty.into()]); } - Fields::Tuple(ref mut fields) => { + Self::Tuple(fields) => { fields.push(ty.into()); } _ => panic!("field list is tuple"), @@ -59,30 +59,30 @@ impl Fields { pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { match *self { - Fields::Named(ref fields) => { + Self::Named(ref fields) => { assert!(!fields.is_empty()); fmt.block(|fmt| { for f in fields { if !f.documentation.is_empty() { for doc in &f.documentation { - write!(fmt, "/// {}\n", doc)?; + writeln!(fmt, "/// {}", doc)?; } } if !f.annotation.is_empty() { for ann in &f.annotation { - write!(fmt, "{}\n", ann)?; + writeln!(fmt, "{}", ann)?; } } write!(fmt, "{}: ", f.name)?; f.ty.fmt(fmt)?; - write!(fmt, ",\n")?; + writeln!(fmt, ",")?; } Ok(()) })?; } - Fields::Tuple(ref tys) => { + Self::Tuple(tys) => { assert!(!tys.is_empty()); write!(fmt, "(")?; @@ -96,7 +96,7 @@ impl Fields { write!(fmt, ")")?; } - Fields::Empty => {} + Self::Empty => {} } Ok(()) diff --git a/src/formatter.rs b/src/formatter.rs index 447a6f6..a73bbba 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -22,7 +22,7 @@ pub struct Formatter<'a> { impl<'a> Formatter<'a> { /// Return a new formatter that writes to the given string. pub fn new(dst: &'a mut String) -> Self { - Formatter { + Self { dst, spaces: 0, indent: DEFAULT_INDENT, @@ -38,9 +38,9 @@ impl<'a> Formatter<'a> { write!(self, " ")?; } - write!(self, "{{\n")?; + writeln!(self, "{{")?; self.indent(f)?; - write!(self, "}}\n")?; + writeln!(self, "}}")?; Ok(()) } @@ -57,7 +57,7 @@ impl<'a> Formatter<'a> { /// Check if current destination is the start of a new line. pub fn is_start_of_line(&self) -> bool { - self.dst.is_empty() || self.dst.as_bytes().last() == Some(&b'\n') + self.dst.is_empty() || self.dst.ends_with('\n') } fn push_spaces(&mut self) { @@ -120,17 +120,17 @@ pub fn fmt_generics(generics: &[String], fmt: &mut Formatter<'_>) -> fmt::Result /// Format generic bounds. pub fn fmt_bounds(bounds: &[Bound], fmt: &mut Formatter<'_>) -> fmt::Result { if !bounds.is_empty() { - write!(fmt, "\n")?; + writeln!(fmt)?; // Write first bound write!(fmt, "where {}: ", bounds[0].name)?; fmt_bound_rhs(&bounds[0].bound, fmt)?; - write!(fmt, ",\n")?; + writeln!(fmt, ",")?; for bound in &bounds[1..] { write!(fmt, " {}: ", bound.name)?; fmt_bound_rhs(&bound.bound, fmt)?; - write!(fmt, ",\n")?; + writeln!(fmt, ",")?; } } diff --git a/src/function.rs b/src/function.rs index 827ec6c..1cb4c4a 100644 --- a/src/function.rs +++ b/src/function.rs @@ -56,7 +56,7 @@ pub struct Function { impl Function { /// Return a new function definition. pub fn new(name: &str) -> Self { - Function { + Self { name: name.to_string(), docs: None, allow: None, @@ -214,12 +214,12 @@ impl Function { docs.fmt(fmt)?; } - if let Some(ref allow) = self.allow { - write!(fmt, "#[allow({})]\n", allow)?; + if let Some(allow) = &self.allow { + writeln!(fmt, "#[allow({})]", allow)?; } for attr in self.attributes.iter() { - write!(fmt, "#[{}]\n", attr)?; + writeln!(fmt, "#[{}]", attr)?; } if is_trait { @@ -229,11 +229,11 @@ impl Function { ); } - if let Some(ref vis) = self.vis { + if let Some(vis) = &self.vis { write!(fmt, "{} ", vis)?; } - if let Some(ref extern_abi) = self.extern_abi { + if let Some(extern_abi) = &self.extern_abi { write!(fmt, "extern \"{extern_abi}\" ", extern_abi = extern_abi)?; } @@ -246,7 +246,7 @@ impl Function { write!(fmt, "(")?; - if let Some(ref s) = self.arg_self { + if let Some(s) = &self.arg_self { write!(fmt, "{}", s)?; } @@ -261,15 +261,15 @@ impl Function { write!(fmt, ")")?; - if let Some(ref ret) = self.ret { + if let Some(ret) = &self.ret { write!(fmt, " -> ")?; ret.fmt(fmt)?; } fmt_bounds(&self.bounds, fmt)?; - match self.body { - Some(ref body) => fmt.block(|fmt| { + match &self.body { + Some(body) => fmt.block(|fmt| { for b in body { b.fmt(fmt)?; } @@ -281,7 +281,7 @@ impl Function { panic!("impl blocks must define fn bodies"); } - write!(fmt, ";\n") + writeln!(fmt, ";") } } } diff --git a/src/impl.rs b/src/impl.rs index 6e411b5..4157ff9 100644 --- a/src/impl.rs +++ b/src/impl.rs @@ -36,7 +36,7 @@ impl Impl { where T: Into, { - Impl { + Self { target: target.into(), generics: vec![], impl_trait: None, @@ -121,7 +121,7 @@ impl Impl { /// Formats the impl block using the given formatter. pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { for m in self.macros.iter() { - write!(fmt, "{}\n", m)?; + writeln!(fmt, "{}", m)?; } write!(fmt, "impl")?; fmt_generics(&self.generics[..], fmt)?; @@ -143,13 +143,13 @@ impl Impl { for ty in &self.assoc_tys { write!(fmt, "type {} = ", ty.name)?; ty.ty.fmt(fmt)?; - write!(fmt, ";\n")?; + writeln!(fmt, ";")?; } } for (i, func) in self.fns.iter().enumerate() { if i != 0 || !self.assoc_tys.is_empty() { - write!(fmt, "\n")?; + writeln!(fmt)?; } func.fmt(false, fmt)?; diff --git a/src/import.rs b/src/import.rs index 6d44bd0..1d943b3 100644 --- a/src/import.rs +++ b/src/import.rs @@ -10,7 +10,7 @@ pub struct Import { impl Import { /// Return a new import. pub fn new(path: &str, ty: &str) -> Self { - Import { + Self { line: format!("{}::{}", path, ty), vis: None, } diff --git a/src/lib.rs b/src/lib.rs index 70b8675..7be6aa2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ #![deny(missing_debug_implementations, missing_docs)] #![doc(html_root_url = "https://docs.rs/codegen/0.1.1")] -#![warn(rust_2018_idioms)] +#![warn(rust_2018_idioms, clippy::nursery)] //! Provides a builder API for generating Rust code. //! @@ -47,7 +47,6 @@ mod r#struct; mod r#trait; mod r#type; - pub use associated_type::*; pub use block::*; pub use field::*; diff --git a/src/module.rs b/src/module.rs index 4846a61..5c96388 100644 --- a/src/module.rs +++ b/src/module.rs @@ -29,7 +29,7 @@ pub struct Module { impl Module { /// Return a new, blank module pub fn new(name: &str) -> Self { - Module { + Self { name: name.to_string(), vis: None, docs: None, @@ -69,12 +69,12 @@ impl Module { /// will return the existing definition instead. /// /// [`get_or_new_module`]: #method.get_or_new_module - pub fn new_module(&mut self, name: &str) -> &mut Module { + pub fn new_module(&mut self, name: &str) -> &mut Self { self.scope.new_module(name) } /// Returns a reference to a module if it is exists in this scope. - pub fn get_module(&self, name: &Q) -> Option<&Module> + pub fn get_module(&self, name: &Q) -> Option<&Self> where String: PartialEq, { @@ -82,7 +82,7 @@ impl Module { } /// Returns a mutable reference to a module if it is exists in this scope. - pub fn get_module_mut(&mut self, name: &Q) -> Option<&mut Module> + pub fn get_module_mut(&mut self, name: &Q) -> Option<&mut Self> where String: PartialEq, { @@ -91,7 +91,7 @@ impl Module { /// Returns a mutable reference to a module, creating it if it does /// not exist. - pub fn get_or_new_module(&mut self, name: &str) -> &mut Module { + pub fn get_or_new_module(&mut self, name: &str) -> &mut Self { self.scope.get_or_new_module(name) } @@ -107,7 +107,7 @@ impl Module { /// return the existing definition instead. /// /// [`get_or_new_module`]: #method.get_or_new_module - pub fn push_module(&mut self, item: Module) -> &mut Self { + pub fn push_module(&mut self, item: Self) -> &mut Self { self.scope.push_module(item); self } diff --git a/src/scope.rs b/src/scope.rs index 1312139..c5a36ea 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -32,7 +32,7 @@ pub struct Scope { impl Scope { /// Returns a new scope pub fn new() -> Self { - Scope { + Self { docs: None, imports: IndexMap::new(), items: vec![], @@ -69,8 +69,8 @@ impl Scope { pub fn new_module(&mut self, name: &str) -> &mut Module { self.push_module(Module::new(name)); - match *self.items.last_mut().unwrap() { - Item::Module(ref mut v) => v, + match self.items.last_mut().unwrap() { + Item::Module(v) => v, _ => unreachable!(), } } @@ -83,7 +83,7 @@ impl Scope { self.items .iter_mut() .filter_map(|item| match item { - &mut Item::Module(ref mut module) if module.name == *name => Some(module), + Item::Module(ref mut module) if module.name == *name => Some(module), _ => None, }) .next() @@ -97,7 +97,7 @@ impl Scope { self.items .iter() .filter_map(|item| match item { - &Item::Module(ref module) if module.name == *name => Some(module), + Item::Module(module) if module.name == *name => Some(module), _ => None, }) .next() @@ -135,8 +135,8 @@ impl Scope { pub fn new_struct(&mut self, name: &str) -> &mut Struct { self.push_struct(Struct::new(name)); - match *self.items.last_mut().unwrap() { - Item::Struct(ref mut v) => v, + match self.items.last_mut().unwrap() { + Item::Struct(v) => v, _ => unreachable!(), } } @@ -238,23 +238,23 @@ impl Scope { self.fmt_imports(fmt)?; if !self.imports.is_empty() { - write!(fmt, "\n")?; + writeln!(fmt)?; } for (i, item) in self.items.iter().enumerate() { if i != 0 { - write!(fmt, "\n")?; + writeln!(fmt)?; } - match *item { - Item::Module(ref v) => v.fmt(fmt)?, - Item::Struct(ref v) => v.fmt(fmt)?, - Item::Function(ref v) => v.fmt(false, fmt)?, - Item::Trait(ref v) => v.fmt(fmt)?, - Item::Enum(ref v) => v.fmt(fmt)?, - Item::Impl(ref v) => v.fmt(fmt)?, - Item::Raw(ref v) => { - write!(fmt, "{}\n", v)?; + match &item { + Item::Module(v) => v.fmt(fmt)?, + Item::Struct(v) => v.fmt(fmt)?, + Item::Function(v) => v.fmt(false, fmt)?, + Item::Trait(v) => v.fmt(fmt)?, + Item::Enum(v) => v.fmt(fmt)?, + Item::Impl(v) => v.fmt(fmt)?, + Item::Raw(v) => { + writeln!(fmt, "{}", v)?; } } } @@ -288,7 +288,7 @@ impl Scope { } if !tys.is_empty() { - if let Some(ref vis) = *vis { + if let Some(vis) = &vis { write!(fmt, "{} ", vis)?; } @@ -304,9 +304,9 @@ impl Scope { write!(fmt, "{}", ty)?; } - write!(fmt, "}};\n")?; + writeln!(fmt, "}};")?; } else if tys.len() == 1 { - write!(fmt, "{};\n", tys[0])?; + writeln!(fmt, "{};", tys[0])?; } } } diff --git a/src/struct.rs b/src/struct.rs index 8feae8a..9d1ca1e 100644 --- a/src/struct.rs +++ b/src/struct.rs @@ -19,14 +19,14 @@ pub struct Struct { impl Struct { /// Return a structure definition with the provided name pub fn new(name: &str) -> Self { - Struct { + Self { type_def: TypeDef::new(name), fields: Fields::Empty, } } /// Returns a reference to the type - pub fn ty(&self) -> &Type { + pub const fn ty(&self) -> &Type { &self.type_def.ty } @@ -115,10 +115,10 @@ impl Struct { match self.fields { Fields::Empty => { - write!(fmt, ";\n")?; + writeln!(fmt, ";")?; } Fields::Tuple(..) => { - write!(fmt, ";\n")?; + writeln!(fmt, ";")?; } _ => {} } diff --git a/src/trait.rs b/src/trait.rs index 7a8c069..58b3363 100644 --- a/src/trait.rs +++ b/src/trait.rs @@ -21,7 +21,7 @@ pub struct Trait { impl Trait { /// Return a trait definition with the provided name pub fn new(name: &str) -> Self { - Trait { + Self { type_def: TypeDef::new(name), parents: vec![], associated_tys: vec![], @@ -31,7 +31,7 @@ impl Trait { } /// Returns a reference to the type - pub fn ty(&self) -> &Type { + pub const fn ty(&self) -> &Type { &self.type_def.ty } @@ -122,13 +122,13 @@ impl Trait { fmt_bound_rhs(&ty.bound, fmt)?; } - write!(fmt, ";\n")?; + writeln!(fmt, ";")?; } } for (i, func) in self.fns.iter().enumerate() { if i != 0 || !assoc.is_empty() { - write!(fmt, "\n")?; + writeln!(fmt)?; } func.fmt(true, fmt)?; diff --git a/src/type.rs b/src/type.rs index 4a46eca..d0a15a2 100644 --- a/src/type.rs +++ b/src/type.rs @@ -12,7 +12,7 @@ pub struct Type { impl Type { /// Return a new type with the given name. pub fn new(name: &str) -> Self { - Type { + Self { name: name.to_string(), generics: vec![], } @@ -21,11 +21,11 @@ impl Type { /// Add a generic to the type. pub fn generic(&mut self, ty: T) -> &mut Self where - T: Into, + T: Into, { // Make sure that the name doesn't already include generics assert!( - !self.name.contains("<"), + !self.name.contains('<'), "type name already includes generics" ); @@ -36,7 +36,7 @@ impl Type { /// Rewrite the `Type` with the provided path /// /// TODO: Is this needed? - pub fn path(&self, path: &str) -> Type { + pub fn path(&self, path: &str) -> Self { // TODO: This isn't really correct assert!(!self.name.contains("::")); @@ -44,7 +44,7 @@ impl Type { name.push_str("::"); name.push_str(&self.name); - Type { + Self { name, generics: self.generics.clone(), } @@ -53,10 +53,10 @@ impl Type { /// Formats the struct using the given formatter. pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { write!(fmt, "{}", self.name)?; - Type::fmt_slice(&self.generics, fmt) + Self::fmt_slice(&self.generics, fmt) } - fn fmt_slice(generics: &[Type], fmt: &mut Formatter<'_>) -> fmt::Result { + fn fmt_slice(generics: &[Self], fmt: &mut Formatter<'_>) -> fmt::Result { if !generics.is_empty() { write!(fmt, "<")?; @@ -74,29 +74,29 @@ impl Type { } } -impl<'a> From<&'a str> for Type { - fn from(src: &'a str) -> Self { - Type::new(src) +impl From<&str> for Type { + fn from(src: &str) -> Self { + Self::new(src) } } impl From for Type { fn from(src: String) -> Self { - Type { + Self { name: src, generics: vec![], } } } -impl<'a> From<&'a String> for Type { - fn from(src: &'a String) -> Self { - Type::new(src) +impl From<&String> for Type { + fn from(src: &String) -> Self { + Self::new(src) } } -impl<'a> From<&'a Type> for Type { - fn from(src: &'a Type) -> Self { +impl From<&Type> for Type { + fn from(src: &Self) -> Self { src.clone() } } diff --git a/src/type_def.rs b/src/type_def.rs index be82737..6b91292 100644 --- a/src/type_def.rs +++ b/src/type_def.rs @@ -22,7 +22,7 @@ pub struct TypeDef { impl TypeDef { /// Return a structure definition with the provided name pub fn new(name: &str) -> Self { - TypeDef { + Self { ty: Type::new(name), vis: None, docs: None, @@ -107,7 +107,7 @@ impl TypeDef { Ok(()) } - fn fmt_allow(&self, fmt: &mut Formatter) -> fmt::Result { + fn fmt_allow(&self, fmt: &mut Formatter<'_>) -> fmt::Result { for allow in &self.allow { write!(fmt, "#[allow({})]\n", allow)?; } @@ -117,7 +117,7 @@ impl TypeDef { fn fmt_repr(&self, fmt: &mut Formatter<'_>) -> fmt::Result { if let Some(ref repr) = self.repr { - write!(fmt, "#[repr({})]\n", repr)?; + writeln!(fmt, "#[repr({})]", repr)?; } Ok(()) @@ -134,7 +134,7 @@ impl TypeDef { write!(fmt, "{}", name)?; } - write!(fmt, ")]\n")?; + writeln!(fmt, ")]")?; } Ok(()) @@ -142,7 +142,7 @@ impl TypeDef { fn fmt_macros(&self, fmt: &mut Formatter<'_>) -> fmt::Result { for m in self.macros.iter() { - write!(fmt, "{}\n", m)?; + writeln!(fmt, "{}", m)?; } Ok(()) } diff --git a/src/variant.rs b/src/variant.rs index 164f2e4..3edfea8 100644 --- a/src/variant.rs +++ b/src/variant.rs @@ -15,7 +15,7 @@ pub struct Variant { impl Variant { /// Return a new enum variant with the given name. pub fn new(name: &str) -> Self { - Variant { + Self { name: name.to_string(), fields: Fields::Empty, } @@ -40,7 +40,7 @@ impl Variant { pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { write!(fmt, "{}", self.name)?; self.fields.fmt(fmt)?; - write!(fmt, ",\n")?; + writeln!(fmt, ",")?; Ok(()) }