Skip to content

Commit 61a529d

Browse files
authored
Rollup merge of #100617 - chenyukang:fix-100605, r=compiler-errors
Suggest the right help message for as_ref Fixes #100605
2 parents c4b83eb + 3de74f7 commit 61a529d

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2079,7 +2079,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
20792079
diag.span_suggestion(
20802080
span,
20812081
*msg,
2082-
format!("{}.as_ref()", snippet),
2082+
// HACK: fix issue# 100605, suggesting convert from &Option<T> to Option<&T>, remove the extra `&`
2083+
format!("{}.as_ref()", snippet.trim_start_matches('&')),
20832084
Applicability::MachineApplicable,
20842085
);
20852086
}

src/test/ui/issues/issue-100605.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn takes_option(_arg: Option<&String>) {}
2+
3+
fn main() {
4+
takes_option(&None); //~ ERROR 4:18: 4:23: mismatched types [E0308]
5+
6+
let x = String::from("x");
7+
let res = Some(x);
8+
takes_option(&res); //~ ERROR 8:18: 8:22: mismatched types [E0308]
9+
}
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-100605.rs:4:18
3+
|
4+
LL | takes_option(&None);
5+
| ------------ ^^^^^ expected enum `Option`, found `&Option<_>`
6+
| |
7+
| arguments to this function are incorrect
8+
|
9+
= note: expected enum `Option<&String>`
10+
found reference `&Option<_>`
11+
note: function defined here
12+
--> $DIR/issue-100605.rs:1:4
13+
|
14+
LL | fn takes_option(_arg: Option<&String>) {}
15+
| ^^^^^^^^^^^^ ---------------------
16+
help: you can convert from `&Option<T>` to `Option<&T>` using `.as_ref()`
17+
|
18+
LL | takes_option(None.as_ref());
19+
| ~~~~~~~~~~~~~
20+
help: consider removing the borrow
21+
|
22+
LL - takes_option(&None);
23+
LL + takes_option(None);
24+
|
25+
26+
error[E0308]: mismatched types
27+
--> $DIR/issue-100605.rs:8:18
28+
|
29+
LL | takes_option(&res);
30+
| ------------ ^^^^
31+
| | |
32+
| | expected enum `Option`, found `&Option<String>`
33+
| | help: you can convert from `&Option<T>` to `Option<&T>` using `.as_ref()`: `res.as_ref()`
34+
| arguments to this function are incorrect
35+
|
36+
= note: expected enum `Option<&String>`
37+
found reference `&Option<String>`
38+
note: function defined here
39+
--> $DIR/issue-100605.rs:1:4
40+
|
41+
LL | fn takes_option(_arg: Option<&String>) {}
42+
| ^^^^^^^^^^^^ ---------------------
43+
44+
error: aborting due to 2 previous errors
45+
46+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)