Skip to content

Incorrect suggestion of function_item_references lint #78571

Closed
@tesuji

Description

@tesuji

The function_item_references lint suggests a wrong function item cast.

I tried this code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=d46c30ab809202ea2b88891e529da4f9

fn main() {
    use std::mem::size_of;
    type Left = [u8; 16];
    println!("{:p}", &size_of::<Left>);
    // println!("{:p}", size_of as fn() -> _);
}

The compiler suggests this:

warning: taking a reference to a function item does not give a function pointer
 --> src/main.rs:4:22
  |
4 |     println!("{:p}", &size_of::<Left>);
  |                      ^^^^^^^^^^^^^^^^ help: cast `size_of` to obtain a function pointer: `size_of as fn() -> _`
  |
  = note: `#[warn(function_item_references)]` on by default

However, following the suggestion causes a compiler error:

error[E0282]: type annotations needed
 --> src/main.rs:5:22
  |
5 |     println!("{:p}", size_of as fn() -> _);
  |                      ^^^^^^^ cannot infer type for type parameter `T` declared on the function `size_of`

I would expect the compiler suggests size_of::<Left> as fn() -> _ instead.

Meta

rustc --version --verbose: (2020-10-29 6bdae9e) from playground

cc #76269

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.T-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