Skip to content

Fix clippy lints #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![],
Expand All @@ -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
}
Expand All @@ -54,7 +54,7 @@ impl Block {
write!(fmt, " ")?;
}

write!(fmt, "{{\n")?;
writeln!(fmt, "{{")?;

fmt.indent(|fmt| {
for b in &self.body {
Expand All @@ -70,7 +70,7 @@ impl Block {
write!(fmt, "{}", after)?;
}

write!(fmt, "\n")?;
writeln!(fmt)?;
Ok(())
}
}
4 changes: 2 additions & 2 deletions src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
}
4 changes: 2 additions & 2 deletions src/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
4 changes: 2 additions & 2 deletions src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Field {
where
T: Into<Type>,
{
Field {
Self {
name: name.into(),
ty: ty.into(),
documentation: Vec::new(),
Expand Down
26 changes: 13 additions & 13 deletions src/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -44,11 +44,11 @@ impl Fields {
where
T: Into<Type>,
{
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"),
Expand All @@ -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, "(")?;
Expand All @@ -96,7 +96,7 @@ impl Fields {

write!(fmt, ")")?;
}
Fields::Empty => {}
Self::Empty => {}
}

Ok(())
Expand Down
14 changes: 7 additions & 7 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -38,9 +38,9 @@ impl<'a> Formatter<'a> {
write!(self, " ")?;
}

write!(self, "{{\n")?;
writeln!(self, "{{")?;
self.indent(f)?;
write!(self, "}}\n")?;
writeln!(self, "}}")?;
Ok(())
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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, ",")?;
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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)?;
}

Expand All @@ -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)?;
}

Expand All @@ -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)?;
}
Expand All @@ -281,7 +281,7 @@ impl Function {
panic!("impl blocks must define fn bodies");
}

write!(fmt, ";\n")
writeln!(fmt, ";")
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Impl {
where
T: Into<Type>,
{
Impl {
Self {
target: target.into(),
generics: vec![],
impl_trait: None,
Expand Down Expand Up @@ -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)?;
Expand All @@ -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)?;
Expand Down
2 changes: 1 addition & 1 deletion src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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.
//!
Expand Down Expand Up @@ -47,7 +47,6 @@ mod r#struct;
mod r#trait;
mod r#type;


pub use associated_type::*;
pub use block::*;
pub use field::*;
Expand Down
Loading