Compiler warns that field is never read for serde struct

I have a small rust test program that works, but the compiler complains that all fields in the structs are never used, which is not true because they are used for parsing the JSON string:

extern crate serde;
extern crate serde_json;

#[macro_use]
extern crate serde_derive;

#[derive(Deserialize, Debug)]
struct InstanceId {
  instance_seqno: i64,
  permanent_uuid: String,
  start_time_us: i64,
}

#[derive(Deserialize, Debug)]
struct Registration {
  private_rpc_addresses: Vec<PrivateRpcAddresses>,
  http_addresses: Vec<HttpAddresses>,
  cloud_info: CloudInfo,
  placement_uuid: String,
}

#[derive(Deserialize, Debug)]
struct PrivateRpcAddresses {
  host: String,
  port: i16,
}

#[derive(Deserialize, Debug)]
struct HttpAddresses {
  host: String,
  port: i16,
}

#[derive(Deserialize, Debug)]
struct CloudInfo {
  placement_cloud: String,
  placement_region: String,
  placement_zone: String,
}

#[derive(Deserialize, Debug)]
struct Masters {
  instance_id: InstanceId,
  registration: Registration,
  role: String,
}

#[derive(Deserialize, Debug)]
struct AllMasters {
  masters: Vec<Masters>,
}

fn main() {

  let test_data = r#"
  {
  "masters": [
    {
      "instance_id": {
        "permanent_uuid": "509395298ef3487c8e5979a4c1192797",
        "instance_seqno": 1640040755035803,
        "start_time_us": 1640040755035803
      },
      "registration": {
        "private_rpc_addresses": [
          {
            "host": "yb-1.local",
            "port": 7100
          }
        ],
        "http_addresses": [
          {
            "host": "ybxxxxx1.local",
            "port": 7000
          }
        ],
        "cloud_info": {
          "placement_cloud": "local",
          "placement_region": "local",
          "placement_zone": "local"
        },
        "placement_uuid": ""
      },
      "role": "LEADER"
    }
  ]
}
  "#;
  let test_parse: AllMasters = serde_json::from_str(test_data).unwrap();
  println!("{:#?}", test_parse);

}

The code works, but throws warnings:

   Compiling json_test v0.1.0 (/Users/fritshoogland/code/json_test)
warning: field is never read: `permanent_uuid`
  --> src/main.rs:13:3
   |
13 |   permanent_uuid: String,
   |   ^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: field is never read: `start_time_us`
  --> src/main.rs:14:3
   |
14 |   start_time_us: i64,
   |   ^^^^^^^^^^^^^^^^^^

warning: field is never read: `private_rpc_addresses`
  --> src/main.rs:19:3
   |
19 |   private_rpc_addresses: Vec<PrivateRpcAddresses>,
   |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: field is never read: `http_addresses`
  --> src/main.rs:20:3
   |
20 |   http_addresses: Vec<HttpAddresses>,
   |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: field is never read: `cloud_info`
  --> src/main.rs:21:3
   |
21 |   cloud_info: CloudInfo,
   |   ^^^^^^^^^^^^^^^^^^^^^

warning: field is never read: `placement_uuid`
  --> src/main.rs:22:3
   |
22 |   placement_uuid: String,
   |   ^^^^^^^^^^^^^^^^^^^^^^

warning: field is never read: `host`
  --> src/main.rs:27:3
   |
27 |   host: String,
   |   ^^^^^^^^^^^^

warning: field is never read: `port`
  --> src/main.rs:28:3
   |
28 |   port: i16,
   |   ^^^^^^^^^

warning: field is never read: `host`
  --> src/main.rs:33:3
   |
33 |   host: String,
   |   ^^^^^^^^^^^^

warning: field is never read: `port`
  --> src/main.rs:34:3
   |
34 |   port: i16,
   |   ^^^^^^^^^

warning: field is never read: `placement_cloud`
  --> src/main.rs:39:3
   |
39 |   placement_cloud: String,
   |   ^^^^^^^^^^^^^^^^^^^^^^^

warning: field is never read: `placement_region`
  --> src/main.rs:40:3
   |
40 |   placement_region: String,
   |   ^^^^^^^^^^^^^^^^^^^^^^^^

warning: field is never read: `placement_zone`
  --> src/main.rs:41:3
   |
41 |   placement_zone: String,
   |   ^^^^^^^^^^^^^^^^^^^^^^

warning: field is never read: `registration`
  --> src/main.rs:47:3
   |
47 |   registration: Registration,
   |   ^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: field is never read: `role`
  --> src/main.rs:48:3
   |
48 |   role: String,
   |   ^^^^^^^^^^^^

warning: `json_test` (bin "json_test") generated 15 warnings
    Finished dev [unoptimized + debuginfo] target(s) in 0.49s
     Running `/Users/fritshoogland/code/json_test/target/debug/json_test`

Interesting. The fact that the fields are used for deserializing is irrelevant here (warning says they are never read, but deserializer uses them for writing); however, the debug implementation should probably count as "read".

#[derive(Debug)] is (as of the latest release) specifically ignored for dead code analysis. The rationale is that it would otherwise almost never warn authors of fields that are never used meaningfully, assuming standard practice of applying #[derive(Debug)] to most structs.

2 Likes

If you care about the output, I suggest deriving Serialize for them too, not just using Debug for it.

Then they'll show as used.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.