Skip to content

clippy::needless_maybe_sized emits a false positive on derived impls with Sized bounds #13360

@tertsdiepraam

Description

@tertsdiepraam

Summary

If there is a type with a ?Sized type parameter which has a derived impl (e.g. via serde) that requires a Sized bound, a needless_maybe_sized warning will be emitted. This is a false positive because the Sized bound then only applies to that specific impl, not to the type in general.

As far as I can tell this happens on 1.81 and on nightly. I couldn't find any issues about this yet, but if there is one already, I'm sorry for the noise :)

Lint Name

clippy::needless_maybe_sized

Reproducer

I tried this code:

use serde;
use std::marker::PhantomData

#[derive(serde::Serialize)]
#[serde(bound = "T: Bar")]
struct Foo<T: ?Sized> {
    t: PhantomData<T>,
}

trait Bar: Sized {}

I saw this happen:

warning: `?Sized` bound is ignored because of a `Sized` requirement
 --> src/main.rs:6:15
  |
6 | struct Foo<T: ?Sized> {
  |               ^^^^^^
  |
note: `T` cannot be unsized because of the bound
 --> src/main.rs:5:17
  |
5 | #[serde(bound = "T: Bar")]
  |                 ^^^^^^^^
  = note: ...because `Bar` has the bound `Sized`
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_maybe_sized
  = note: `#[warn(clippy::needless_maybe_sized)]` on by default
help: change the bounds that require `Sized`, or remove the `?Sized` bound
  |
6 - struct Foo<T: ?Sized> {
6 + struct Foo<T> {
  |

I expected to see no warning about the ?Sized bound.

Version

rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: x86_64-unknown-linux-gnu
release: 1.81.0
LLVM version: 18.1.7

It also happens on nightly still.

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions