Skip to content

Suggest calling size_of when it is missing parens in a comparison #101054

Closed
@fitzgen

Description

@fitzgen

Given the following code:

fn is_one_byte<T>() -> bool {
    if std::mem::size_of::<T> == 1 {
        true
    } else {
        false
    }
}

The current output is:

Compiling playground v0.0.1 (/playground)
error[[E0369]](https://doc.rust-lang.org/stable/error-index.html#E0369): binary operation `==` cannot be applied to type `fn() -> usize {std::mem::size_of::<T>}`
 --> src/lib.rs:2:31
  |
2 |     if std::mem::size_of::<T> == 1 {
  |        ---------------------- ^^ - {integer}
  |        |
  |        fn() -> usize {std::mem::size_of::<T>}

error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
 --> src/lib.rs:2:34
  |
2 |     if std::mem::size_of::<T> == 1 {
  |                                  ^ expected fn item, found integer
  |
  = note: expected fn item `fn() -> usize {std::mem::size_of::<T>}`
                found type `{integer}`

Some errors have detailed explanations: E0308, E0369.
For more information about an error, try `rustc --explain E0308`.
error: could not compile `playground` due to 2 previous errors

Ideally the output should look like:

Try calling the `size_of` function:

    std::mem::size_of::<T>()
                          ^^ try calling the `size_of` function        

Probably worth not making this diagnostic suggestion specific to size_of but to any type error where there is a comparison of a function and the return type of that function.

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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