Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1692,12 +1692,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}

let suggestion = match self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr) {
Some(ident) => format!(": {ident}.is_some()"),
None => ".is_some()".to_string(),
Some(ident) => vec![(expr.span.shrink_to_hi(), format!(": {ident}.is_some()"))],
None if self.precedence(expr) < ExprPrecedence::Unambiguous => {
// Apply the method to the whole expression, e.g. `(*value).is_some()`.
vec![
(expr.span.shrink_to_lo(), "(".to_string()),
(expr.span.shrink_to_hi(), ").is_some()".to_string()),
]
}
None => vec![(expr.span.shrink_to_hi(), ".is_some()".to_string())],
};

diag.span_suggestion_verbose(
expr.span.shrink_to_hi(),
diag.multipart_suggestion(
"use `Option::is_some` to test if the `Option` has a value",
suggestion,
Applicability::MachineApplicable,
Expand Down
35 changes: 35 additions & 0 deletions tests/ui/suggestions/option-to-bool.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// An `is_some()` suggestion must apply to the whole expression, preserving its precedence.
//@ run-rustfix

fn foo(x: Option<i32>) {
if true && x.is_some() {}
//~^ ERROR mismatched types
//~| HELP use `Option::is_some` to test if the `Option` has a value

let reference = &x;
if (*reference).is_some() {}
//~^ ERROR mismatched types
//~| HELP use `Option::is_some` to test if the `Option` has a value

if (&x).is_some() {}
//~^ ERROR mismatched types
//~| HELP use `Option::is_some` to test if the `Option` has a value

if (Value + Value).is_some() {}
//~^ ERROR mismatched types
//~| HELP use `Option::is_some` to test if the `Option` has a value
}

struct Value;

impl std::ops::Add for Value {
type Output = Option<i32>;

fn add(self, _: Self) -> Self::Output {
Some(1)
}
}

fn main() {
foo(Some(1));
}
30 changes: 29 additions & 1 deletion tests/ui/suggestions/option-to-bool.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
// An `is_some()` suggestion must apply to the whole expression, preserving its precedence.
//@ run-rustfix

fn foo(x: Option<i32>) {
if true && x {}
//~^ ERROR mismatched types
//~| HELP use `Option::is_some` to test if the `Option` has a value

let reference = &x;
if *reference {}
//~^ ERROR mismatched types
//~| HELP use `Option::is_some` to test if the `Option` has a value

if &x {}
//~^ ERROR mismatched types
//~| HELP use `Option::is_some` to test if the `Option` has a value

if Value + Value {}
//~^ ERROR mismatched types
//~| HELP use `Option::is_some` to test if the `Option` has a value
}

fn main() {}
struct Value;

impl std::ops::Add for Value {
type Output = Option<i32>;

fn add(self, _: Self) -> Self::Output {
Some(1)
}
}

fn main() {
foo(Some(1));
}
43 changes: 41 additions & 2 deletions tests/ui/suggestions/option-to-bool.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0308]: mismatched types
--> $DIR/option-to-bool.rs:2:16
--> $DIR/option-to-bool.rs:5:16
|
LL | if true && x {}
| ---- ^ expected `bool`, found `Option<i32>`
Expand All @@ -13,6 +13,45 @@ help: use `Option::is_some` to test if the `Option` has a value
LL | if true && x.is_some() {}
| ++++++++++

error: aborting due to 1 previous error
error[E0308]: mismatched types
--> $DIR/option-to-bool.rs:10:8
|
LL | if *reference {}
| ^^^^^^^^^^ expected `bool`, found `Option<i32>`
|
= note: expected type `bool`
found enum `Option<i32>`
help: use `Option::is_some` to test if the `Option` has a value
|
LL | if (*reference).is_some() {}
| + +++++++++++

error[E0308]: mismatched types
--> $DIR/option-to-bool.rs:14:8
|
LL | if &x {}
| ^^ expected `bool`, found `&Option<i32>`
|
= note: expected type `bool`
found reference `&Option<i32>`
help: use `Option::is_some` to test if the `Option` has a value
|
LL | if (&x).is_some() {}
| + +++++++++++

error[E0308]: mismatched types
--> $DIR/option-to-bool.rs:18:8
|
LL | if Value + Value {}
| ^^^^^^^^^^^^^ expected `bool`, found `Option<i32>`
|
= note: expected type `bool`
found enum `Option<i32>`
help: use `Option::is_some` to test if the `Option` has a value
|
LL | if (Value + Value).is_some() {}
| + +++++++++++

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0308`.
Loading