Skip to content

Suggest dereferencing boolean reference when used in 'if' #64557

Closed
@Aaron1011

Description

@Aaron1011

The following code:

fn take_bool(val: bool) {}

fn main() {
    let a = &true;
    if a {
        println!("Hello!");
    }
    take_bool(a)
}

gives the following errors:

error[E0308]: mismatched types
 --> src/main.rs:5:8
  |
5 |     if a {
  |        ^ expected bool, found &bool
  |
  = note: expected type `bool`
             found type `&bool`

error[E0308]: mismatched types
 --> src/main.rs:8:15
  |
8 |     take_bool(a)
  |               ^
  |               |
  |               expected bool, found &bool
  |               help: consider dereferencing the borrow: `*a`
  |
  = note: expected type `bool`
             found type `&bool`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
error: Could not compile `playground`.

Note that the first error is missing the "help: consider dereferencing the borrow: *a" suggestion.

For consistency, Rust should suggest dereferencing a boolean reference when used in an 'if' expression.

This issue has been assigned to @XiangQingW via this comment.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-feature-requestCategory: A feature request, i.e: not implemented / a PR.D-papercutDiagnostics: An error or lint that needs small tweaks.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.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