Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

field_reassign_with_default triggers in custom derives #6545

Closed
jan-auer opened this issue Jan 4, 2021 · 0 comments · Fixed by #6553
Closed

field_reassign_with_default triggers in custom derives #6545

jan-auer opened this issue Jan 4, 2021 · 0 comments · Fixed by #6553
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have T-macros Type: Issues with macros and macro expansion

Comments

@jan-auer
Copy link

jan-auer commented Jan 4, 2021

Lint name: field_reassign_with_default

I tried this code:

use schemars::JsonSchema;

#[derive(Default, JsonSchema)]
#[serde(default)]
pub struct MyStruct {
    pub field: i32,
}
Click to show cargo expand output
#![feature(prelude_import)]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std;
use schemars::JsonSchema;
#[serde(default)]
pub struct MyStruct {
    pub field: i32,
}
#[automatically_derived]
#[allow(unused_qualifications)]
impl ::core::default::Default for MyStruct {
    #[inline]
    fn default() -> MyStruct {
        MyStruct {
            field: ::core::default::Default::default(),
        }
    }
}
#[automatically_derived]
#[allow(unused_braces)]
impl schemars::JsonSchema for MyStruct {
    fn schema_name() -> std::string::String {
        "MyStruct".to_owned()
    }
    fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
        {
            let schema = {
                let container_default = Self::default();
                let mut schema_object = schemars::schema::SchemaObject {
                    instance_type: Some(schemars::schema::InstanceType::Object.into()),
                    ..Default::default()
                };
                <i32 as schemars::JsonSchema>::add_schema_as_property(
                    gen,
                    &mut schema_object,
                    "field".to_owned(),
                    Some({
                        let mut metadata = schemars::schema::Metadata::default();
                        metadata.default = Some(container_default.field)
                            .and_then(|d| schemars::_serde_json::value::to_value(d).ok());
                        metadata
                    }),
                    false,
                );
                schemars::schema::Schema::Object(schema_object)
            };
            gen.apply_metadata(schema, None)
        }
    }
}

I expected to see this happen:
No lint violations in generated code annotated with #[automatically_derived].

Instead, this happened:

 $ cargo clippy -- -D clippy::all
error: field assignment outside of initializer for an instance created with Default::default()
 --> src/lib.rs:3:19
  |
3 | #[derive(Default, JsonSchema)]
  |                   ^^^^^^^^^^

Meta

  • cargo clippy -V: clippy 0.0.212 (e1884a8e 2020-12-29)
  • rustc -Vv:
    rustc 1.49.0 (e1884a8e3 2020-12-29)
    binary: rustc
    commit-hash: e1884a8e3c3e813aada8254edfa120e85bf5ffca
    commit-date: 2020-12-29
    host: x86_64-apple-darwin
    release: 1.49.0
    
@jan-auer jan-auer added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jan 4, 2021
@phansch phansch self-assigned this Jan 5, 2021
@phansch phansch added the T-macros Type: Issues with macros and macro expansion label Jan 5, 2021
@bors bors closed this as completed in efccfe8 Jan 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have T-macros Type: Issues with macros and macro expansion
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants