diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2020-01-02 21:21:28 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2020-01-02 22:58:13 +0100 |
commit | ea2034e8062f8db6b5ae74ca8aeab4e2ee08479f (patch) | |
tree | 207b103539bacd4147c1ee274d3a1aa4938b7853 /askama_shared/src | |
parent | 9085e1e3dd0ae9924433c6e5166c33e0f6d2a283 (diff) | |
download | askama-ea2034e8062f8db6b5ae74ca8aeab4e2ee08479f.tar.gz askama-ea2034e8062f8db6b5ae74ca8aeab4e2ee08479f.tar.bz2 askama-ea2034e8062f8db6b5ae74ca8aeab4e2ee08479f.zip |
Clean up clippy warnings
Diffstat (limited to 'askama_shared/src')
-rw-r--r-- | askama_shared/src/filters/mod.rs | 57 | ||||
-rw-r--r-- | askama_shared/src/lib.rs | 13 |
2 files changed, 34 insertions, 36 deletions
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; |