Skip to content

Fix bad suggestion for clone/is_some in field init shorthand #109355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2023
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
29 changes: 19 additions & 10 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -985,13 +985,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
)
.must_apply_modulo_regions()
{
diag.span_suggestion_verbose(
expr.span.shrink_to_hi(),
"consider using clone here",
".clone()",
Applicability::MachineApplicable,
);
return true;
let suggestion = match self.maybe_get_struct_pattern_shorthand_field(expr) {
Some(ident) => format!(": {}.clone()", ident),
None => ".clone()".to_string()
};

diag.span_suggestion_verbose(
expr.span.shrink_to_hi(),
"consider using clone here",
suggestion,
Applicability::MachineApplicable,
);
return true;
}
false
}
Expand Down Expand Up @@ -1153,13 +1158,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return false;
}

diag.span_suggestion(
let suggestion = match self.maybe_get_struct_pattern_shorthand_field(expr) {
Some(ident) => format!(": {}.is_some()", ident),
None => ".is_some()".to_string(),
};

diag.span_suggestion_verbose(
expr.span.shrink_to_hi(),
"use `Option::is_some` to test if the `Option` has a value",
".is_some()",
suggestion,
Applicability::MachineApplicable,
);

true
}

Expand Down
29 changes: 29 additions & 0 deletions tests/ui/suggestions/issue-108470.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// run-rustfix
#![allow(dead_code)]

struct Foo {
t: Thing
}

#[derive(Clone)]
struct Thing;

fn test_clone() {
let t = &Thing;
let _f = Foo {
t: t.clone() //~ ERROR mismatched types
};
}

struct Bar {
t: bool
}

fn test_is_some() {
let t = Option::<i32>::Some(1);
let _f = Bar {
t: t.is_some() //~ ERROR mismatched types
};
}

fn main() {}
29 changes: 29 additions & 0 deletions tests/ui/suggestions/issue-108470.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// run-rustfix
#![allow(dead_code)]

struct Foo {
t: Thing
}

#[derive(Clone)]
struct Thing;

fn test_clone() {
let t = &Thing;
let _f = Foo {
t //~ ERROR mismatched types
};
}

struct Bar {
t: bool
}

fn test_is_some() {
let t = Option::<i32>::Some(1);
let _f = Bar {
t //~ ERROR mismatched types
};
}

fn main() {}
27 changes: 27 additions & 0 deletions tests/ui/suggestions/issue-108470.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error[E0308]: mismatched types
--> $DIR/issue-108470.rs:14:9
|
LL | t
| ^ expected `Thing`, found `&Thing`
|
help: consider using clone here
|
LL | t: t.clone()
| +++++++++++

error[E0308]: mismatched types
--> $DIR/issue-108470.rs:25:9
|
LL | t
| ^ 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 | t: t.is_some()
| +++++++++++++

error: aborting due to 2 previous errors

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