Skip to content

Commit

Permalink
add fixes for some clippy lints (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
knickish authored Oct 29, 2023
1 parent 626a07e commit 49b6123
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 35 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,29 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: test
- uses: actions-rs/cargo@v1
with:
command: test

test_no_std:
name: Test No Std
runs-on: ${{ matrix.config.os }}
strategy:
fail-fast: false
matrix:
config:
- { os: ubuntu-latest, target: 'x86_64-unknown-linux-gnu' }

steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
target: ${{ matrix.config.target }}
override: true
- uses: actions-rs/cargo@v1
with:
command: test
- uses: actions-rs/cargo@v1
with:
command: test
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ no_std = ["dep:hashbrown"]

[dependencies]
hashbrown = { version = "0.12.3", optional = true }
nanoserde-derive = { path = "derive", version = "=0.1.20" }
nanoserde-derive = { path = "derive", version = "=0.1.21" }
2 changes: 1 addition & 1 deletion derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nanoserde-derive"
version = "0.1.20"
version = "0.1.21"
authors = ["Makepad <[email protected]>", "Fedor <[email protected]>"]
edition = "2018"
description = "Fork of makepad-tinyserde derive without any external dependencies"
Expand Down
2 changes: 1 addition & 1 deletion derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg_attr(features = "no_std", no_std)]
#![cfg_attr(feature = "no_std", no_std)]

extern crate alloc;
extern crate proc_macro;
Expand Down
4 changes: 4 additions & 0 deletions derive/src/serde_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ pub fn derive_de_json_named(name: &str, defaults: bool, fields: &[Field]) -> Tok
pub fn derive_de_json_proxy(proxy_type: &str, type_: &str) -> TokenStream {
format!(
"impl DeJson for {} {{
#[allow(clippy::ignored_unit_patterns)]
fn de_json(_s: &mut nanoserde::DeJsonState, i: &mut core::str::Chars) -> core::result::Result<Self, nanoserde::DeJsonErr> {{
let proxy: {} = DeJson::deserialize_json(i)?;
core::result::Result::Ok(Into::into(&proxy))
Expand All @@ -247,6 +248,7 @@ pub fn derive_de_json_struct(struct_: &Struct) -> TokenStream {

format!(
"impl{} DeJson for {}{} {{
#[allow(clippy::ignored_unit_patterns)]
fn de_json(s: &mut nanoserde::DeJsonState, i: &mut core::str::Chars) -> core::result::Result<Self,
nanoserde::DeJsonErr> {{
core::result::Result::Ok({{ {} }})
Expand Down Expand Up @@ -459,6 +461,7 @@ pub fn derive_de_json_enum(enum_: &Enum) -> TokenStream {

let mut r = format!(
"impl{} DeJson for {}{} {{
#[allow(clippy::ignored_unit_patterns)]
fn de_json(s: &mut nanoserde::DeJsonState, i: &mut core::str::Chars) -> core::result::Result<Self, nanoserde::DeJsonErr> {{
match s.tok {{",
generic_w_bounds, generic_no_bounds, enum_.name,
Expand Down Expand Up @@ -592,6 +595,7 @@ pub fn derive_de_json_struct_unnamed(struct_: &Struct) -> TokenStream {

format! ("
impl{} DeJson for {}{} {{
#[allow(clippy::ignored_unit_patterns)]
fn de_json(s: &mut nanoserde::DeJsonState, i: &mut core::str::Chars) -> core::result::Result<Self,nanoserde::DeJsonErr> {{
{}
core::result::Result::Ok(r)
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
//! `nanoserde` supports some serialization customisation with `#[nserde()]` attributes.
//! For `#[nserde(..)]` supported attributes for each format check [Features support matrix](https://github.com/not-fl3/nanoserde#features-support-matrix)
#![cfg_attr(features = "no_std", no_std)]
#![cfg_attr(feature = "no_std", no_std)]
// Possibly stable in 1.65.
// See: https://github.com/rust-lang/rust/pull/99917
#![cfg_attr(features = "no_std", feature(error_in_core))]
#![cfg_attr(feature = "no_std", feature(error_in_core))]

extern crate alloc;

Expand Down
10 changes: 5 additions & 5 deletions src/serde_bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use alloc::collections::{BTreeSet, LinkedList};
use alloc::string::{String, ToString};
use alloc::vec::Vec;

#[cfg(features = "no_std")]
#[cfg(feature = "no_std")]
use hashbrown::{HashMap, HashSet};

#[cfg(not(features = "no_std"))]
#[cfg(not(feature = "no_std"))]
use std::collections::{HashMap, HashSet};

/// A trait for objects that can be serialized to binary.
Expand Down Expand Up @@ -83,10 +83,10 @@ impl core::fmt::Display for DeBinErr {
}
}

#[cfg(features = "no_std")]
#[cfg(feature = "no_std")]
impl core::error::Error for DeBinErr {}

#[cfg(not(features = "no_std"))]
#[cfg(not(feature = "no_std"))]
impl std::error::Error for DeBinErr {}

macro_rules! impl_ser_de_bin_for {
Expand Down Expand Up @@ -543,7 +543,7 @@ where

impl<K, V> DeBin for HashMap<K, V>
where
K: DeBin + std::cmp::Eq + std::hash::Hash,
K: DeBin + core::cmp::Eq + Hash,
V: DeBin,
{
fn de_bin(o: &mut usize, d: &[u8]) -> Result<Self, DeBinErr> {
Expand Down
18 changes: 9 additions & 9 deletions src/serde_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;

#[cfg(features = "no_std")]
#[cfg(feature = "no_std")]
use hashbrown::{HashMap, HashSet};

#[cfg(not(features = "no_std"))]
#[cfg(not(feature = "no_std"))]
use std::collections::{HashMap, HashSet};

/// The internal state of a JSON serialization.
Expand Down Expand Up @@ -167,10 +167,10 @@ impl core::fmt::Display for DeJsonErr {
}
}

#[cfg(features = "no_std")]
#[cfg(feature = "no_std")]
impl core::error::Error for DeJsonErr {}

#[cfg(not(features = "no_std"))]
#[cfg(not(feature = "no_std"))]
impl std::error::Error for DeJsonErr {}

impl DeJsonState {
Expand Down Expand Up @@ -756,9 +756,9 @@ impl DeJson for () {
fn de_json(s: &mut DeJsonState, i: &mut Chars) -> Result<(), DeJsonErr> {
if let DeJsonTok::Null = s.tok {
s.next_tok(i)?;
return Ok(());
Ok(())
} else {
return Err(s.err_token("null"));
Err(s.err_token("null"))
}
}
}
Expand All @@ -777,7 +777,7 @@ impl DeJson for bool {
fn de_json(s: &mut DeJsonState, i: &mut Chars) -> Result<bool, DeJsonErr> {
let val = s.as_bool()?;
s.next_tok(i)?;
return Ok(val);
Ok(val)
}
}

Expand Down Expand Up @@ -815,7 +815,7 @@ impl DeJson for String {
fn de_json(s: &mut DeJsonState, i: &mut Chars) -> Result<String, DeJsonErr> {
let val = s.as_string()?;
s.next_tok(i)?;
return Ok(val);
Ok(val)
}
}

Expand Down Expand Up @@ -1030,7 +1030,7 @@ where
// https://github.com/rust-lang/rust/issues/61956
// initializing before block close so that drop will run automatically if err encountered there
let initialized =
unsafe { (&*(&to as *const _ as *const MaybeUninit<_>)).assume_init_read() };
unsafe { (*(&to as *const _ as *const MaybeUninit<_>)).assume_init_read() };
o.block_close(d)?;

Ok(initialized)
Expand Down
8 changes: 4 additions & 4 deletions src/serde_ron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;

#[cfg(features = "no_std")]
#[cfg(feature = "no_std")]
use hashbrown::{HashMap, HashSet};

#[cfg(not(features = "no_std"))]
#[cfg(not(feature = "no_std"))]
use std::collections::{HashMap, HashSet};

/// The internal state of a RON serialization.
Expand Down Expand Up @@ -164,10 +164,10 @@ impl core::fmt::Display for DeRonErr {
}
}

#[cfg(features = "no_std")]
#[cfg(feature = "no_std")]
impl core::error::Error for DeRonErr {}

#[cfg(not(features = "no_std"))]
#[cfg(not(feature = "no_std"))]
impl std::error::Error for DeRonErr {}

impl DeRonState {
Expand Down
18 changes: 9 additions & 9 deletions src/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use alloc::format;
use alloc::string::{String, ToString};
use alloc::{vec, vec::Vec};

#[cfg(features = "no_std")]
#[cfg(feature = "no_std")]
use hashbrown::HashMap;

#[cfg(not(features = "no_std"))]
#[cfg(not(feature = "no_std"))]
use std::collections::HashMap;

/// A parser for TOML string values.
Expand Down Expand Up @@ -158,7 +158,7 @@ struct Out {
}
impl Out {
fn start_array(&mut self, key: &str) {
if self.out.contains_key(key) == false {
if !self.out.contains_key(key) {
self.out.insert(key.to_string(), Toml::Array(vec![]));
}

Expand Down Expand Up @@ -186,10 +186,10 @@ impl Out {
}
}

#[cfg(features = "no_std")]
#[cfg(feature = "no_std")]
impl core::error::Error for TomlErr {}

#[cfg(not(features = "no_std"))]
#[cfg(not(feature = "no_std"))]
impl std::error::Error for TomlErr {}

impl TomlParser {
Expand Down Expand Up @@ -252,11 +252,11 @@ impl TomlParser {
}
}
TomlTok::Str(key) | TomlTok::Ident(key) => {
self.parse_key_value(&local_scope, key, i, out.out())?
self.parse_key_value(local_scope, key, i, out.out())?
}
_ => return Err(self.err_token(tok)),
}
return Ok(true);
Ok(true)
}

fn to_val(&mut self, tok: TomlTok, i: &mut Chars) -> Result<Toml, TomlErr> {
Expand All @@ -277,7 +277,7 @@ impl TomlParser {
TomlTok::Str(v) => Ok(Toml::Str(v)),
TomlTok::U64(v) => Ok(Toml::Num(v as f64)),
TomlTok::I64(v) => Ok(Toml::Num(v as f64)),
TomlTok::F64(v) => Ok(Toml::Num(v as f64)),
TomlTok::F64(v) => Ok(Toml::Num(v)),
TomlTok::Bool(v) => Ok(Toml::Bool(v)),
TomlTok::Nan(v) => Ok(Toml::Num(if v { -core::f64::NAN } else { core::f64::NAN })),
TomlTok::Inf(v) => Ok(Toml::Num(if v {
Expand All @@ -303,7 +303,7 @@ impl TomlParser {
}
let tok = self.next_tok(i)?;
let val = self.to_val(tok, i)?;
let key = if local_scope.len() > 0 {
let key = if !local_scope.is_empty() {
format!("{}.{}", local_scope, key)
} else {
key
Expand Down
8 changes: 7 additions & 1 deletion tests/bin.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use std::{
array,
collections::{BTreeSet, HashMap, HashSet, LinkedList},
collections::{BTreeSet, LinkedList},
sync::atomic::AtomicBool,
};

#[cfg(feature = "no_std")]
use hashbrown::{HashMap, HashSet};

#[cfg(not(feature = "no_std"))]
use std::collections::{HashMap, HashSet};

use nanoserde::{DeBin, SerBin};

#[test]
Expand Down
8 changes: 7 additions & 1 deletion tests/json.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use nanoserde::{DeJson, SerJson};

use std::{
collections::{BTreeSet, HashMap, HashSet, LinkedList},
collections::{BTreeSet, LinkedList},
sync::atomic::AtomicBool,
};

#[cfg(feature = "no_std")]
use hashbrown::{HashMap, HashSet};

#[cfg(not(feature = "no_std"))]
use std::collections::{HashMap, HashSet};

#[test]
fn de() {
#[derive(DeJson)]
Expand Down
8 changes: 7 additions & 1 deletion tests/ron.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use nanoserde::{DeRon, SerRon};

use std::{
collections::{BTreeSet, HashMap, HashSet, LinkedList},
collections::{BTreeSet, LinkedList},
sync::atomic::AtomicBool,
};

#[cfg(feature = "no_std")]
use hashbrown::{HashMap, HashSet};

#[cfg(not(feature = "no_std"))]
use std::collections::{HashMap, HashSet};

#[test]
fn ron_de() {
#[derive(DeRon)]
Expand Down
4 changes: 4 additions & 0 deletions tests/ser_de.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use nanoserde::{DeBin, DeJson, DeRon, SerBin, SerJson, SerRon};

#[cfg(feature = "no_std")]
use hashbrown::HashMap;

#[cfg(not(feature = "no_std"))]
use std::collections::HashMap;

#[test]
Expand Down

0 comments on commit 49b6123

Please sign in to comment.