Deny 2018 edition idioms globally (#911)

This commit is contained in:
lzutao 2019-05-07 03:50:34 +07:00 committed by Dylan DPC
parent ab7802a9a9
commit 581187098c
9 changed files with 40 additions and 35 deletions

View File

@ -82,7 +82,7 @@ impl Book {
} }
/// Get a depth-first iterator over the items in the book. /// Get a depth-first iterator over the items in the book.
pub fn iter(&self) -> BookItems { pub fn iter(&self) -> BookItems<'_> {
BookItems { BookItems {
items: self.sections.iter().collect(), items: self.sections.iter().collect(),
} }
@ -286,7 +286,7 @@ impl<'a> Iterator for BookItems<'a> {
} }
impl Display for Chapter { impl Display for Chapter {
fn fmt(&self, f: &mut Formatter) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
if let Some(ref section_number) = self.number { if let Some(ref section_number) = self.number {
write!(f, "{} ", section_number)?; write!(f, "{} ", section_number)?;
} }

View File

@ -37,10 +37,10 @@ pub struct MDBook {
pub config: Config, pub config: Config,
/// A representation of the book's contents in memory. /// A representation of the book's contents in memory.
pub book: Book, pub book: Book,
renderers: Vec<Box<Renderer>>, renderers: Vec<Box<dyn Renderer>>,
/// List of pre-processors to be run on the book /// List of pre-processors to be run on the book
preprocessors: Vec<Box<Preprocessor>>, preprocessors: Vec<Box<dyn Preprocessor>>,
} }
impl MDBook { impl MDBook {
@ -146,7 +146,7 @@ impl MDBook {
/// // etc. /// // etc.
/// # } /// # }
/// ``` /// ```
pub fn iter(&self) -> BookItems { pub fn iter(&self) -> BookItems<'_> {
self.book.iter() self.book.iter()
} }
@ -183,7 +183,7 @@ impl MDBook {
} }
/// Run the entire build process for a particular `Renderer`. /// Run the entire build process for a particular `Renderer`.
fn execute_build_process(&self, renderer: &Renderer) -> Result<()> { fn execute_build_process(&self, renderer: &dyn Renderer) -> Result<()> {
let mut preprocessed_book = self.book.clone(); let mut preprocessed_book = self.book.clone();
let preprocess_ctx = PreprocessorContext::new( let preprocess_ctx = PreprocessorContext::new(
self.root.clone(), self.root.clone(),
@ -217,7 +217,7 @@ impl MDBook {
Ok(()) Ok(())
} }
fn render(&self, preprocessed_book: &Book, renderer: &Renderer) -> Result<()> { fn render(&self, preprocessed_book: &Book, renderer: &dyn Renderer) -> Result<()> {
let name = renderer.name(); let name = renderer.name();
let build_dir = self.build_dir_for(name); let build_dir = self.build_dir_for(name);
@ -344,8 +344,8 @@ impl MDBook {
} }
/// Look at the `Config` and try to figure out what renderers to use. /// Look at the `Config` and try to figure out what renderers to use.
fn determine_renderers(config: &Config) -> Vec<Box<Renderer>> { fn determine_renderers(config: &Config) -> Vec<Box<dyn Renderer>> {
let mut renderers: Vec<Box<Renderer>> = Vec::new(); let mut renderers: Vec<Box<dyn Renderer>> = Vec::new();
if let Some(output_table) = config.get("output").and_then(Value::as_table) { if let Some(output_table) = config.get("output").and_then(Value::as_table) {
for (key, table) in output_table.iter() { for (key, table) in output_table.iter() {
@ -367,20 +367,20 @@ fn determine_renderers(config: &Config) -> Vec<Box<Renderer>> {
renderers renderers
} }
fn default_preprocessors() -> Vec<Box<Preprocessor>> { fn default_preprocessors() -> Vec<Box<dyn Preprocessor>> {
vec![ vec![
Box::new(LinkPreprocessor::new()), Box::new(LinkPreprocessor::new()),
Box::new(IndexPreprocessor::new()), Box::new(IndexPreprocessor::new()),
] ]
} }
fn is_default_preprocessor(pre: &Preprocessor) -> bool { fn is_default_preprocessor(pre: &dyn Preprocessor) -> bool {
let name = pre.name(); let name = pre.name();
name == LinkPreprocessor::NAME || name == IndexPreprocessor::NAME name == LinkPreprocessor::NAME || name == IndexPreprocessor::NAME
} }
/// Look at the `MDBook` and try to figure out what preprocessors to run. /// Look at the `MDBook` and try to figure out what preprocessors to run.
fn determine_preprocessors(config: &Config) -> Result<Vec<Box<Preprocessor>>> { fn determine_preprocessors(config: &Config) -> Result<Vec<Box<dyn Preprocessor>>> {
let mut preprocessors = Vec::new(); let mut preprocessors = Vec::new();
if config.build.use_default_preprocessors { if config.build.use_default_preprocessors {
@ -432,7 +432,11 @@ fn interpret_custom_renderer(key: &str, table: &Value) -> Box<CmdRenderer> {
/// ///
/// The `build.use-default-preprocessors` config option can be used to ensure /// The `build.use-default-preprocessors` config option can be used to ensure
/// default preprocessors always run if they support the renderer. /// default preprocessors always run if they support the renderer.
fn preprocessor_should_run(preprocessor: &Preprocessor, renderer: &Renderer, cfg: &Config) -> bool { fn preprocessor_should_run(
preprocessor: &dyn Preprocessor,
renderer: &dyn Renderer,
cfg: &Config,
) -> bool {
// default preprocessors should be run by default (if supported) // default preprocessors should be run by default (if supported)
if cfg.build.use_default_preprocessors && is_default_preprocessor(preprocessor) { if cfg.build.use_default_preprocessors && is_default_preprocessor(preprocessor) {
return preprocessor.supports_renderer(renderer.name()); return preprocessor.supports_renderer(renderer.name());

View File

@ -195,7 +195,7 @@ macro_rules! collect_events {
} }
impl<'a> SummaryParser<'a> { impl<'a> SummaryParser<'a> {
fn new(text: &str) -> SummaryParser { fn new(text: &str) -> SummaryParser<'_> {
let pulldown_parser = pulldown_cmark::Parser::new(text); let pulldown_parser = pulldown_cmark::Parser::new(text);
SummaryParser { SummaryParser {
@ -471,7 +471,7 @@ fn get_last_link(links: &mut [SummaryItem]) -> Result<(usize, &mut Link)> {
/// Removes the styling from a list of Markdown events and returns just the /// Removes the styling from a list of Markdown events and returns just the
/// plain text. /// plain text.
fn stringify_events(events: Vec<Event>) -> String { fn stringify_events(events: Vec<Event<'_>>) -> String {
events events
.into_iter() .into_iter()
.filter_map(|t| match t { .filter_map(|t| match t {
@ -487,7 +487,7 @@ fn stringify_events(events: Vec<Event>) -> String {
pub struct SectionNumber(pub Vec<u32>); pub struct SectionNumber(pub Vec<u32>);
impl Display for SectionNumber { impl Display for SectionNumber {
fn fmt(&self, f: &mut Formatter) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
if self.0.is_empty() { if self.0.is_empty() {
write!(f, "0") write!(f, "0")
} else { } else {

View File

@ -81,6 +81,7 @@
//! [`Config`]: config/struct.Config.html //! [`Config`]: config/struct.Config.html
#![deny(missing_docs)] #![deny(missing_docs)]
#![deny(rust_2018_idioms)]
#[macro_use] #[macro_use]
extern crate error_chain; extern crate error_chain;

View File

@ -294,7 +294,7 @@ impl<'a> Iterator for LinkIter<'a> {
} }
} }
fn find_links(contents: &str) -> LinkIter { fn find_links(contents: &str) -> LinkIter<'_> {
// lazily compute following regex // lazily compute following regex
// r"\\\{\{#.*\}\}|\{\{#([a-zA-Z0-9]+)\s*([a-zA-Z0-9_.\-:/\\\s]+)\}\}")?; // r"\\\{\{#.*\}\}|\{\{#([a-zA-Z0-9]+)\s*([a-zA-Z0-9_.\-:/\\\s]+)\}\}")?;
lazy_static! { lazy_static! {

View File

@ -26,7 +26,7 @@ impl HtmlHandlebars {
fn render_item( fn render_item(
&self, &self,
item: &BookItem, item: &BookItem,
mut ctx: RenderItemContext, mut ctx: RenderItemContext<'_>,
print_content: &mut String, print_content: &mut String,
) -> Result<()> { ) -> Result<()> {
// FIXME: This should be made DRY-er and rely less on mutable state // FIXME: This should be made DRY-er and rely less on mutable state
@ -505,7 +505,7 @@ fn build_header_links(html: &str) -> String {
let mut id_counter = HashMap::new(); let mut id_counter = HashMap::new();
regex regex
.replace_all(html, |caps: &Captures| { .replace_all(html, |caps: &Captures<'_>| {
let level = caps[1] let level = caps[1]
.parse() .parse()
.expect("Regex should ensure we only ever get numbers here"); .expect("Regex should ensure we only ever get numbers here");
@ -552,7 +552,7 @@ fn wrap_header_with_link(
fn fix_code_blocks(html: &str) -> String { fn fix_code_blocks(html: &str) -> String {
let regex = Regex::new(r##"<code([^>]+)class="([^"]+)"([^>]*)>"##).unwrap(); let regex = Regex::new(r##"<code([^>]+)class="([^"]+)"([^>]*)>"##).unwrap();
regex regex
.replace_all(html, |caps: &Captures| { .replace_all(html, |caps: &Captures<'_>| {
let before = &caps[1]; let before = &caps[1];
let classes = &caps[2].replace(",", " "); let classes = &caps[2].replace(",", " ");
let after = &caps[3]; let after = &caps[3];
@ -570,7 +570,7 @@ fn fix_code_blocks(html: &str) -> String {
fn add_playpen_pre(html: &str, playpen_config: &Playpen) -> String { fn add_playpen_pre(html: &str, playpen_config: &Playpen) -> String {
let regex = Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"##).unwrap(); let regex = Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"##).unwrap();
regex regex
.replace_all(html, |caps: &Captures| { .replace_all(html, |caps: &Captures<'_>| {
let text = &caps[1]; let text = &caps[1];
let classes = &caps[2]; let classes = &caps[2];
let code = &caps[3]; let code = &caps[3];

View File

@ -47,7 +47,7 @@ impl Target {
fn find_chapter( fn find_chapter(
ctx: &Context, ctx: &Context,
rc: &mut RenderContext, rc: &mut RenderContext<'_>,
target: Target, target: Target,
) -> Result<Option<StringMap>, RenderError> { ) -> Result<Option<StringMap>, RenderError> {
debug!("Get data from context"); debug!("Get data from context");
@ -86,11 +86,11 @@ fn find_chapter(
} }
fn render( fn render(
_h: &Helper, _h: &Helper<'_, '_>,
r: &Handlebars, r: &Handlebars,
ctx: &Context, ctx: &Context,
rc: &mut RenderContext, rc: &mut RenderContext<'_>,
out: &mut Output, out: &mut dyn Output,
chapter: &StringMap, chapter: &StringMap,
) -> Result<(), RenderError> { ) -> Result<(), RenderError> {
trace!("Creating BTreeMap to inject in context"); trace!("Creating BTreeMap to inject in context");
@ -137,11 +137,11 @@ fn render(
} }
pub fn previous( pub fn previous(
_h: &Helper, _h: &Helper<'_, '_>,
r: &Handlebars, r: &Handlebars,
ctx: &Context, ctx: &Context,
rc: &mut RenderContext, rc: &mut RenderContext<'_>,
out: &mut Output, out: &mut dyn Output,
) -> Result<(), RenderError> { ) -> Result<(), RenderError> {
trace!("previous (handlebars helper)"); trace!("previous (handlebars helper)");
@ -153,11 +153,11 @@ pub fn previous(
} }
pub fn next( pub fn next(
_h: &Helper, _h: &Helper<'_, '_>,
r: &Handlebars, r: &Handlebars,
ctx: &Context, ctx: &Context,
rc: &mut RenderContext, rc: &mut RenderContext<'_>,
out: &mut Output, out: &mut dyn Output,
) -> Result<(), RenderError> { ) -> Result<(), RenderError> {
trace!("next (handlebars helper)"); trace!("next (handlebars helper)");

View File

@ -1,11 +1,11 @@
use handlebars::{Context, Handlebars, Helper, Output, RenderContext, RenderError}; use handlebars::{Context, Handlebars, Helper, Output, RenderContext, RenderError};
pub fn theme_option( pub fn theme_option(
h: &Helper, h: &Helper<'_, '_>,
_r: &Handlebars, _r: &Handlebars,
ctx: &Context, ctx: &Context,
rc: &mut RenderContext, rc: &mut RenderContext<'_>,
out: &mut Output, out: &mut dyn Output,
) -> Result<(), RenderError> { ) -> Result<(), RenderError> {
trace!("theme_option (handlebars helper)"); trace!("theme_option (handlebars helper)");

View File

@ -157,7 +157,7 @@ impl EventQuoteConverter {
} }
} }
fn clean_codeblock_headers(event: Event) -> Event { fn clean_codeblock_headers(event: Event<'_>) -> Event<'_> {
match event { match event {
Event::Start(Tag::CodeBlock(ref info)) => { Event::Start(Tag::CodeBlock(ref info)) => {
let info: String = info.chars().filter(|ch| !ch.is_whitespace()).collect(); let info: String = info.chars().filter(|ch| !ch.is_whitespace()).collect();