rust-lang / rust-lang/rust

Lint `trivial_casts`: A FnDef->FnPtr cast gets incorrectly flagged as trivial

Open
#161,339 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-coercions A-lints C-bug L-false-positive S-has-mcve T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code

EDIT: please see https://github.com/rust-lang/rust/issues/161339#issuecomment-5348482270 for the full repro that actually breaks.

// Needed due to the lack of https://github.com/rust-lang/rust/issues/63063
pub(crate) struct MyIterator {
    inner: std::iter::Map<std::iter::Copied<core::slice::Iter<'static, bool>>, fn(bool) -> usize>,
}

impl MyIterator {
    fn new() -> Self {
        Self {
            inner:
                [false].as_slice().into_iter().copied().map(Into::into as fn(bool) -> usize)
        }
    }
}
Current output
warning: trivial cast: `fn(bool) -> usize {<bool as Into<usize>>::into}` as `fn(bool) -> usize`
 --> <source>:9:61
  |
9 |                 [false].as_slice().into_iter().copied().map(Into::into as fn(bool) -> usize)
  |                                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: cast can be replaced by coercion; this might require a temporary variable
  = note: requested on the command line with `-W trivial-casts`

warning: struct `MyIterator` is never constructed
 --> <source>:1:19
  |
1 | pub(crate) struct MyIterator {
  |                   ^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` (part of `#[warn(unused)]`) on by default

warning: associated function `new` is never used
 --> <source>:6:8
  |
5 | impl MyIterator {
  | --------------- associated function in this implementation
6 |     fn new() -> Self {
  |        ^^^

warning: 3 warnings emitted

Compiler returned: 0
Desired output
`no-diagnostic`
Rationale and extra context

If as fn(bool) -> usize is dropped, this no longer compiles.

This -w trivial-casts diag seems like a false-positive here,
i'm not quite sure how it can be easily avoided.

Other cases

Rust Version
rustc 1.100.0-nightly (8fa1c96cf 2026-08-17)
binary: rustc
commit-hash: 8fa1c96cfd489e4c27654c144ae871ce2c4db6c6
commit-date: 2026-08-17
host: x86_64-unknown-linux-gnu
release: 1.100.0-nightly
LLVM version: 23.1.0
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported warning with the Rust code in the issue and inspect the compiler's trivial_casts lint implementation. Verify the behavior around the FnDef-to-FnPtr cast, then add coverage for this example and confirm the lint produces no diagnostic while the code still compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.