Skip to content

Commit

Permalink
Some clippy assisted cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusklaas committed Mar 14, 2019
1 parent 0e25479 commit 49c7fb3
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 55 deletions.
4 changes: 2 additions & 2 deletions src/entities.rs
Expand Up @@ -22,7 +22,7 @@

// Autogenerated by mk_entities.py

const ENTITIES: [&'static str; 2125] = [
const ENTITIES: [&str; 2125] = [
"AElig",
"AMP",
"Aacute",
Expand Down Expand Up @@ -2150,7 +2150,7 @@ const ENTITIES: [&'static str; 2125] = [
"zwnj",
];

const ENTITY_VALUES: [&'static str; 2125] = [
const ENTITY_VALUES: [&str; 2125] = [
"\u{00C6}",
"\u{0026}",
"\u{00C1}",
Expand Down
8 changes: 4 additions & 4 deletions src/html.rs
Expand Up @@ -70,7 +70,7 @@ where
if write_newline {
self.write_newline()
} else {
if bytes.len() > 0 {
if !bytes.is_empty() {
self.end_newline = bytes[bytes.len() - 1] == b'\n';
}
Ok(())
Expand Down Expand Up @@ -324,7 +324,7 @@ where
}

// run raw text, consuming end tag
fn raw_text<'c>(&mut self) -> io::Result<()> {
fn raw_text(&mut self) -> io::Result<()> {
let mut nest = 0;
while let Some(event) = self.iter.next() {
match event {
Expand Down Expand Up @@ -429,8 +429,8 @@ where
W: Write,
{
let writer = HtmlWriter {
iter: iter,
writer: writer,
iter,
writer,
end_newline: true,
table_state: TableState::Head,
table_alignments: vec![],
Expand Down
4 changes: 2 additions & 2 deletions src/linklabel.rs
Expand Up @@ -32,7 +32,7 @@ pub enum ReferenceLabel<'a> {
pub type LinkLabel<'a> = UniCase<CowStr<'a>>;

/// Returns number of bytes (including brackets) and label on success.
pub(crate) fn scan_link_label<'a>(text: &'a str) -> Option<(usize, ReferenceLabel<'a>)> {
pub(crate) fn scan_link_label(text: &str) -> Option<(usize, ReferenceLabel)> {
if text.len() < 2 || text.as_bytes()[0] != b'[' { return None; }
let pair = if b'^' == text.as_bytes()[1] {
let (byte_index, cow) = scan_link_label_rest(&text[2..])?;
Expand All @@ -46,7 +46,7 @@ pub(crate) fn scan_link_label<'a>(text: &'a str) -> Option<(usize, ReferenceLabe

/// Assumes the opening bracket has already been scanned.
/// Returns the number of bytes read (including closing bracket) and label on success.
pub(crate) fn scan_link_label_rest<'a>(text: &'a str) -> Option<(usize, CowStr<'a>)> {
pub(crate) fn scan_link_label_rest(text: &str) -> Option<(usize, CowStr)> {
let mut char_iter = text.chars().peekable();
let mut byte_index = 0;
let mut only_white_space = true;
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Expand Up @@ -91,7 +91,7 @@ struct Spec<'a> {

impl<'a> Spec<'a> {
pub fn new(spec: &'a str) -> Self {
Spec{ spec: spec, test_n: 0 }
Spec{ spec, test_n: 0 }
}
}

Expand All @@ -103,7 +103,7 @@ struct TestCase<'a> {

impl<'a> TestCase<'a> {
pub fn new(n: usize, input: &'a str, expected: &'a str) -> Self {
TestCase { n: n, input: input, expected: expected }
TestCase { n, input, expected }
}
}

Expand Down
46 changes: 23 additions & 23 deletions src/parse.rs
Expand Up @@ -494,7 +494,7 @@ impl<'a> FirstPass<'a> {
if n_containers == self.tree.spine_len() {
if let Some((n, level)) = scan_setext_heading(&self.text[ix_new..]) {
self.tree[node_ix].item.body = ItemBody::Header(level);
if let Some(Item { start, end: _, body: ItemBody::HardBreak }) = brk {
if let Some(Item { start, body: ItemBody::HardBreak, .. }) = brk {
if self.text.as_bytes()[start] == b'\\' {
self.tree.append_text(start, start + 1);
}
Expand Down Expand Up @@ -538,7 +538,7 @@ impl<'a> FirstPass<'a> {
b'\n' | b'\r' => {
let mut i = ix;
let eol_bytes = scan_eol(&self.text[ix..]).0;
if ix >= begin_text + 1 && bytes[ix - 1] == b'\\' && ix + eol_bytes < self.text.len() {
if ix > begin_text && bytes[ix - 1] == b'\\' && ix + eol_bytes < self.text.len() {
i -= 1;
self.tree.append_text(begin_text, i);
ix += eol_bytes;
Expand Down Expand Up @@ -778,7 +778,7 @@ impl<'a> FirstPass<'a> {
ix = next_line_ix;
remaining_space = line_start.remaining_space();
}
&self.pop(end_ix);
self.pop(end_ix);
ix
}

Expand Down Expand Up @@ -926,7 +926,7 @@ impl<'a> FirstPass<'a> {
fn append_code_text(&mut self, remaining_space: usize, start: usize, end: usize) {
if remaining_space > 0 {
self.tree.append(Item {
start: start,
start,
end: start,
body: ItemBody::SynthesizeText(" "[..remaining_space].into()),
});
Expand All @@ -945,7 +945,7 @@ impl<'a> FirstPass<'a> {
fn append_html_line(&mut self, remaining_space: usize, start: usize, end: usize) {
if remaining_space > 0 {
self.tree.append(Item {
start: start,
start,
end: start,
// TODO: maybe this should synthesize to html rather than text?
body: ItemBody::SynthesizeText(" "[..remaining_space].into()),
Expand All @@ -954,19 +954,19 @@ impl<'a> FirstPass<'a> {
if self.text.as_bytes()[end - 2] == b'\r' {
// Normalize CRLF to LF
self.tree.append(Item {
start: start,
start,
end: end - 2,
body: ItemBody::Html,
});
self.tree.append(Item {
start: end - 1,
end: end,
end,
body: ItemBody::Html,
});
} else {
self.tree.append(Item {
start: start,
end: end,
start,
end,
body: ItemBody::Html,
});
}
Expand Down Expand Up @@ -1050,7 +1050,7 @@ impl<'a> FirstPass<'a> {
self.finish_list(start);
}
self.tree.append(Item {
start: start,
start,
end: 0, // will get set later
body: ItemBody::List(true, ch, index),
});
Expand Down Expand Up @@ -1141,7 +1141,7 @@ impl<'a> FirstPass<'a> {
}
i += 1;
self.tree.append(Item {
start: start,
start,
end: 0, // will get set later
body: ItemBody::FootnoteDefinition(label.clone()), // TODO: check whether the label here is strictly necessary
});
Expand Down Expand Up @@ -1339,7 +1339,7 @@ fn count_header_cols(text: &str, mut pipes: usize, mut start: usize, last_pipe_i
pipes + 1
}

fn unescape_cow<'a>(c: CowStr<'a>) -> CowStr<'a> {
fn unescape_cow(c: CowStr) -> CowStr {
match c {
CowStr::Inlined(s) => unescape_str_line(s),
CowStr::Boxed(s) => unescape_str_line(s),
Expand Down Expand Up @@ -1371,8 +1371,8 @@ impl<'a> Tree<Item<'a>> {
}
}
self.append(Item {
start: start,
end: end,
start,
end,
body: ItemBody::Text,
});
}
Expand Down Expand Up @@ -1449,9 +1449,9 @@ fn get_html_end_tag(text : &str) -> Option<&'static str> {

// TODO: Consider using `strcasecmp` here
'type_1: for (beg_tag, end_tag) in BEGIN_TAGS.iter().zip(END_TAGS.iter()) {
if text.len() >= beg_tag.len() && text.starts_with("<") {
if text.len() >= beg_tag.len() && text.starts_with('<') {
for (i, c) in beg_tag.as_bytes()[1..].iter().enumerate() {
if ! (&text.as_bytes()[i+1] == c || &text.as_bytes()[i+1] == &(c - 32)) {
if ! (&text.as_bytes()[i+1] == c || text.as_bytes()[i+1] == c - 32) {
continue 'type_1;
}
}
Expand Down Expand Up @@ -2307,9 +2307,9 @@ impl<'a> Parser<'a> {
cur = TreePointer::Valid(tos.node);
cur_ix = tos.node;
self.tree[cur_ix].item.body = if tos.ty == LinkStackTy::Image {
ItemBody::Image(LinkType::Inline, url.into(), title.into())
ItemBody::Image(LinkType::Inline, url, title)
} else {
ItemBody::Link(LinkType::Inline, url.into(), title.into())
ItemBody::Link(LinkType::Inline, url, title)
};
self.tree[cur_ix].child = self.tree[cur_ix].next;
self.tree[cur_ix].next = next_node;
Expand Down Expand Up @@ -2368,7 +2368,7 @@ impl<'a> Parser<'a> {
else if let Some(matching_def) = label.and_then(|l| match l { ReferenceLabel::Link(l) => Some(l), _ => None, })
.and_then(|l| self.refdefs.get(&UniCase::new(l))) {
// found a matching definition!
let title = matching_def.title.as_ref().cloned().unwrap_or("".into());
let title = matching_def.title.as_ref().cloned().unwrap_or_else(|| "".into());
let url = matching_def.dest.clone();
self.tree[tos.node].item.body = if tos.ty == LinkStackTy::Image {
ItemBody::Image(link_type, url, title)
Expand Down Expand Up @@ -2469,7 +2469,7 @@ impl<'a> Parser<'a> {
start: el.start,
count: el.count - match_count,
c: el.c,
both: both,
both,
})
}
count -= match_count;
Expand All @@ -2484,9 +2484,9 @@ impl<'a> Parser<'a> {
if can_open {
stack.push(InlineEl {
start: cur_ix,
count: count,
c: c,
both: both,
count,
c,
both,
});
} else {
for i in 0..count {
Expand Down
30 changes: 10 additions & 20 deletions src/scanners.rs
Expand Up @@ -29,7 +29,7 @@ use crate::strings::CowStr;
pub use crate::puncttable::{is_ascii_punctuation, is_punctuation};

// sorted for binary search
const HTML_TAGS: [&'static str; 47] = ["article", "aside", "blockquote",
const HTML_TAGS: [&str; 47] = ["article", "aside", "blockquote",
"body", "button", "canvas", "caption", "col", "colgroup", "dd", "div",
"dl", "dt", "embed", "fieldset", "figcaption", "figure", "footer", "form",
"h1", "h2", "h3", "h4", "h5", "h6", "header", "hgroup", "hr", "iframe",
Expand Down Expand Up @@ -106,7 +106,7 @@ impl<'a> LineStart<'a> {
}

/// Determine whether we're at end of line (includes end of file).
pub fn is_at_eol(&mut self) -> bool {
pub fn is_at_eol(&self) -> bool {
if self.ix == self.text.len() {
return true;
}
Expand Down Expand Up @@ -157,12 +157,12 @@ impl<'a> LineStart<'a> {
} else if c >= b'0' && c <= b'9' {
let start_ix = self.ix;
let mut ix = self.ix + 1;
let mut val = (c - b'0') as u64;
let mut val = u64::from(c - b'0');
while ix < self.text.len() && ix - start_ix < 10 {
let c = self.text.as_bytes()[ix];
ix += 1;
if c >= b'0' && c <= b'9' {
val = val * 10 + (c - b'0') as u64;
val = val * 10 + u64::from(c - b'0');
} else if c == b')' || c == b'.' {
self.ix = ix;
let val_usize = val as usize;
Expand Down Expand Up @@ -315,8 +315,7 @@ pub fn scan_closing_code_fence(text: &str, fence_char: u8, n_fence_char: usize)
i += num_fence_chars_found;
let num_trailing_spaces = scan_ch_repeat(&text[i..], b' ');
i += num_trailing_spaces;
if scan_eol(&text[i..]).1 { return Some(i); }
return None;
if scan_eol(&text[i..]).1 { Some(i) } else { None }
}

// returned pair is (number of bytes, number of spaces)
Expand Down Expand Up @@ -533,7 +532,7 @@ pub fn scan_listitem(data: &str) -> (usize, u8, usize, usize) {
postn = 1;
postindent = 1;
}
if let Some(_) = scan_blank_line(&data[w..]) {
if scan_blank_line(&data[w..]).is_some() {
postn = 0;
postindent = 1;
}
Expand Down Expand Up @@ -670,12 +669,12 @@ pub fn scan_attribute_value(data: &str) -> Option<usize> {
}
}
if i >= data.len() { return None; }
return Some(i);
Some(i)

}

// Remove backslash escapes and resolve entities
pub fn unescape<'a>(input: &'a str) -> CowStr<'a> {
pub fn unescape(input: &str) -> CowStr {
let mut result = String::new();
let mut mark = 0;
let mut i = 0;
Expand Down Expand Up @@ -775,11 +774,7 @@ pub fn scan_html_type_7(data: &str) -> Option<usize> {
}
i += c;

if let Some(_) = scan_blank_line(&data[i..]) {
return Some(i);
} else {
return None;
}
scan_blank_line(&data[i..]).map(|_| i)
}

pub fn scan_attribute(data: &str) -> Option<usize> {
Expand All @@ -789,12 +784,7 @@ pub fn scan_attribute(data: &str) -> Option<usize> {
} else {
return None;
}
if let Some(attr_valspec_bytes) = scan_attribute_value_spec(&data[i..]) {
i += attr_valspec_bytes;
} else {
return None;
}
return Some(i);
scan_attribute_value_spec(&data[i..]).map(|attr_valspec_bytes| attr_valspec_bytes + i)
}

pub fn scan_attribute_value_spec(data: &str) -> Option<usize> {
Expand Down
2 changes: 1 addition & 1 deletion src/strings.rs
Expand Up @@ -142,7 +142,7 @@ impl<'a> Borrow<str> for CowStr<'a> {
}

impl<'a> CowStr<'a> {
pub fn to_string(self) -> String {
pub fn into_string(self) -> String {
match self {
CowStr::Boxed(b) => b.into(),
CowStr::Borrowed(b) => b.to_owned(),
Expand Down
2 changes: 1 addition & 1 deletion src/tree.rs
Expand Up @@ -32,7 +32,7 @@ impl TreeIndex {
TreeIndex(NonZeroUsize::new(i).unwrap())
}

pub fn get(&self) -> usize {
pub fn get(self) -> usize {
self.0.get()
}
}
Expand Down

0 comments on commit 49c7fb3

Please sign in to comment.