From 57ebbb33d99b59ca71255828214b1acda5fa6d61 Mon Sep 17 00:00:00 2001 From: Jay Oster Date: Wed, 22 Jun 2022 08:25:34 -0700 Subject: [PATCH] Fix clippy lint - This is mostly the result of `cargo clippy --all --fix` - The exceptions are the transformations from `s.push_str(&format!(...))` -> `write!(s, ...)`, which clippy would not automate. --- examples/basic.rs | 2 +- examples/dump.rs | 2 +- examples/errors.rs | 2 +- examples/with_structs.rs | 2 +- src/errors.rs | 4 ++-- src/parser.rs | 21 +++++++++++---------- tests/full.rs | 12 ++++++------ tests/importing.rs | 6 +++--- tests/objects.rs | 4 ++-- tests/strings.rs | 2 +- tests/variables.rs | 12 ++++++------ 11 files changed, 35 insertions(+), 34 deletions(-) diff --git a/examples/basic.rs b/examples/basic.rs index 2f9939b..019c408 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -17,7 +17,7 @@ hosts: [ ]"##; // Parse: transforms a Gura string into a dictionary - let parsed = parse(&gura_string).unwrap(); + let parsed = parse(gura_string).unwrap(); // Debug and Display // println!("{:#?}", parsed); diff --git a/examples/dump.rs b/examples/dump.rs index 3fe12d4..2f52e38 100644 --- a/examples/dump.rs +++ b/examples/dump.rs @@ -35,7 +35,7 @@ fn main() { ] ]"##; - let parsed = parse(&str).unwrap(); + let parsed = parse(str).unwrap(); let dumped = dump(&parsed); assert_eq!(str.trim(), dumped.trim()); } diff --git a/examples/errors.rs b/examples/errors.rs index 8061dcd..80a5d3a 100644 --- a/examples/errors.rs +++ b/examples/errors.rs @@ -9,7 +9,7 @@ some_invalid: $non_existent_var "##; // Checks parsing result - match parse(&gura_string) { + match parse(gura_string) { Ok(parsed) => { println!("Title -> {}", parsed["title"]); } diff --git a/examples/with_structs.rs b/examples/with_structs.rs index 0d88269..067ba29 100644 --- a/examples/with_structs.rs +++ b/examples/with_structs.rs @@ -27,7 +27,7 @@ tango_singers: [ ]"##; // Parse: transforms a Gura string into a dictionary - let parsed = parse(&gura_string).unwrap(); + let parsed = parse(gura_string).unwrap(); // Lets make an array of singers if let GuraType::Array(tango_singers) = &parsed["tango_singers"] { diff --git a/src/errors.rs b/src/errors.rs index 3cb423a..0baa6c3 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,7 +1,7 @@ use std::fmt; /// All Gura error variants -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Eq)] pub enum Error { /// Raises when Gura syntax is invalid ParseError, @@ -20,7 +20,7 @@ pub enum Error { } /// A Gura error with position, line and custom message -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Eq)] pub struct GuraError { pub pos: isize, pub line: usize, diff --git a/src/parser.rs b/src/parser.rs index 9c37e25..bf20ef5 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -9,7 +9,8 @@ use std::{ collections::{HashMap, HashSet}, env, f64::{INFINITY, NAN, NEG_INFINITY}, - fmt, fs, + fmt::{self, Write as _}, + fs, ops::Index, path::Path, usize, @@ -782,7 +783,7 @@ fn char(text: &mut Input, chars: &Option) -> Result { } } - return Err(GuraError { + Err(GuraError { pos: next_char_pos, line: text.line, msg: format!( @@ -790,7 +791,7 @@ fn char(text: &mut Input, chars: &Option) -> Result { chars_value, text.text[next_char_pos_usize] ), kind: Error::ParseError, - }); + }) } } } @@ -1657,25 +1658,25 @@ fn dump_content(content: &GuraType) -> String { let mut result = String::new(); for (key, gura_value) in values.iter() { - result.push_str(&format!("{}:", key)); + let _ = write!(result, "{}:", key); // If the value is an object, splits the stringified value by // newline and indents each line before adding it to the result - if let GuraType::Object(obj) = &*gura_value { + if let GuraType::Object(obj) = gura_value { let dumped = dump_content(gura_value); let stringified_value = dumped.trim_end(); if !obj.is_empty() { result.push('\n'); for line in stringified_value.split('\n') { - result.push_str(&format!("{}{}\n", INDENT, line)); + let _ = writeln!(result, "{}{}", INDENT, line); } } else { // Prevents indentation on empty objects - result.push_str(&format!(" {}\n", stringified_value)); + let _ = writeln!(result, " {}", stringified_value); } } else { - result.push_str(&format!(" {}\n", dump_content(gura_value))); + let _ = writeln!(result, " {}", dump_content(gura_value)); } } @@ -1685,7 +1686,7 @@ fn dump_content(content: &GuraType) -> String { // Lists are a special case: if it has an object, and indented representation must be returned. In case // of primitive values or nested arrays, a plain representation is more appropriated let should_multiline = array.iter().any(|e| { - if let GuraType::Object(obj) = &*e { + if let GuraType::Object(obj) = e { !obj.is_empty() } else { false @@ -1717,7 +1718,7 @@ fn dump_content(content: &GuraType) -> String { result += &splitted.iter().cloned().join("\n"); } else { // Otherwise indent the value and add to result - result.push_str(&format!("{}{}", INDENT, stringified_value)); + let _ = write!(result, "{}{}", INDENT, stringified_value); } // Add a comma if this entry is not the final entry in the list diff --git a/tests/full.rs b/tests/full.rs index 56f44b2..212fd3d 100644 --- a/tests/full.rs +++ b/tests/full.rs @@ -149,7 +149,7 @@ fn test_dumps_result() { ] ]"##; - let parsed = parse(&str).unwrap(); + let parsed = parse(str).unwrap(); let dumped = dump(&parsed); assert_eq!(str, dumped); } @@ -168,34 +168,34 @@ fn test_dumps_nan() { #[test] /// Tests empty Gura documents fn test_empty() { - let parsed_data = parse(&"").unwrap(); + let parsed_data = parse("").unwrap(); assert_eq!(parsed_data, object! {}); } #[test] /// Tests empty Gura documents, even when some data is defined fn test_empty_2() { - let parsed_data = parse(&"$unused_var: 5").unwrap(); + let parsed_data = parse("$unused_var: 5").unwrap(); assert_eq!(parsed_data, object! {}); } #[test] /// Tests invalid key fn test_invalid_key() { - let parsed_data = parse(&"with.dot: 5"); + let parsed_data = parse("with.dot: 5"); assert_eq!(parsed_data.unwrap_err().kind, Error::ParseError); } #[test] /// Tests invalid key fn test_invalid_key_2() { - let parsed_data = parse(&"\"with_quotes\": 5"); + let parsed_data = parse("\"with_quotes\": 5"); assert_eq!(parsed_data.unwrap_err().kind, Error::ParseError); } #[test] /// Tests invalid key fn test_invalid_key_3() { - let parsed_data = parse(&"with-dashes: 5"); + let parsed_data = parse("with-dashes: 5"); assert_eq!(parsed_data.unwrap_err().kind, Error::ParseError); } diff --git a/tests/importing.rs b/tests/importing.rs index ba70b39..8e2b7bd 100644 --- a/tests/importing.rs +++ b/tests/importing.rs @@ -40,7 +40,7 @@ fn test_with_variables() { #[test] /// Tests errors importing a non existing file fn test_not_found_error() { - let parsed_data = parse(&"import \"invalid_file.ura\""); + let parsed_data = parse("import \"invalid_file.ura\""); assert_eq!(parsed_data.unwrap_err().kind, Error::FileNotFoundError); } @@ -92,13 +92,13 @@ fn test_with_absolute_paths() { #[test] /// Tests errors invalid importing sentence (there are blanks before import) fn test_parse_error_1() { - let parsed_data = parse(&" import \"another_file.ura\""); + let parsed_data = parse(" import \"another_file.ura\""); assert_eq!(parsed_data.unwrap_err().kind, Error::ParseError); } #[test] /// Tests errors invalid importing sentence (there are more than one whitespace between import and file name) fn test_parse_error_2() { - let parsed_data = parse(&"import \"another_file.ura\""); + let parsed_data = parse("import \"another_file.ura\""); assert_eq!(parsed_data.unwrap_err().kind, Error::ParseError); } diff --git a/tests/objects.rs b/tests/objects.rs index 44ef624..bbe93a3 100644 --- a/tests/objects.rs +++ b/tests/objects.rs @@ -38,14 +38,14 @@ fn test_normal() { #[test] /// Tests empty object fn test_empty() { - let parsed_data = parse(&"empty_object: empty").unwrap(); + let parsed_data = parse("empty_object: empty").unwrap(); assert_eq!(parsed_data, get_empty_object()); } #[test] /// Tests empty object with several blanks fn test_empty_2() { - let parsed_data = parse(&"empty_object: empty ").unwrap(); + let parsed_data = parse("empty_object: empty ").unwrap(); assert_eq!(parsed_data, get_empty_object()); } diff --git a/tests/strings.rs b/tests/strings.rs index 789b374..bdb57a2 100644 --- a/tests/strings.rs +++ b/tests/strings.rs @@ -82,7 +82,7 @@ fn test_multiline_basic_strings() { #[test] /// Tests errors in basic strings fn test_basic_strings_errors() { - let parsed_data = parse(&"test: \"$false_var\""); + let parsed_data = parse("test: \"$false_var\""); assert_eq!( parsed_data.unwrap_err().kind, Error::VariableNotDefinedError diff --git a/tests/variables.rs b/tests/variables.rs index 9991103..a956aeb 100644 --- a/tests/variables.rs +++ b/tests/variables.rs @@ -31,7 +31,7 @@ fn test_normal() { #[test] /// Tests errors in variables definition fn test_with_error() { - let parsed_data = parse(&"test: $false_var"); + let parsed_data = parse("test: $false_var"); assert_eq!( parsed_data.unwrap_err().kind, Error::VariableNotDefinedError @@ -41,7 +41,7 @@ fn test_with_error() { #[test] /// Tests errors in variables definition fn test_with_duplicated() { - let parsed_data = parse(&"$a_var: 14\n$a_var: 15"); + let parsed_data = parse("$a_var: 14\n$a_var: 15"); assert_eq!( parsed_data.unwrap_err().kind, Error::DuplicatedVariableError @@ -63,28 +63,28 @@ fn test_env_var() { #[test] /// Tests invalid variable value type fn test_invalid_variable() { - let parsed_data = parse(&"$invalid: true"); + let parsed_data = parse("$invalid: true"); assert_eq!(parsed_data.unwrap_err().kind, Error::ParseError); } #[test] /// Tests invalid variable value type fn test_invalid_variable_2() { - let parsed_data = parse(&"$invalid: false"); + let parsed_data = parse("$invalid: false"); assert_eq!(parsed_data.unwrap_err().kind, Error::ParseError); } #[test] /// Tests invalid variable value type fn test_invalid_variable_3() { - let parsed_data = parse(&"$invalid: null"); + let parsed_data = parse("$invalid: null"); assert_eq!(parsed_data.unwrap_err().kind, Error::ParseError); } #[test] /// Tests invalid variable value type fn test_invalid_variable_4() { - let parsed_data = parse(&"$invalid: [ 1, 2, 3]"); + let parsed_data = parse("$invalid: [ 1, 2, 3]"); assert_eq!(parsed_data.unwrap_err().kind, Error::ParseError); }