diff --git a/.travis.yml b/.travis.yml index 0f39007..818141c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,16 +14,20 @@ branches: matrix: include: - - rust: stable + - rust: nightly os: linux env: RUST_BACKTRACE=1 before_cache: | if [[ "$TRAVIS_RUST_VERSION" == stable ]]; then cargo install cargo-tarpaulin -f fi + before_script: + - rustup component add clippy script: - cargo clean + - cargo clippy -- -D warnings - cargo build --verbose --all + - cargo clippy --all-targets --all-features -- -D warnings -A clippy::cognitive_complexity - cargo test --verbose --all after_success: | cargo tarpaulin --exclude-files nodejs wasm parser/mod.rs --out Xml diff --git a/build.sh b/build.sh index 70d0d7f..1d03efb 100755 --- a/build.sh +++ b/build.sh @@ -2,6 +2,9 @@ set -e +cargo clippy -- -D warnings && \ +cargo clippy --all-targets --all-features -- -D warnings -A clippy::cognitive_complexity + # project_root DIR="$(pwd)" WASM="${DIR}"/wasm diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 71eec74..59080f8 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -13,8 +13,8 @@ mod utils { use std::str::FromStr; pub fn string_to_num(string: &str, msg_handler: F) -> Result - where - F: Fn() -> String, + where + F: Fn() -> String, { match string.parse() { Ok(n) => Ok(n), @@ -195,13 +195,13 @@ impl Parser { debug!("#boolean"); match tokenizer.next_token() { Ok(Token::Key(_, ref v)) - if { - let b = v.as_bytes(); - !b.is_empty() && (b[0] == b't' || b[0] == b'T' || b[0] == b'f' || b[0] == b'F') - } => - { - Ok(Self::node(ParseToken::Bool(v.eq_ignore_ascii_case("true")))) - } + if { + let b = v.as_bytes(); + !b.is_empty() && (b[0] == b't' || b[0] == b'T' || b[0] == b'f' || b[0] == b'F') + } => + { + Ok(Self::node(ParseToken::Bool(v.eq_ignore_ascii_case("true")))) + } _ => Err(tokenizer.err_msg()), } } @@ -557,7 +557,7 @@ impl Parser { }; return match key.as_bytes()[0] { - b'-' | b'0'...b'9' => Self::term_num(tokenizer), + b'-' | b'0'..=b'9' => Self::term_num(tokenizer), _ => Self::boolean(tokenizer), }; } @@ -687,7 +687,6 @@ pub trait NodeVisitor { fn end_term(&mut self) {} } - #[cfg(test)] mod parser_tests { use parser::{FilterToken, NodeVisitor, ParseToken, Parser}; @@ -732,11 +731,7 @@ mod parser_tests { setup(); fn invalid(path: &str) { - if let Err(_) = run(path) { - assert!(true); - } else { - assert!(false); - } + assert!(run(path).is_err()); } invalid("$[]"); @@ -821,24 +816,21 @@ mod parser_tests { ]) ); - match run("$.") { - Ok(_) => panic!(), - _ => {} + if run("$.").is_ok() { + panic!(); } - match run("$..") { - Ok(_) => panic!(), - _ => {} + if run("$..").is_ok() { + panic!(); } - match run("$. a") { - Ok(_) => panic!(), - _ => {} + if run("$. a").is_ok() { + panic!(); } } #[test] - fn parse_array_sytax() { + fn parse_array_syntax() { setup(); assert_eq!( @@ -1211,24 +1203,20 @@ mod parser_tests { ]) ); - match run("$[1.1]") { - Ok(_) => panic!(), - _ => {} + if run("$[1.1]").is_ok() { + panic!(); } - match run("$[?(1.1<.2)]") { - Ok(_) => panic!(), - _ => {} + if run("$[?(1.1<.2)]").is_ok() { + panic!(); } - match run("$[?(1.1<2.)]") { - Ok(_) => panic!(), - _ => {} + if run("$[?(1.1<2.)]").is_ok() { + panic!(); } - match run("$[?(1.1<2.a)]") { - Ok(_) => panic!(), - _ => {} + if run("$[?(1.1<2.a)]").is_ok() { + panic!(); } } } @@ -1253,7 +1241,7 @@ mod tokenizer_tests { } fn run(input: &str, expected: (Vec, Option)) { - let (vec, err) = collect_token(input.clone()); + let (vec, err) = collect_token(input); assert_eq!((vec, err), expected, "\"{}\"", input); } diff --git a/src/parser/tokenizer.rs b/src/parser/tokenizer.rs index 3238c5a..bd5a15f 100644 --- a/src/parser/tokenizer.rs +++ b/src/parser/tokenizer.rs @@ -161,7 +161,7 @@ impl<'a> Tokenizer<'a> { if let Some('\\') = val.chars().last() { self.input.next_char().map_err(to_token_error)?; let _ = val.pop(); - let (_, mut val_remain) = self + let (_, val_remain) = self .input .take_while(|c| *c != ch) .map_err(to_token_error)?; diff --git a/tests/common.rs b/tests/common.rs index 82883fd..3fc2c02 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -30,7 +30,7 @@ pub fn read_contents(path: &str) -> String { } #[allow(dead_code)] -pub fn select_and_then_compare<'a>(path: &str, json: Value, target: Value) { +pub fn select_and_then_compare(path: &str, json: Value, target: Value) { let mut selector = Selector::default(); let result = selector .str_path(path) @@ -50,7 +50,7 @@ pub fn select_and_then_compare<'a>(path: &str, json: Value, target: Value) { } #[allow(dead_code)] -pub fn compare_result<'a>(result: Vec<&Value>, target: Value) { +pub fn compare_result(result: Vec<&Value>, target: Value) { let result = serde_json::to_value(result).unwrap(); assert_eq!(result, target); } diff --git a/tests/filter.rs b/tests/filter.rs index 0928c02..4540039 100644 --- a/tests/filter.rs +++ b/tests/filter.rs @@ -624,7 +624,7 @@ fn quote() { fn all_filter() { setup(); - for path in vec![r#"$.*"#, r#"$[*]"#] { + for path in &[r#"$.*"#, r#"$[*]"#] { select_and_then_compare( path, json!(["string", 42, { "key": "value" }, [0, 1]]), @@ -632,7 +632,7 @@ fn all_filter() { ); } - for path in vec![r#"$..*"#, r#"$..[*]"#] { + for path in &[r#"$..*"#, r#"$..[*]"#] { select_and_then_compare( path, json!(["string", 42, { "key": "value" }, [0, 1]]), @@ -640,7 +640,7 @@ fn all_filter() { ); } - for path in vec![r#"$.*.*"#, r#"$[*].*"#, r#"$.*[*]"#, r#"$[*][*]"#] { + for path in &[r#"$.*.*"#, r#"$[*].*"#, r#"$.*[*]"#, r#"$[*][*]"#] { select_and_then_compare( path, json!(["string", 42, { "key": "value" }, [0, 1]]), @@ -648,7 +648,7 @@ fn all_filter() { ); } - for path in vec![r#"$..friends.*"#, r#"$[*].friends.*"#] { + for path in &[r#"$..friends.*"#, r#"$[*].friends.*"#] { select_and_then_compare( path, read_json("./benches/data_array.json"), diff --git a/tests/lib.rs b/tests/lib.rs index 1493e0d..1ee8780 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -37,11 +37,7 @@ fn compile() { fn compile_error() { let mut template = jsonpath::compile("$["); - if let Err(JsonPathError::Path(_)) = template(&Value::Null) { - assert!(true); - } else { - assert!(false); - } + assert!(template(&Value::Null).is_err()); } setup(); diff --git a/tests/mutable.rs b/tests/mutable.rs index 0477a2c..6ae6205 100644 --- a/tests/mutable.rs +++ b/tests/mutable.rs @@ -20,11 +20,8 @@ fn selector_mut() { .unwrap() .value(read_json("./benches/example.json")) .replace_with(&mut |v| { - match v { - Value::Number(n) => { - nums.push(n.as_f64().unwrap()); - } - _ => {} + if let Value::Number(n) = v { + nums.push(n.as_f64().unwrap()); } Value::String("a".to_string()) })