Make json macro compatible with deny(unused_results)

The unused_results lint triggers on any function call returning anything
other than () or ! that is not used. The Map::insert function returns
Option<Value> of the previous value of the inserted key.
This commit is contained in:
David Tolnay 2018-07-16 09:35:32 -07:00
parent 1b36cec484
commit 5c931e0536
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 5 additions and 2 deletions

View File

@ -161,7 +161,7 @@ macro_rules! json_internal {
// Insert the current entry followed by trailing comma. // Insert the current entry followed by trailing comma.
(@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => { (@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {
$object.insert(($($key)+).into(), $value); let _ = $object.insert(($($key)+).into(), $value);
json_internal!(@object $object () ($($rest)*) ($($rest)*)); json_internal!(@object $object () ($($rest)*) ($($rest)*));
}; };
@ -172,7 +172,7 @@ macro_rules! json_internal {
// Insert the last entry without trailing comma. // Insert the last entry without trailing comma.
(@object $object:ident [$($key:tt)+] ($value:expr)) => { (@object $object:ident [$($key:tt)+] ($value:expr)) => {
$object.insert(($($key)+).into(), $value); let _ = $object.insert(($($key)+).into(), $value);
}; };
// Next value is `null`. // Next value is `null`.

View File

@ -1847,6 +1847,9 @@ fn test_json_macro() {
(<Result<&str, ()> as Clone>::clone(&Ok("")).unwrap()): "ok", (<Result<&str, ()> as Clone>::clone(&Ok("")).unwrap()): "ok",
(<Result<(), &str> as Clone>::clone(&Err("")).unwrap_err()): "err" (<Result<(), &str> as Clone>::clone(&Err("")).unwrap_err()): "err"
}); });
#[deny(unused_results)]
let _ = json!({ "architecture": [true, null] });
} }
#[test] #[test]