Skip to content

Unused default functions may be generated #976

@ahl

Description

@ahl

Consider this schema:

{
  "$defs": {
    "MaxPathConfig": {
      "type": "integer",
      "minimum": 1
    },
    "Thing": {
      "type": "object",
      "properties": {
        "max_path": {
          "default": 1,
          "allOf": [
            {
              "$ref": "#/$defs/MaxPathConfig"
            }
          ]
        }
      }
    }
  }
}

This generates this code for Thing:

#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
pub struct Thing {
    #[serde(default = "defaults::thing_max_path")]
    pub max_path: MaxPathConfig,
}

Looks good! However, here's the default mod:

#[doc = r" Generation of default values for serde."]
pub mod defaults {
    pub(super) fn default_nzu64<T, const V: u64>() -> T
    where
        T: ::std::convert::TryFrom<::std::num::NonZeroU64>,
        <T as ::std::convert::TryFrom<::std::num::NonZeroU64>>::Error: ::std::fmt::Debug,
    {
        T::try_from(::std::num::NonZeroU64::try_from(V).unwrap()).unwrap()
    }
    pub(super) fn thing_max_path() -> super::MaxPathConfig {
        super::MaxPathConfig(::std::num::NonZeroU64::new(1).unwrap())
    }
}

Note that while thing_max_path is used, default_nzu64 is not and results in an unused code lint in generated code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions