Skip to content

Wrong unfulfilled lint expectation for dead_code on a specific associated function #148861

@weiznich

Description

@weiznich

Code

use std::any::Any;

pub trait Foo: Any {}

impl Foo for Box<dyn Foo> {}

impl dyn Foo {
    #[expect(dead_code)]
    fn downcast_ref<T: 'static>(&self) -> Option<&T> {
        let this = self as &dyn Any;
        if let Some(boxed) = this.downcast_ref::<Box<dyn Foo>>() {
            boxed.downcast_ref::<T>()
        } else {
            this.downcast_ref::<T>()
        }     
    }
}

Current output

warning: this lint expectation is unfulfilled
 --> src/main.rs:8:14
  |
8 |     #[expect(dead_code)]
  |              ^^^^^^^^^
  |
  = note: `#[warn(unfulfilled_lint_expectations)]` on by default

Desired output

No warning

Rationale and extra context

This function is unused and it is reported as unused without the #[expect] attribute. My expectation would be that adding the #[expect] attribute suppresses the warning

Playground link

Other cases

use std::any::Any;
pub trait Foo: Any {}

impl Foo for Box<dyn Foo> {}

impl dyn Foo {
    fn downcast_ref<T: 'static>(&self) -> Option<&T> {
        let this = self as &dyn Any;
        if let Some(boxed) = this.downcast_ref::<Box<dyn Foo>>() {
            boxed.downcast_ref::<T>()
        } else {
            this.downcast_ref::<T>()
        }     
    }
}

reports

warning: method `downcast_ref` is never used
 --> src/main.rs:9:8
  |
7 | impl dyn Foo {
  | ------------ method in this implementation
8 |     //#[expect(dead_code)]
9 |     fn downcast_ref<T: 'static>(&self) -> Option<&T> {
  |        ^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` (part of `#[warn(unused)]`) on by default

Rust Version

1.93.0-nightly (2025-11-11 25d319a0f656ee8faa7a)

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.L-dead_codeLint: dead_codeS-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions