Skip to content
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

Fix all current clippy warnings #80

Open
wants to merge 1 commit 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
11 changes: 4 additions & 7 deletions src/config/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,17 @@ impl Manifest {
Manifest {
name: cargo_toml.package.name,
license: cargo_toml.package.license,
lib: cargo_toml.lib.map(|lib| ManifestLib::from_cargo_toml(lib)),
lib: cargo_toml.lib.map(ManifestLib::from_cargo_toml),
bin: cargo_toml
.bin
.map(|bin_vec| {
bin_vec
.into_iter()
.map(|bin| ManifestLib::from_cargo_toml(bin))
.map(ManifestLib::from_cargo_toml)
.collect()
})
.unwrap_or_default(),
badges: cargo_toml
.badges
.map(|b| process_badges(b))
.unwrap_or_default(),
badges: cargo_toml.badges.map(process_badges).unwrap_or_default(),
version: cargo_toml.package.version,
}
}
Expand Down Expand Up @@ -96,7 +93,7 @@ fn process_badges(badges: BTreeMap<String, BTreeMap<String, String>>) -> Vec<Str
Some((8, badges::is_it_maintained_open_issues(attrs)))
}
"maintenance" => Some((9, badges::maintenance(attrs))),
_ => return None,
_ => None,
})
.collect();

Expand Down
4 changes: 2 additions & 2 deletions src/config/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ pub fn find_entrypoint(current_dir: &Path, manifest: &Manifest) -> Result<PathBu
}

// try bin defined in `Cargo.toml`
if manifest.bin.len() > 0 {
if !manifest.bin.is_empty() {
let mut bin_list: Vec<_> = manifest
.bin
.iter()
.filter(|b| b.doc == true)
.filter(|b| b.doc)
.map(|b| b.path.clone())
.collect();

Expand Down
22 changes: 10 additions & 12 deletions src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::path::{Path, PathBuf};
use cargo_readme::get_manifest;
use cargo_readme::project;

const DEFAULT_TEMPLATE: &'static str = "README.tpl";
const DEFAULT_TEMPLATE: &str = "README.tpl";

/// Get the project root from given path or defaults to current directory
///
Expand All @@ -24,7 +24,7 @@ pub fn get_source(project_root: &Path, input: Option<&str>) -> Result<File, Stri
File::open(&input)
.map_err(|e| format!("Could not open file '{}': {}", input.to_string_lossy(), e))
}
None => find_entrypoint(&project_root),
None => find_entrypoint(project_root),
}
}

Expand All @@ -33,7 +33,7 @@ pub fn get_dest(project_root: &Path, output: Option<&str>) -> Result<Option<File
match output {
Some(filename) => {
let output = project_root.join(filename);
File::create(&output).map(|f| Some(f)).map_err(|e| {
File::create(&output).map(Some).map_err(|e| {
format!(
"Could not create output file '{}': {}",
output.to_string_lossy(),
Expand All @@ -54,26 +54,24 @@ pub fn get_template_file(
// template path was given, try to read it
Some(template) => {
let template = project_root.join(template);
File::open(&template).map(|f| Some(f)).map_err(|e| {
File::open(&template).map(Some).map_err(|e| {
format!(
"Could not open template file '{}': {}",
template.to_string_lossy(),
e
)
})
}
// try to read the defautl template file
// try to read the default template file
None => {
let template = project_root.join(DEFAULT_TEMPLATE);
match File::open(&template) {
Ok(file) => Ok(Some(file)),
// do not generate an error on file not found
Err(ref e) if e.kind() != ErrorKind::NotFound => {
return Err(format!(
"Could not open template file '{}': {}",
DEFAULT_TEMPLATE, e
))
}
Err(ref e) if e.kind() != ErrorKind::NotFound => Err(format!(
"Could not open template file '{}': {}",
DEFAULT_TEMPLATE, e
)),
// default template not found, return `None`
_ => Ok(None),
}
Expand All @@ -89,7 +87,7 @@ pub fn write_output(dest: &mut Option<File>, readme: String) -> Result<(), Strin
// Append new line at end of file to match behavior of `cargo readme > README.md`
bytes.push(b'\n');

dest.write_all(&mut bytes)
dest.write_all(&bytes)
.map(|_| ())
.map_err(|e| format!("Could not write to output file: {}", e))?;
}
Expand Down
13 changes: 5 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,11 @@ fn main() {
.get_matches();

if let Some(m) = matches.subcommand_matches("readme") {
match execute(m) {
Err(e) => {
io::stderr()
.write_fmt(format_args!("Error: {}\n", e))
.expect("An error occurred while trying to show an error message");
std::process::exit(1);
}
_ => {}
if let Err(e) = execute(m) {
io::stderr()
.write_fmt(format_args!("Error: {}\n", e))
.expect("An error occurred while trying to show an error message");
std::process::exit(1);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/readme/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn extract_docs_singleline_style<R: Read>(

if line.starts_with("//!") {
result.push(normalize_line(line));
} else if line.trim().len() > 0 {
} else if !line.trim().is_empty() {
// doc ends, code starts
break;
}
Expand Down Expand Up @@ -61,7 +61,7 @@ fn extract_docs_multiline_style<R: Read>(
nesting -= line.matches("*/").count() as isize;
if nesting < 0 {
let mut line = line;
line.split_off(pos);
let _ = line.split_off(pos);
if !line.trim().is_empty() {
result.push(line);
}
Expand All @@ -82,7 +82,7 @@ fn normalize_line(mut line: String) -> String {
line
} else {
// if the first character after the comment mark is " ", remove it
let split_at = if line.find(" ") == Some(3) { 4 } else { 3 };
let split_at = if line.find(' ') == Some(3) { 4 } else { 3 };
line.split_at(split_at).1.trim_end().to_owned()
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/readme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ pub fn generate_readme<T: Read>(
/// Load a template String from a file
fn get_template_string<T: Read>(template: &mut T) -> Result<String, String> {
let mut template_string = String::new();
match template.read_to_string(&mut template_string) {
Err(e) => return Err(format!("Error: {}", e)),
_ => {}
if let Err(e) = template.read_to_string(&mut template_string) {
return Err(format!("Error: {}", e));
}

Ok(template_string)
Expand Down
8 changes: 4 additions & 4 deletions src/readme/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::iter::{IntoIterator, Iterator};

use regex::Regex;

lazy_static!{
lazy_static! {
// Is this code block rust?
static ref RE_CODE_RUST: Regex = Regex::new(r"^(?P<delimiter>`{3,4}|~{3,4})(?:rust|(?:(?:rust,)?(?:no_run|ignore|should_panic)))?$").unwrap();
// Is this code block just text?
Expand Down Expand Up @@ -38,7 +38,7 @@ impl Processor {
pub fn new(indent_headings: bool) -> Self {
Processor {
section: Section::None,
indent_headings: indent_headings,
indent_headings,
delimiter: None,
}
}
Expand All @@ -50,7 +50,7 @@ impl Processor {
}

// indent heading when outside code
if self.indent_headings && self.section == Section::None && line.starts_with("#") {
if self.indent_headings && self.section == Section::None && line.starts_with('#') {
line.insert(0, '#');
} else if self.section == Section::None {
let l = line.clone();
Expand All @@ -68,7 +68,7 @@ impl Processor {
}
} else if self.section != Section::None && Some(&line) == self.delimiter.as_ref() {
self.section = Section::None;
line = self.delimiter.take().unwrap_or("```".to_owned());
line = self.delimiter.take().unwrap_or_else(|| "```".to_owned());
}

Some(line)
Expand Down
12 changes: 7 additions & 5 deletions src/readme/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,29 @@ fn process_template(
license: Option<&str>,
version: &str,
) -> Result<String, String> {
template = template.trim_end_matches("\n").to_owned();
template = template.trim_end_matches('\n').to_owned();

if !template.contains("{{readme}}") {
return Err("Missing `{{readme}}` in template".to_owned());
}

if template.contains("{{crate}}") {
template = template.replace("{{crate}}", &title);
template = template.replace("{{crate}}", title);
}

if template.contains("{{badges}}") {
if badges.is_empty() {
return Err("`{{badges}}` was found in template but no badges were provided".to_owned());
return Err(
"`{{badges}}` was found in template but no badges were provided".to_owned(),
);
}
let badges = badges.join("\n");
template = template.replace("{{badges}}", &badges);
}

if template.contains("{{license}}") {
if let Some(license) = license {
template = template.replace("{{license}}", &license);
template = template.replace("{{license}}", license);
} else {
return Err(
"`{{license}}` was found in template but no license was provided".to_owned(),
Expand Down Expand Up @@ -114,7 +116,7 @@ fn process_string(

/// Prepend badges to output string
fn prepend_badges(readme: String, badges: &[&str]) -> String {
if badges.len() > 0 {
if !badges.is_empty() {
let badges = badges.join("\n");
if !readme.is_empty() {
format!("{}\n\n{}", badges, readme)
Expand Down