Add clippy, clean up lots

This commit is contained in:
Yuki Izumi 2017-04-15 13:49:52 +10:00 committed by Yuki Izumi
parent 0c9d8af259
commit c9189527e8
12 changed files with 127 additions and 87 deletions

12
.editorconfig Normal file
View File

@ -0,0 +1,12 @@
root = true
[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 4
[*.md]
trim_trailing_whitespace = false

View File

@ -29,6 +29,8 @@ regex = "^0.2.1"
lazy_static = "^0.2.4"
unicode_categories = "^0.1.1"
clap = { version = "^2.22.2", optional = true }
clippy = { version = "^0.0.123", optional = true }
[features]
default = ["clap"]
dev = ["clap", "clippy"]

2
rustfmt.toml Normal file
View File

@ -0,0 +1,2 @@
format_strings = false
reorder_imports = true

View File

@ -44,11 +44,11 @@ enum Escaping {
impl<'a, 'o> Write for CommonMarkFormatter<'a, 'o> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.output(buf, false, Escaping::Literal);
std::result::Result::Ok(buf.len())
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
std::result::Result::Ok(())
Ok(())
}
}
@ -282,7 +282,7 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
}
NodeValue::Item(..) => {
let parent = match node.parent().unwrap().data.borrow().value {
NodeValue::List(ref nl) => nl.clone(),
NodeValue::List(ref nl) => *nl,
_ => unreachable!(),
};
@ -492,7 +492,7 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
if is_autolink(node, nl) {
if entering {
write!(self, "<").unwrap();
if &&nl.url[..7] == &"mailto:" {
if &nl.url[..7] == "mailto:" {
self.write_all(nl.url[7..].as_bytes()).unwrap();
} else {
self.write_all(nl.url.as_bytes()).unwrap();
@ -565,11 +565,11 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
for a in alignments {
write!(self,
" {} |",
match a {
&TableAlignment::Left => ":--",
&TableAlignment::Center => ":-:",
&TableAlignment::Right => "--:",
&TableAlignment::None => "---",
match *a {
TableAlignment::Left => ":--",
TableAlignment::Center => ":-:",
TableAlignment::Right => "--:",
TableAlignment::None => "---",
})
.unwrap();
}
@ -647,7 +647,7 @@ fn is_autolink<'a>(node: &'a AstNode<'a>, nl: &NodeLink) -> bool {
};
let mut real_url: &str = &nl.url;
if &&real_url[..7] == &"mailto:" {
if &real_url[..7] == "mailto:" {
real_url = &real_url[7..];
}

View File

@ -60,7 +60,20 @@
//! # }
//! ```
#![warn(missing_docs)]
#![deny(missing_docs,
missing_debug_implementations,
missing_copy_implementations,
trivial_casts,
trivial_numeric_casts,
unsafe_code,
unstable_features,
unused_import_braces,
unused_qualifications)]
#![cfg_attr(feature = "dev", allow(unstable_features))]
#![cfg_attr(feature = "dev", feature(plugin))]
#![cfg_attr(feature = "dev", plugin(clippy))]
#![allow(doc_markdown, cyclomatic_complexity)]
extern crate unicode_categories;
extern crate typed_arena;

View File

@ -1,3 +1,20 @@
//! The `comrak` binary.
#![deny(missing_docs,
missing_debug_implementations,
missing_copy_implementations,
trivial_casts,
trivial_numeric_casts,
unsafe_code,
unstable_features,
unused_import_braces,
unused_qualifications)]
#![cfg_attr(feature = "dev", allow(unstable_features))]
#![cfg_attr(feature = "dev", feature(plugin))]
#![cfg_attr(feature = "dev", plugin(clippy))]
#![allow(doc_markdown, cyclomatic_complexity)]
#[macro_use]
extern crate clap;
extern crate unicode_categories;

View File

@ -218,7 +218,7 @@ pub struct NodeCodeBlock {
}
/// The metadata of a heading.
#[derive(Default, Debug, Clone)]
#[derive(Default, Debug, Clone, Copy)]
pub struct NodeHeading {
/// The level of the header; from 1 to 6 for ATX headings, 1 or 2 for setext headings.
pub level: u32,

View File

@ -43,7 +43,7 @@ pub fn process_autolinks<'a>(arena: &'a Arena<AstNode<'a>>,
node.insert_after(post);
if i + skip < len {
let remain = contents[i + skip..].to_string();
assert!(remain.len() > 0);
assert!(!remain.is_empty());
post.insert_after(make_inline(arena, NodeValue::Text(remain)));
}
contents.truncate(i);

View File

@ -553,16 +553,14 @@ impl<'a, 'r, 'o> Subject<'a, 'r, 'o> {
}
pub fn skip_line_end(&mut self) -> bool {
let mut seen_line_end_char = false;
let old_pos = self.pos;
if self.peek_char() == Some(&(b'\r')) {
self.pos += 1;
seen_line_end_char = true;
}
if self.peek_char() == Some(&(b'\n')) {
self.pos += 1;
seen_line_end_char = true;
}
seen_line_end_char || self.eof()
self.pos > old_pos || self.eof()
}
pub fn handle_entity(&mut self) -> &'a AstNode<'a> {

View File

@ -65,7 +65,7 @@ pub struct Parser<'a, 'o> {
options: &'o ComrakOptions,
}
#[derive(Default)]
#[derive(Default, Debug, Clone, Copy)]
/// Options for both parser and formatter functions.
pub struct ComrakOptions {
/// [Soft line breaks](http://spec.commonmark.org/0.27/#soft-line-breaks) in the input
@ -355,67 +355,9 @@ impl<'a, 'o> Parser<'a, 'o> {
line: &mut String,
all_matched: &mut bool)
-> Option<&'a AstNode<'a>> {
let mut should_continue = true;
*all_matched = false;
let mut container = self.root;
'done: loop {
while nodes::last_child_is_open(container) {
container = container.last_child().unwrap();
let ast = &mut *container.data.borrow_mut();
self.find_first_nonspace(line);
match ast.value {
NodeValue::BlockQuote => {
if !self.parse_block_quote_prefix(line) {
break 'done;
}
}
NodeValue::Item(ref nl) => {
if !self.parse_node_item_prefix(line, container, nl) {
break 'done;
}
}
NodeValue::CodeBlock(..) => {
if !self.parse_code_block_prefix(
line, container, ast, &mut should_continue) {
break 'done;
}
}
NodeValue::Heading(..) => {
break 'done;
}
NodeValue::HtmlBlock(ref nhb) => {
if !self.parse_html_block_prefix(nhb.block_type) {
break 'done;
}
}
NodeValue::Paragraph => {
if self.blank {
break 'done;
}
}
NodeValue::Table(..) => {
if !table::matches(&line[self.first_nonspace..]) {
break 'done;
}
continue;
}
NodeValue::TableRow(..) => {
break 'done;
}
NodeValue::TableCell => {
break 'done;
}
_ => {}
}
}
*all_matched = true;
break 'done;
}
let (new_all_matched, mut container, should_continue) = self.check_open_blocks_inner(self.root, line);
*all_matched = new_all_matched;
if !*all_matched {
container = container.parent().unwrap();
}
@ -427,6 +369,60 @@ impl<'a, 'o> Parser<'a, 'o> {
}
}
fn check_open_blocks_inner(&mut self, mut container: &'a AstNode<'a>, line: &mut String) -> (bool, &'a AstNode<'a>, bool) {
let mut should_continue = true;
while nodes::last_child_is_open(container) {
container = container.last_child().unwrap();
let ast = &mut *container.data.borrow_mut();
self.find_first_nonspace(line);
match ast.value {
NodeValue::BlockQuote => {
if !self.parse_block_quote_prefix(line) {
return (false, container, should_continue);
}
}
NodeValue::Item(ref nl) => {
if !self.parse_node_item_prefix(line, container, nl) {
return (false, container, should_continue);
}
}
NodeValue::CodeBlock(..) => {
if !self.parse_code_block_prefix(
line, container, ast, &mut should_continue) {
return (false, container, should_continue);
}
}
NodeValue::HtmlBlock(ref nhb) => {
if !self.parse_html_block_prefix(nhb.block_type) {
return (false, container, should_continue);
}
}
NodeValue::Paragraph => {
if self.blank {
return (false, container, should_continue);
}
}
NodeValue::Table(..) => {
if !table::matches(&line[self.first_nonspace..]) {
return (false, container, should_continue);
}
continue;
}
NodeValue::Heading(..) |
NodeValue::TableRow(..) |
NodeValue::TableCell => {
return (false, container, should_continue);
}
_ => {}
}
}
(true, container, should_continue)
}
fn open_new_blocks(&mut self,
container: &mut &'a AstNode<'a>,
line: &mut String,

View File

@ -89,23 +89,23 @@ lazy_static! {
pub fn html_block_start(line: &str) -> Option<usize> {
lazy_static! {
static ref RE1: Regex = Regex::new(r"\A(?:<(script|pre|style)([ \t\v\f\r\n]|>))").unwrap();
static ref RE2: Regex = Regex::new(r"\A(?:<!--)").unwrap();
static ref RE3: Regex = Regex::new(r"\A(?:<\?)").unwrap();
static ref STR2: &'static str = "<!--";
static ref STR3: &'static str = "<?";
static ref RE4: Regex = Regex::new(r"\A(?:<![A-Z])").unwrap();
static ref RE5: Regex = Regex::new(r"\A(?:<!\[CDATA\[)").unwrap();
static ref STR5: &'static str = "<![CDATA[";
static ref RE6: Regex = Regex::new(
&format!(r"\A(?:</?({})([ \t\v\f\r\n]|/?>))", *BLOCK_TAG_NAMES_PIPED)).unwrap();
}
if is_match(&RE1, line) {
Some(1)
} else if is_match(&RE2, line) {
} else if line.starts_with(*STR2) {
Some(2)
} else if is_match(&RE3, line) {
} else if line.starts_with(*STR3) {
Some(3)
} else if is_match(&RE4, line) {
Some(4)
} else if is_match(&RE5, line) {
} else if line.starts_with(*STR5) {
Some(5)
} else if is_match(&RE6, line) {
Some(6)

View File

@ -31,12 +31,12 @@ fn html_opts<F>(input: &str, expected: &str, opts: F)
opts(&mut options);
let root = parse_document(&arena, &input.chars().collect::<String>(), &options);
let output = html::format_document(&root, &options);
let output = html::format_document(root, &options);
compare_strs(&output, expected, "regular");
let md = cm::format_document(&root, &options);
let md = cm::format_document(root, &options);
let root = parse_document(&arena, &md.chars().collect::<String>(), &options);
let output_from_rt = html::format_document(&root, &options);
let output_from_rt = html::format_document(root, &options);
compare_strs(&output_from_rt, expected, "roundtrip");
}