diff options
-rw-r--r-- | askama_derive/src/generator.rs | 2 | ||||
-rw-r--r-- | askama_derive/src/input.rs | 2 | ||||
-rw-r--r-- | askama_derive/src/lib.rs | 1 | ||||
-rw-r--r-- | askama_derive/src/parser.rs | 29 | ||||
-rw-r--r-- | askama_shared/src/filters/mod.rs | 57 | ||||
-rw-r--r-- | askama_shared/src/lib.rs | 13 | ||||
-rw-r--r-- | testing/benches/all.rs | 12 | ||||
-rw-r--r-- | testing/tests/filters.rs | 4 | ||||
-rw-r--r-- | testing/tests/matches.rs | 2 | ||||
-rw-r--r-- | testing/tests/operators.rs | 2 | ||||
-rw-r--r-- | testing/tests/simple.rs | 2 | ||||
-rw-r--r-- | testing/tests/vars.rs | 2 |
12 files changed, 68 insertions, 60 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 359de5a..81f6ac9 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -450,6 +450,7 @@ impl<'a> Generator<'a> { flushed + median(&mut arm_sizes) } + #[allow(clippy::too_many_arguments)] fn write_match( &mut self, ctx: &'a Context, @@ -531,6 +532,7 @@ impl<'a> Generator<'a> { flushed + median(&mut arm_sizes) } + #[allow(clippy::too_many_arguments)] fn write_loop( &mut self, ctx: &'a Context, diff --git a/askama_derive/src/input.rs b/askama_derive/src/input.rs index 3b65640..6a54a99 100644 --- a/askama_derive/src/input.rs +++ b/askama_derive/src/input.rs @@ -155,7 +155,7 @@ impl<'a> TemplateInput<'a> { config .syntaxes .get(&s) - .expect(&format!("attribute syntax {} not exist", s)) + .unwrap_or_else(|| panic!("attribute syntax {} not exist", s)) }, ); diff --git a/askama_derive/src/lib.rs b/askama_derive/src/lib.rs index 2c02cb5..97228fd 100644 --- a/askama_derive/src/lib.rs +++ b/askama_derive/src/lib.rs @@ -199,6 +199,7 @@ impl<'a> Context<'a> { } } +#[allow(clippy::match_wild_err_arm)] fn get_template_source(tpl_path: &Path) -> String { match fs::read_to_string(tpl_path) { Err(_) => panic!( diff --git a/askama_derive/src/parser.rs b/askama_derive/src/parser.rs index d22ac5a..e483ff7 100644 --- a/askama_derive/src/parser.rs +++ b/askama_derive/src/parser.rs @@ -126,17 +126,15 @@ fn split_ws_parts(s: &[u8]) -> Node { let is_ws = |c: &u8| *c != b' ' && *c != b'\t' && *c != b'\r' && *c != b'\n'; let start = s.iter().position(&is_ws); - let res = if start.is_none() { - (s, &s[0..0], &s[0..0]) - } else { - let start = start.unwrap(); + let res = if let Some(start) = start { let end = s.iter().rposition(&is_ws); - if end.is_none() { - (&s[..start], &s[start..], &s[0..0]) - } else { - let end = end.unwrap(); + if let Some(end) = end { (&s[..start], &s[start..=end], &s[end + 1..]) + } else { + (&s[..start], &s[start..], &s[0..0]) } + } else { + (s, &s[0..0], &s[0..0]) }; Node::Lit( @@ -154,10 +152,7 @@ enum ContentState { End(usize), } -fn take_content<'a>( - i: &'a [u8], - s: &'a Syntax<'a>, -) -> Result<(&'a [u8], Node<'a>), nom::Err<(&'a [u8], nom::error::ErrorKind)>> { +fn take_content<'a>(i: &'a [u8], s: &'a Syntax<'a>) -> ParserError<'a, Node<'a>> { use crate::parser::ContentState::*; let bs = s.block_start.as_bytes()[0]; let be = s.block_start.as_bytes()[1]; @@ -200,7 +195,7 @@ fn take_content<'a>( } } -fn identifier(input: &[u8]) -> Result<(&[u8], &str), nom::Err<(&[u8], nom::error::ErrorKind)>> { +fn identifier(input: &[u8]) -> ParserError<&str> { if !nom::character::is_alphabetic(input[0]) && input[0] != b'_' && !non_ascii(input[0]) { return Err(nom::Err::Error(error_position!( input, @@ -319,7 +314,7 @@ fn macro_arguments(i: &[u8]) -> IResult<&[u8], &str> { delimited(char('('), nested_parenthesis, char(')'))(i) } -fn nested_parenthesis(i: &[u8]) -> Result<(&[u8], &str), nom::Err<(&[u8], nom::error::ErrorKind)>> { +fn nested_parenthesis(i: &[u8]) -> ParserError<&str> { let mut nested = 0; let mut last = 0; let mut in_str = false; @@ -442,8 +437,8 @@ fn expr_attr(i: &[u8]) -> IResult<&[u8], Expr> { let mut res = obj; for (aname, args) in attrs { - res = if args.is_some() { - Expr::MethodCall(Box::new(res), aname, args.unwrap()) + res = if let Some(args) = args { + Expr::MethodCall(Box::new(res), aname, args) } else { Expr::Attr(Box::new(res), aname) }; @@ -1056,3 +1051,5 @@ mod tests { super::parse("{~ strvar|e ~}", &syntax); } } + +type ParserError<'a, T> = Result<(&'a [u8], T), nom::Err<(&'a [u8], nom::error::ErrorKind)>>; diff --git a/askama_shared/src/filters/mod.rs b/askama_shared/src/filters/mod.rs index 7e7bc40..4484ce4 100644 --- a/askama_shared/src/filters/mod.rs +++ b/askama_shared/src/filters/mod.rs @@ -3,6 +3,7 @@ //! Contains all the built-in filter functions for use in templates. //! You can define your own filters; for more information, //! see the top-level crate documentation. +#![allow(clippy::trivially_copy_pass_by_ref)] #[cfg(feature = "serde_json")] mod json; @@ -246,25 +247,25 @@ pub fn capitalize(s: &dyn fmt::Display) -> Result<String> { } /// Centers the value in a field of a given width -pub fn center(s: &dyn fmt::Display, l: usize) -> Result<String> { - let s = s.to_string(); - let len = s.len(); +pub fn center(src: &dyn fmt::Display, dst_len: usize) -> Result<String> { + let src = src.to_string(); + let len = src.len(); - if l <= len { - Ok(s) + if dst_len <= len { + Ok(src) } else { - let p = l - len; - let q = p / 2; - let r = p % 2; - let mut buf = String::with_capacity(l); + let diff = dst_len - len; + let mid = diff / 2; + let r = diff % 2; + let mut buf = String::with_capacity(dst_len); - for _ in 0..q { + for _ in 0..mid { buf.push(' '); } - buf.push_str(&s); + buf.push_str(&src); - for _ in 0..q + r { + for _ in 0..mid + r { buf.push(' '); } @@ -347,6 +348,7 @@ mod tests { } #[test] + #[allow(clippy::float_cmp)] fn test_into_f64() { assert_eq!(into_f64(1).unwrap(), 1.0 as f64); assert_eq!(into_f64(1.9).unwrap(), 1.9 as f64); @@ -363,51 +365,46 @@ mod tests { assert_eq!(into_isize(1.5 as f64).unwrap(), 1 as isize); assert_eq!(into_isize(-1.5 as f64).unwrap(), -1 as isize); match into_isize(INFINITY) { - Err(Fmt(fmt::Error)) => assert!(true), - _ => assert!(false, "Should return error of type Err(Fmt(fmt::Error))"), + Err(Fmt(fmt::Error)) => {} + _ => panic!("Should return error of type Err(Fmt(fmt::Error))"), }; } #[test] fn test_join() { assert_eq!( - join((&["hello", "world"]).into_iter(), ", ").unwrap(), + join((&["hello", "world"]).iter(), ", ").unwrap(), "hello, world" ); - assert_eq!(join((&["hello"]).into_iter(), ", ").unwrap(), "hello"); + assert_eq!(join((&["hello"]).iter(), ", ").unwrap(), "hello"); let empty: &[&str] = &[]; - assert_eq!(join(empty.into_iter(), ", ").unwrap(), ""); + assert_eq!(join(empty.iter(), ", ").unwrap(), ""); let input: Vec<String> = vec!["foo".into(), "bar".into(), "bazz".into()]; assert_eq!( - join((&input).into_iter(), ":".to_string()).unwrap(), - "foo:bar:bazz" - ); - assert_eq!( - join(input.clone().into_iter(), ":").unwrap(), - "foo:bar:bazz" - ); - assert_eq!( - join(input.clone().into_iter(), ":".to_string()).unwrap(), + join((&input).iter(), ":".to_string()).unwrap(), "foo:bar:bazz" ); + assert_eq!(join(input.clone().iter(), ":").unwrap(), "foo:bar:bazz"); + assert_eq!(join(input.iter(), ":".to_string()).unwrap(), "foo:bar:bazz"); let input: &[String] = &["foo".into(), "bar".into()]; - assert_eq!(join(input.into_iter(), ":").unwrap(), "foo:bar"); - assert_eq!(join(input.into_iter(), ":".to_string()).unwrap(), "foo:bar"); + assert_eq!(join(input.iter(), ":").unwrap(), "foo:bar"); + assert_eq!(join(input.iter(), ":".to_string()).unwrap(), "foo:bar"); let real: String = "blah".into(); let input: Vec<&str> = vec![&real]; - assert_eq!(join(input.into_iter(), ";").unwrap(), "blah"); + assert_eq!(join(input.iter(), ";").unwrap(), "blah"); assert_eq!( - join((&&&&&["foo", "bar"]).into_iter(), ", ").unwrap(), + join((&&&&&["foo", "bar"]).iter(), ", ").unwrap(), "foo, bar" ); } #[test] + #[allow(clippy::float_cmp)] fn test_abs() { assert_eq!(abs(1).unwrap(), 1); assert_eq!(abs(-1).unwrap(), 1); diff --git a/askama_shared/src/lib.rs b/askama_shared/src/lib.rs index 71543fc..61def8a 100644 --- a/askama_shared/src/lib.rs +++ b/askama_shared/src/lib.rs @@ -36,7 +36,7 @@ impl<'a> Config<'a> { syntaxes.insert(DEFAULT_SYNTAX_NAME.to_string(), Syntax::default()); let raw: RawConfig<'_> = - toml::from_str(&s).expect(&format!("invalid TOML in {}", CONFIG_FILE_NAME)); + toml::from_str(&s).unwrap_or_else(|_| panic!("invalid TOML in {}", CONFIG_FILE_NAME)); let (dirs, default_syntax) = match raw.general { Some(General { @@ -75,14 +75,14 @@ impl<'a> Config<'a> { escaper .extensions .iter() - .map(|ext| ext.to_string()) + .map(|ext| (*ext).to_string()) .collect(), escaper.path.to_string(), )); } } for (extensions, path) in DEFAULT_ESCAPERS { - escapers.push((str_set(extensions), path.to_string())); + escapers.push((str_set(extensions), (*path).to_string())); } Config { @@ -97,7 +97,7 @@ impl<'a> Config<'a> { if let Some(root) = start_at { let relative = root.with_file_name(path); if relative.exists() { - return relative.to_owned(); + return relative; } } @@ -166,7 +166,7 @@ impl<'a> From<RawSyntax<'a>> for Syntax<'a> { let ce = syntax.comment_start.as_bytes()[1]; let es = syntax.block_start.as_bytes()[0]; let ee = syntax.block_start.as_bytes()[1]; - if !(bs == cs && bs == es) && !(be == ce && be == ee) { + if !((bs == cs && bs == es) || (be == ce && be == ee)) { panic!("bad delimiters block_start: {}, comment_start: {}, expr_start: {}, needs one of the two characters in common", syntax.block_start, syntax.comment_start, syntax.expr_start); } @@ -211,7 +211,7 @@ pub fn read_config_file() -> String { let filename = root.join(CONFIG_FILE_NAME); if filename.exists() { fs::read_to_string(&filename) - .expect(&format!("unable to read {}", filename.to_str().unwrap())) + .unwrap_or_else(|_| panic!("unable to read {}", filename.to_str().unwrap())) } else { "".to_string() } @@ -233,6 +233,7 @@ static DEFAULT_ESCAPERS: &[(&[&str], &str)] = &[ ]; #[cfg(test)] +#[allow(clippy::blacklisted_name)] mod tests { use super::*; use std::env; diff --git a/testing/benches/all.rs b/testing/benches/all.rs index 3ce0812..9d605af 100644 --- a/testing/benches/all.rs +++ b/testing/benches/all.rs @@ -8,15 +8,15 @@ criterion_main!(benches); criterion_group!(benches, functions); fn functions(c: &mut Criterion) { - c.bench_function("Big table", |b| big_table(b, &100)); + c.bench_function("Big table", |b| big_table(b, 100)); c.bench_function("Teams", teams); } -fn big_table(b: &mut criterion::Bencher, size: &usize) { - let mut table = Vec::with_capacity(*size); - for _ in 0..*size { - let mut inner = Vec::with_capacity(*size); - for i in 0..*size { +fn big_table(b: &mut criterion::Bencher, size: usize) { + let mut table = Vec::with_capacity(size); + for _ in 0..size { + let mut inner = Vec::with_capacity(size); + for i in 0..size { inner.push(i); } table.push(inner); diff --git a/testing/tests/filters.rs b/testing/tests/filters.rs index 4579afe..b02a64b 100644 --- a/testing/tests/filters.rs +++ b/testing/tests/filters.rs @@ -1,3 +1,5 @@ +#![allow(clippy::blacklisted_name)] + #[cfg(feature = "serde-json")] #[macro_use] extern crate serde_json; @@ -44,7 +46,7 @@ struct MyFilterTemplate<'a> { mod filters { pub fn myfilter(s: &str) -> ::askama::Result<String> { - Ok(s.replace("oo", "aa").to_string()) + Ok(s.replace("oo", "aa")) } // for test_nested_filter_ref pub fn mytrim(s: &dyn (::std::fmt::Display)) -> ::askama::Result<String> { diff --git a/testing/tests/matches.rs b/testing/tests/matches.rs index 8657fa5..3e6d636 100644 --- a/testing/tests/matches.rs +++ b/testing/tests/matches.rs @@ -1,3 +1,5 @@ +#![allow(clippy::blacklisted_name)] + use askama::Template; #[derive(Template)] diff --git a/testing/tests/operators.rs b/testing/tests/operators.rs index d6c3cae..99a2a4c 100644 --- a/testing/tests/operators.rs +++ b/testing/tests/operators.rs @@ -1,3 +1,5 @@ +#![allow(clippy::blacklisted_name)] + use askama::Template; #[derive(Template)] diff --git a/testing/tests/simple.rs b/testing/tests/simple.rs index a311e90..6dbfcfb 100644 --- a/testing/tests/simple.rs +++ b/testing/tests/simple.rs @@ -1,3 +1,5 @@ +#![allow(clippy::blacklisted_name)] + use askama::{SizedTemplate, Template}; use std::collections::HashMap; diff --git a/testing/tests/vars.rs b/testing/tests/vars.rs index 0e1e367..3af41c6 100644 --- a/testing/tests/vars.rs +++ b/testing/tests/vars.rs @@ -1,3 +1,5 @@ +#![allow(clippy::useless_let_if_seq)] + use askama::Template; #[derive(Template)] |