From 85529186e446620056237dda2fa0a10f1d2ad75c Mon Sep 17 00:00:00 2001 From: Christian Friedrich Coors Date: Sun, 30 Sep 2018 21:15:09 +0200 Subject: [PATCH] Fix some clippy warnings --- components/config/src/lib.rs | 4 +-- components/content/src/page.rs | 12 +++---- components/content/src/section.rs | 4 +-- components/pagination/src/lib.rs | 8 ++--- components/rebuild/src/lib.rs | 47 +++++++++++--------------- components/rendering/src/markdown.rs | 34 +++++++++---------- components/rendering/src/shortcode.rs | 6 ++-- components/search/src/lib.rs | 6 ++-- components/site/src/lib.rs | 10 +++--- components/taxonomies/src/lib.rs | 2 +- components/templates/src/global_fns.rs | 8 ++--- src/cmd/init.rs | 2 +- src/cmd/serve.rs | 4 +-- src/console.rs | 4 +-- 14 files changed, 68 insertions(+), 83 deletions(-) diff --git a/components/config/src/lib.rs b/components/config/src/lib.rs index 9bf23e9..0aa884e 100644 --- a/components/config/src/lib.rs +++ b/components/config/src/lib.rs @@ -195,9 +195,7 @@ impl Config { format!("{}/", self.base_url) } else if self.base_url.ends_with('/') && path.starts_with('/') { format!("{}{}{}", self.base_url, &path[1..], trailing_bit) - } else if self.base_url.ends_with('/') { - format!("{}{}{}", self.base_url, path, trailing_bit) - } else if path.starts_with('/') { + } else if self.base_url.ends_with('/') || path.starts_with('/') { format!("{}{}{}", self.base_url, path, trailing_bit) } else { format!("{}/{}{}", self.base_url, path, trailing_bit) diff --git a/components/content/src/page.rs b/components/content/src/page.rs index e9fd5ac..df5b3ab 100644 --- a/components/content/src/page.rs +++ b/components/content/src/page.rs @@ -103,16 +103,14 @@ impl Page { page.slug = { if let Some(ref slug) = page.meta.slug { slug.trim().to_string() - } else { - if page.file.name == "index" { - if let Some(parent) = page.file.path.parent() { - slugify(parent.file_name().unwrap().to_str().unwrap()) - } else { - slugify(page.file.name.clone()) - } + } else if page.file.name == "index" { + if let Some(parent) = page.file.path.parent() { + slugify(parent.file_name().unwrap().to_str().unwrap()) } else { slugify(page.file.name.clone()) } + } else { + slugify(page.file.name.clone()) } }; diff --git a/components/content/src/section.rs b/components/content/src/section.rs index e5e5825..7433dfc 100644 --- a/components/content/src/section.rs +++ b/components/content/src/section.rs @@ -212,8 +212,8 @@ impl Section { subsections, pages: vec![], ignored_pages: vec![], - word_count: self.word_count.clone(), - reading_time: self.reading_time.clone(), + word_count: self.word_count, + reading_time: self.reading_time, } } } diff --git a/components/pagination/src/lib.rs b/components/pagination/src/lib.rs index ff9347f..6b45ee4 100644 --- a/components/pagination/src/lib.rs +++ b/components/pagination/src/lib.rs @@ -149,12 +149,10 @@ impl<'a> Paginator<'a> { let pager_path = if self.is_index { page_path + } else if self.path.ends_with('/') { + format!("{}{}", self.path, page_path) } else { - if self.path.ends_with("/") { - format!("{}{}", self.path, page_path) - } else { - format!("{}/{}", self.path, page_path) - } + format!("{}/{}", self.path, page_path) }; pagers.push(Pager::new( diff --git a/components/rebuild/src/lib.rs b/components/rebuild/src/lib.rs index 07652a8..6a4f2c2 100644 --- a/components/rebuild/src/lib.rs +++ b/components/rebuild/src/lib.rs @@ -107,27 +107,25 @@ fn delete_element(site: &mut Site, path: &Path, is_section: bool) -> Result<()> site.permalinks.remove(&s.file.relative); site.populate_sections(); } - } else { - if let Some(p) = site.pages.remove(path) { - site.permalinks.remove(&p.file.relative); + } else if let Some(p) = site.pages.remove(path) { + site.permalinks.remove(&p.file.relative); - if !p.meta.taxonomies.is_empty() { - site.populate_taxonomies()?; - } + if !p.meta.taxonomies.is_empty() { + site.populate_taxonomies()?; + } - // if there is a parent section, we will need to re-render it - // most likely - if find_parent_section(site, &p).is_some() { - site.populate_sections(); - } - }; + // if there is a parent section, we will need to re-render it + // most likely + if find_parent_section(site, &p).is_some() { + site.populate_sections(); + } } // Ensure we have our fn updated so it doesn't contain the permalink(s)/section/page deleted site.register_tera_global_fns(); // Deletion is something that doesn't happen all the time so we // don't need to optimise it too much - return site.build(); + site.build() } /// Handles a `_index.md` (a section) being edited in some ways @@ -164,24 +162,21 @@ fn handle_section_editing(site: &mut Site, path: &Path) -> Result<()> { } }; } - return Ok(()); + Ok(()) } // New section, only render that one None => { site.populate_sections(); site.register_tera_global_fns(); - return site.render_section(&site.sections[path], true); + site.render_section(&site.sections[path], true) } - }; + } } macro_rules! render_parent_section { ($site: expr, $path: expr) => { - match find_parent_section($site, &$site.pages[$path]) { - Some(s) => { - $site.render_section(s, false)?; - }, - None => (), + if let Some(s) = find_parent_section($site, &$site.pages[$path]) { + $site.render_section(s, false)?; }; } } @@ -293,12 +288,10 @@ pub fn after_content_change(site: &mut Site, path: &Path) -> Result<()> { } else { handle_page_editing(site, path) } + } else if index.exists() { + handle_page_editing(site, &index) } else { - if index.exists() { - handle_page_editing(site, &index) - } else { - Ok(()) - } + Ok(()) } } @@ -325,7 +318,7 @@ pub fn after_template_change(site: &mut Site, path: &Path) -> Result<()> { // because we have no clue which one needs rebuilding // TODO: look if there the shortcode is used in the markdown instead of re-rendering // everything - if path.components().collect::>().contains(&Component::Normal("shortcodes".as_ref())) { + if path.components().any(|x| x == Component::Normal("shortcodes".as_ref())) { site.render_markdown()?; } site.populate_sections(); diff --git a/components/rendering/src/markdown.rs b/components/rendering/src/markdown.rs index f3c5dfc..63d45eb 100644 --- a/components/rendering/src/markdown.rs +++ b/components/rendering/src/markdown.rs @@ -20,7 +20,7 @@ const CONTINUE_READING: &str = "

\n"; pub struct Rendered { pub body: String, pub summary_len: Option, - pub toc: Vec
+ pub toc: Vec
, } // We might have cases where the slug is already present in our list of anchor @@ -41,7 +41,7 @@ fn find_anchor(anchors: &[String], name: String, level: u8) -> String { } fn is_colocated_asset_link(link: &str) -> bool { - !link.contains("/") // http://, ftp://, ../ etc + !link.contains('/') // http://, ftp://, ../ etc && !link.starts_with("mailto:") } @@ -159,22 +159,20 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result Result) -> (String, Map) { } -fn render_shortcode(name: String, args: Map, context: &RenderContext, body: Option<&str>) -> Result { +fn render_shortcode(name: &str, args: &Map, context: &RenderContext, body: Option<&str>) -> Result { let mut tera_context = Context::new(); for (key, value) in args.iter() { tera_context.insert(key, value); @@ -138,7 +138,7 @@ pub fn render_shortcodes(content: &str, context: &RenderContext) -> Result res.push_str(p.into_span().as_str()), Rule::inline_shortcode => { let (name, args) = parse_shortcode_call(p); - res.push_str(&render_shortcode(name, args, context, None)?); + res.push_str(&render_shortcode(&name, &args, context, None)?); } Rule::shortcode_with_body => { let mut inner = p.into_inner(); @@ -146,7 +146,7 @@ pub fn render_shortcodes(content: &str, context: &RenderContext) -> Result { res.push_str( diff --git a/components/search/src/lib.rs b/components/search/src/lib.rs index ea3bcb2..cfc28ca 100644 --- a/components/search/src/lib.rs +++ b/components/search/src/lib.rs @@ -15,7 +15,7 @@ use content::Section; use errors::Result; -pub const ELASTICLUNR_JS: &'static str = include_str!("elasticlunr.min.js"); +pub const ELASTICLUNR_JS: &str = include_str!("elasticlunr.min.js"); lazy_static! { static ref AMMONIA: ammonia::Builder<'static> = { @@ -63,7 +63,7 @@ fn add_section_to_index(index: &mut Index, section: &Section) { if section.meta.redirect_to.is_none() { index.add_doc( §ion.permalink, - &[§ion.meta.title.clone().unwrap_or(String::new()), &AMMONIA.clean(§ion.content).to_string()], + &[§ion.meta.title.clone().unwrap_or_default(), &AMMONIA.clean(§ion.content).to_string()], ); } @@ -74,7 +74,7 @@ fn add_section_to_index(index: &mut Index, section: &Section) { index.add_doc( &page.permalink, - &[&page.meta.title.clone().unwrap_or(String::new()), &AMMONIA.clean(&page.content).to_string()], + &[&page.meta.title.clone().unwrap_or_default(), &AMMONIA.clean(&page.content).to_string()], ); } } diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index 4d3d8ba..d1da3c8 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -189,7 +189,7 @@ impl Site { let (section_entries, page_entries): (Vec<_>, Vec<_>) = glob(&content_glob) .unwrap() .filter_map(|e| e.ok()) - .filter(|e| !e.as_path().file_name().unwrap().to_str().unwrap().starts_with(".")) + .filter(|e| !e.as_path().file_name().unwrap().to_str().unwrap().starts_with('.')) .partition(|entry| entry.as_path().file_name().unwrap() == "_index.md"); let sections = { @@ -272,7 +272,7 @@ impl Site { // This is needed in the first place because of silly borrow checker let mut pages_insert_anchors = HashMap::new(); - for (_, p) in &self.pages { + for p in self.pages.values() { pages_insert_anchors.insert(p.file.path.clone(), self.find_parent_section_insert_anchor(&p.file.parent.clone())); } @@ -580,10 +580,10 @@ impl Site { let mut options = SassOptions::default(); options.output_style = OutputStyle::Compressed; - let mut compiled_paths = self.compile_sass_glob(&sass_path, "scss", options.clone())?; + let mut compiled_paths = self.compile_sass_glob(&sass_path, "scss", &options.clone())?; options.indented_syntax = true; - compiled_paths.extend(self.compile_sass_glob(&sass_path, "sass", options)?); + compiled_paths.extend(self.compile_sass_glob(&sass_path, "sass", &options)?); compiled_paths.sort(); for window in compiled_paths.windows(2) { @@ -600,7 +600,7 @@ impl Site { Ok(()) } - fn compile_sass_glob(&self, sass_path: &Path, extension: &str, options: SassOptions) -> Result> { + fn compile_sass_glob(&self, sass_path: &Path, extension: &str, options: &SassOptions) -> Result> { let glob_string = format!("{}/**/*.{}", sass_path.display(), extension); let files = glob(&glob_string) .unwrap() diff --git a/components/taxonomies/src/lib.rs b/components/taxonomies/src/lib.rs index 36dc4f2..422c0b2 100644 --- a/components/taxonomies/src/lib.rs +++ b/components/taxonomies/src/lib.rs @@ -127,7 +127,7 @@ pub fn find_taxonomies(config: &Config, all_pages: Vec<&Page>) -> Result GlobalFn { String, args.get("lang"), "`trans`: `lang` must be a string." - ).unwrap_or(default_lang.clone()); + ).unwrap_or_else(|| default_lang.clone()); let translations = &translations_config[lang.as_str()]; Ok(to_value(&translations[key.as_str()]).unwrap()) }) @@ -122,7 +122,7 @@ pub fn make_get_url(permalinks: HashMap, config: Config) -> Glob } else { // anything else let mut permalink = config.make_permalink(&path); - if !trailing_slash && permalink.ends_with("/") { + if !trailing_slash && permalink.ends_with('/') { permalink.pop(); // Removes the slash } @@ -153,7 +153,7 @@ pub fn make_get_taxonomy(all_taxonomies: Vec) -> GlobalFn { ), }; - return Ok(to_value(container).unwrap()); + Ok(to_value(container).unwrap()) }) } @@ -217,7 +217,7 @@ pub fn make_resize_image(imageproc: Arc>) -> GlobalF String, args.get("op"), "`resize_image`: `op` must be a string" - ).unwrap_or(DEFAULT_OP.to_string()); + ).unwrap_or_else(|| DEFAULT_OP.to_string()); let quality = optional_arg!( u8, args.get("quality"), diff --git a/src/cmd/init.rs b/src/cmd/init.rs index d7b5760..7d052e4 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -8,7 +8,7 @@ use prompt::{ask_bool, ask_url}; use console; -const CONFIG: &'static str = r#" +const CONFIG: &str = r#" # The URL the site will be built for base_url = "%BASE_URL%" diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index 2265c08..92481e0 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -56,7 +56,7 @@ enum ChangeKind { // Also, commenting out the lines 330-340 (containing `e instanceof ProtocolError`) was needed // as it seems their build didn't work well and didn't include ProtocolError so it would error on // errors -const LIVE_RELOAD: &'static str = include_str!("livereload.js"); +const LIVE_RELOAD: &str = include_str!("livereload.js"); struct NotFoundHandler { rendered_template: PathBuf, @@ -276,7 +276,7 @@ pub fn serve(interface: &str, port: &str, output_dir: &str, base_url: &str, conf rebuild_done_handling(&broadcaster, site.compile_sass(&site.base_path), &p.to_string_lossy()); }, (ChangeKind::Config, _) => { - console::info(&format!("-> Config changed. The whole site will be reloaded. The browser needs to be refreshed to make the changes visible.")); + console::info("-> Config changed. The whole site will be reloaded. The browser needs to be refreshed to make the changes visible."); site = create_new_site(interface, port, output_dir, base_url, config_file).unwrap().0; } }; diff --git a/src/console.rs b/src/console.rs index a643eb3..f1e7a7f 100644 --- a/src/console.rs +++ b/src/console.rs @@ -99,8 +99,8 @@ pub fn unravel_errors(message: &str, error: &Error) { /// Check whether to output colors fn has_color() -> bool { - let use_colors = env::var("CLICOLOR").unwrap_or("1".to_string()) != "0" && !env::var("NO_COLOR").is_ok(); - let force_colors = env::var("CLICOLOR_FORCE").unwrap_or("0".to_string()) != "0"; + let use_colors = env::var("CLICOLOR").unwrap_or_else(|_| "1".to_string()) != "0" && env::var("NO_COLOR").is_err(); + let force_colors = env::var("CLICOLOR_FORCE").unwrap_or_else(|_|"0".to_string()) != "0"; force_colors || use_colors && atty::is(atty::Stream::Stdout) }