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

clippy fixes #80

Merged
merged 2 commits into from
Feb 28, 2017
Merged
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
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
// #![deny(missing_docs)]
#![cfg_attr(feature="dev", warn(warnings))]

// Allow zero pointers for lazy_static. Otherwise clippy will complain.
#![allow(unknown_lints)]
#![allow(zero_ptr)]

#[macro_use]
extern crate lazy_static;
extern crate regex;
Expand Down Expand Up @@ -102,7 +106,7 @@ pub type Tag = Fn(&str, &[Token], &LiquidOptions) -> Result<Box<Renderable>>;
/// of the block, the argument [Tokens](lexer/enum.Token.html) passed to
/// the block, a Vec of all [Elements](lexer/enum.Element.html) inside the block and
/// the global [`LiquidOptions`](struct.LiquidOptions.html).
pub type Block = Fn(&str, &[Token], Vec<Element>, &LiquidOptions) -> Result<Box<Renderable>>;
pub type Block = Fn(&str, &[Token], &[Element], &LiquidOptions) -> Result<Box<Renderable>>;

/// Any object (tag/block) that can be rendered by liquid must implement this trait.
pub trait Renderable: Send + Sync {
Expand Down
16 changes: 8 additions & 8 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn parse_output(tokens: &[Token]) -> Result<Output> {
iter.next();

while iter.peek() != None {
try!(expect(&mut iter, Pipe));
try!(expect(&mut iter, &Pipe));

let name = match iter.next() {
Some(&Identifier(ref name)) => name,
Expand All @@ -83,7 +83,7 @@ pub fn parse_output(tokens: &[Token]) -> Result<Output> {
_ => (),
}

try!(expect(&mut iter, Colon));
try!(expect(&mut iter, &Colon));

// loops through the argument list after the filter name
while iter.peek() != None && iter.peek().unwrap() != &&Pipe {
Expand Down Expand Up @@ -161,7 +161,7 @@ fn parse_tag(iter: &mut Iter<Element>,
};
children.push(t.clone())
}
options.blocks[x](x, &tokens[1..], children, options)
options.blocks[x](x, &tokens[1..], &children, options)
}

ref x => Err(Error::Parser(format!("parse_tag: {:?} not implemented", x))),
Expand All @@ -170,11 +170,11 @@ fn parse_tag(iter: &mut Iter<Element>,

/// Confirm that the next token in a token stream is what you want it
/// to be. The token iterator is moved to the next token in the stream.
pub fn expect<'a, T>(tokens: &mut T, expected: Token) -> Result<&'a Token>
pub fn expect<'a, T>(tokens: &mut T, expected: &Token) -> Result<&'a Token>
where T: Iterator<Item = &'a Token>
{
match tokens.next() {
Some(x) if *x == expected => Ok(x),
Some(x) if x == expected => Ok(x),
x => Error::parser(&expected.to_string(), x),
}
}
Expand Down Expand Up @@ -314,9 +314,9 @@ mod test {
let token_vec = vec![Pipe, Dot, Colon];
let mut tokens = token_vec.iter();

assert!(expect(&mut tokens, Pipe).is_ok());
assert!(expect(&mut tokens, Dot).is_ok());
assert!(expect(&mut tokens, Comma).is_err());
assert!(expect(&mut tokens, &Pipe).is_ok());
assert!(expect(&mut tokens, &Dot).is_ok());
assert!(expect(&mut tokens, &Comma).is_err());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/tags/assign_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn assign_tag(_tag_name: &str,
x => return Error::parser("Identifier", x),
};

try!(expect(&mut args, Assignment));
try!(expect(&mut args, &Assignment));

let src = try!(parse_output(&arguments[2..]));

Expand Down
2 changes: 1 addition & 1 deletion src/tags/capture_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Renderable for Capture {

pub fn capture_block(_tag_name: &str,
arguments: &[Token],
tokens: Vec<Element>,
tokens: &[Element],
options: &LiquidOptions)
-> Result<Box<Renderable>> {
let mut args = arguments.iter();
Expand Down
2 changes: 1 addition & 1 deletion src/tags/case_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn parse_condition(element: &Element) -> Result<Conditional> {

pub fn case_block(_tag_name: &str,
arguments: &[Token],
tokens: Vec<Element>,
tokens: &[Element],
options: &LiquidOptions)
-> Result<Box<Renderable>> {
let delims = &["when", "else"];
Expand Down
4 changes: 2 additions & 2 deletions src/tags/comment_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Renderable for Comment {

pub fn comment_block(_tag_name: &str,
_arguments: &[Token],
_tokens: Vec<Element>,
_tokens: &[Element],
_options: &LiquidOptions)
-> Result<Box<Renderable>> {
Ok(Box::new(Comment))
Expand All @@ -33,7 +33,7 @@ mod test {
let options: LiquidOptions = Default::default();
let comment = comment_block("comment",
&[],
vec![Expression(vec![], "This is a test".to_string())],
&vec![Expression(vec![], "This is a test".to_string())],
&options);
assert_eq!(comment.unwrap().render(&mut Default::default()).unwrap(),
None);
Expand Down
46 changes: 22 additions & 24 deletions src/tags/for_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl Renderable for For {

/// Extracts an attribute with an integer value from the token stream
fn int_attr(args: &mut Iter<Token>) -> Result<Option<usize>> {
try!(expect(args, Colon));
try!(expect(args, &Colon));
match args.next() {
Some(&NumberLiteral(ref n)) => Ok(Some(*n as usize)),
x => Error::parser("number", x),
Expand All @@ -137,7 +137,7 @@ fn range_end_point(args: &mut Iter<Token>) -> Result<Token> {

pub fn for_block(_tag_name: &str,
arguments: &[Token],
tokens: Vec<Element>,
tokens: &[Element],
options: &LiquidOptions)
-> Result<Box<Renderable>> {
let mut args = arguments.iter();
Expand All @@ -146,19 +146,19 @@ pub fn for_block(_tag_name: &str,
x => return Error::parser("Identifier", x),
};

try!(expect(&mut args, Identifier("in".to_owned())));
try!(expect(&mut args, &Identifier("in".to_owned())));

let range = match args.next() {
Some(&Identifier(ref x)) => Range::Array(x.clone()),
Some(&OpenRound) => {
// this might be a range, let's try and see
let start = try!(range_end_point(&mut args));

try!(expect(&mut args, DotDot));
try!(expect(&mut args, &DotDot));

let stop = try!(range_end_point(&mut args));

try!(expect(&mut args, CloseRound));
try!(expect(&mut args, &CloseRound));

Range::Counted(start, stop)
}
Expand All @@ -184,7 +184,7 @@ pub fn for_block(_tag_name: &str,
}
}

let (leading, trailing) = split_block(&tokens, &["else"], options);
let (leading, trailing) = split_block(tokens, &["else"], options);
let item_template = Template::new(try!(parse(leading, options)));

let else_template = match trailing {
Expand Down Expand Up @@ -225,7 +225,7 @@ mod test {
&[Identifier("name".to_owned()),
Identifier("in".to_owned()),
Identifier("array".to_owned())],
tokenize("test {{name}} ").unwrap(),
&tokenize("test {{name}} ").unwrap(),
&options);

let mut data: Context = Default::default();
Expand All @@ -250,7 +250,7 @@ mod test {
DotDot,
NumberLiteral(46f32),
CloseRound],
tokenize("#{{forloop.index}} test {{name}} | ").unwrap(),
&tokenize("#{{forloop.index}} test {{name}} | ").unwrap(),
&options);

let mut data: Context = Default::default();
Expand Down Expand Up @@ -420,14 +420,14 @@ mod test {
DotDot,
NumberLiteral(103f32),
CloseRound],
tokenize(concat!("length: {{forloop.length}}, ",
"index: {{forloop.index}}, ",
"index0: {{forloop.index0}}, ",
"rindex: {{forloop.rindex}}, ",
"rindex0: {{forloop.rindex0}}, ",
"value: {{v}}, ",
"first: {{forloop.first}}, ",
"last: {{forloop.last}}\n"))
&tokenize(concat!("length: {{forloop.length}}, ",
"index: {{forloop.index}}, ",
"index0: {{forloop.index0}}, ",
"rindex: {{forloop.rindex}}, ",
"rindex0: {{forloop.rindex0}}, ",
"value: {{v}}, ",
"first: {{forloop.first}}, ",
"last: {{forloop.last}}\n"))
.unwrap(),
&options);

Expand All @@ -451,18 +451,16 @@ mod test {
&[Identifier("name".to_owned()),
Identifier("in".to_owned()),
Identifier("array".to_owned())],
tokenize("test {{name | shout}} ").unwrap(),
&tokenize("test {{name | shout}} ").unwrap(),
&options);

let mut data: Context = Default::default();
data.add_filter("shout",
Box::new(|input, _args| {
if let &Value::Str(ref s) = input {
Ok(Value::Str(s.to_uppercase()))
} else {
FilterError::invalid_type("Expected a string")
}
}));
Box::new(|input, _args| if let &Value::Str(ref s) = input {
Ok(Value::Str(s.to_uppercase()))
} else {
FilterError::invalid_type("Expected a string")
}));

data.set_val("array",
Value::Array(vec![Value::str("alpha"),
Expand Down
6 changes: 3 additions & 3 deletions src/tags/if_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn condition(arguments: &[Token]) -> Result<Condition> {

pub fn unless_block(_tag_name: &str,
arguments: &[Token],
tokens: Vec<Element>,
tokens: &[Element],
options: &LiquidOptions)
-> Result<Box<Renderable>> {
let cond = try!(condition(arguments));
Expand All @@ -99,7 +99,7 @@ pub fn unless_block(_tag_name: &str,

pub fn if_block(_tag_name: &str,
arguments: &[Token],
tokens: Vec<Element>,
tokens: &[Element],
options: &LiquidOptions)
-> Result<Box<Renderable>> {
let cond = try!(condition(arguments));
Expand All @@ -119,7 +119,7 @@ pub fn if_block(_tag_name: &str,
.skip(1)
.cloned()
.collect();
let parsed = try!(if_block("if", &split.args[1..], child_tokens, options));
let parsed = try!(if_block("if", &split.args[1..], &child_tokens, options));
Some(Template::new(vec![parsed]))
}

Expand Down
4 changes: 2 additions & 2 deletions src/tags/interrupt_tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn break_tag(_tag_name: &str,
-> Result<Box<Renderable>> {

// no arguments should be supplied, trying to supply them is an error
if arguments.len() > 0 {
if !arguments.is_empty() {
return Error::parser("%}", arguments.first());
}
Ok(Box::new(Break))
Expand All @@ -39,7 +39,7 @@ pub fn continue_tag(_tag_name: &str,
_options: &LiquidOptions)
-> Result<Box<Renderable>> {
// no arguments should be supplied, trying to supply them is an error
if arguments.len() > 0 {
if !arguments.is_empty() {
return Error::parser("%}", arguments.first());
}
Ok(Box::new(Continue))
Expand Down
4 changes: 2 additions & 2 deletions src/tags/raw_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Renderable for RawT {

pub fn raw_block(_tag_name: &str,
_arguments: &[Token],
tokens: Vec<Element>,
tokens: &[Element],
_options: &LiquidOptions)
-> Result<Box<Renderable>> {
let content = tokens.iter().fold("".to_owned(), |a, b| {
Expand All @@ -38,7 +38,7 @@ fn test_raw() {
let options: LiquidOptions = Default::default();
let raw = raw_block("raw",
&[],
vec![Expression(vec![], "This is a test".to_owned())],
&vec![Expression(vec![], "This is a test".to_owned())],
&options);
assert_eq!(raw.unwrap().render(&mut Default::default()).unwrap(),
Some("This is a test".to_owned()));
Expand Down
8 changes: 3 additions & 5 deletions tests/custom_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ fn run() {
-> Result<Box<Renderable>, Error> {

let numbers = arguments.iter()
.filter_map(|x| {
match x {
&Token::NumberLiteral(ref num) => Some(*num),
_ => None,
}
.filter_map(|x| match x {
&Token::NumberLiteral(ref num) => Some(*num),
_ => None,
})
.collect();
Ok(Box::new(Multiply { numbers: numbers }))
Expand Down