tokio-rs / prost

PROST! a Protocol Buffers implementation for the Rust Language
Apache License 2.0
3.69k stars 481 forks source link

Nightly Compiler generates Clippy warnings for non-local `impl` definition #1040

Closed CodingAnarchy closed 2 months ago

CodingAnarchy commented 3 months ago

We recently ran Clippy against our application on the nightly build for 2024-04-18, and the generated code from prost was flagged with a number of warnings for a non-local impl definition, as below:

warning: non-local `impl` definition, they should be avoided as they go against expectation
    --> /Users/mtanous/[path]9/out/[protobuf-generated filename].rs:6244:168
     |
6244 | ...st IMPL_MESSAGE_SERDE_FOR_NODE : () = { use :: prost_wkt :: typetag ; # [typetag :: serde (name = "type.googleapis.com/shopify.cdp.v1.Node")] impl :: prost_wkt :: MessageSerde for Node { fn package_name (& self) -> & 'static str { "shopify.cdp.v1" } fn message_name (& self) -> & 'static str { "Node" } fn type_url (& self) -> & 'static str { "type.googleapis.com/shopify.cdp.v1.Node" } fn new_instance (& self , data : Vec < u8 >) -> :: std :: result :: Result < Box < dyn :: prost_wkt :: MessageSerde > , :: prost :: DecodeError > { let mut target = Self :: default () ; :: prost :: Message :: merge (& mut target , data . as_slice ()) ? ; let erased : :: std :: boxed :: Box < dyn :: prost_wkt :: MessageSerde > = :: std :: boxed :: Box :: new (target) ; Ok (erased) } fn try_encoded (& self) -> :: std :: result :: Result < :: std :: vec :: Vec < u8 > , :: prost :: EncodeError > { let mut buf = :: std :: vec :: Vec :: with_capacity (:: prost :: Message :: encoded_len (self)) ; :: prost :: Message :: encode (self , & mut buf) ? ; Ok (buf) } } ::...
     |       --------------------------- help: use a const-anon item to suppress this lint: `_`                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     |
     = help: move this `impl` block outside the of the current constant `IMPL_MESSAGE_SERDE_FOR_NODE`
     = note: an `impl` definition is non-local if it is nested inside an item and may impact type checking outside of that item. This can be the case if neither the trait or the self type are at the same nesting level as the `impl`
     = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
     = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>

I thought this was worth surfacing now, so it can be addressed ahead of time before we get to that may become deny-by-default in the edition 2024 aspect of the lint.

caspermeijn commented 2 months ago

Are you willing to fix this issue? I would appreciate it if you could create a PR.

CodingAnarchy commented 2 months ago

@caspermeijn I can definitely look into it and see if I can solve it! Let me give it a go and I'll circle back with a PR if I can figure it out.

CodingAnarchy commented 2 months ago

I think this is actually an issue generated from prost-wkt, so I opened a PR there. Closing this issue in favor of following up there.