Skip to content

Add parenthesis around suggestions to use .into() on cast expr #47699

Closed
@estebank

Description

@estebank

Given the following code:

fn main() {
    let array = [1, 2, 3];
    test(array.len() as u8);
}

fn test(length: u32) {
    println!("{}", length);
}

rustc suggests

error[E0308]: mismatched types
 --> src/main.rs:3:10
  |
3 |     test(array.len() as u8);
  |          ^^^^^^^^^^^^^^^^^ expected u32, found u8
help: you can cast an `u8` to `u32`, which will zero-extend the source value
  |
3 |     test(array.len() as u8.into());
  |          ^^^^^^^^^^^^^^^^^^^^^^^^

The suggestion should be enclosing the entire expression in parenthesis.

This was introduced in #47247. The solution is to change needs_paren to be inclusive of the precedence of as, in other words, change the < with <= and add a test for the behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions