Skip to content

Commit 78fc7c3

Browse files
committed
Suggest unwrapping when private method name is available in inner type
Given ```rust fn main() { let maybe_vec = Some(vec![1,2,3]); assert_eq!(maybe_vec.len(), 3); } ``` suggest unwraping `maybe_vec` to call `.len()` on the `Vec<_>`. ``` error[E0624]: method `len` is private --> $DIR/enum-method-probe.rs:61:9 | LL | res.len(); | ^^^ private method --> $SRC_DIR/core/src/option.rs:LL:COL | = note: private method defined here | note: the method `len` exists on the type `Vec<{integer}>` --> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL help: consider using `Option::expect` to unwrap the `Vec<{integer}>` value, panicking if the value is an `Option::None` | LL | res.expect("REASON").len(); | +++++++++++++++++ ```
1 parent ee3a078 commit 78fc7c3

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
264264
err.span_label(within_macro_span, "due to this macro variable");
265265
}
266266
self.suggest_valid_traits(&mut err, item_name, out_of_scope_traits, true);
267+
self.suggest_unwrapping_inner_self(&mut err, source, rcvr_ty, item_name);
267268
err.emit()
268269
}
269270

tests/ui/suggestions/enum-method-probe.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,11 @@ fn test_option_in_unit_return() {
5656
//~| HELP consider using `Option::expect` to unwrap the `Foo` value, panicking if the value is an `Option::None`
5757
}
5858

59+
fn test_option_private_method() {
60+
let res: Option<_> = Some(vec![1, 2, 3]);
61+
res.expect("REASON").len();
62+
//~^ ERROR method `len` is private
63+
//~| HELP consider using `Option::expect` to unwrap the `Vec<{integer}>` value, panicking if the value is an `Option::None`
64+
}
65+
5966
fn main() {}

tests/ui/suggestions/enum-method-probe.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,11 @@ fn test_option_in_unit_return() {
5656
//~| HELP consider using `Option::expect` to unwrap the `Foo` value, panicking if the value is an `Option::None`
5757
}
5858

59+
fn test_option_private_method() {
60+
let res: Option<_> = Some(vec![1, 2, 3]);
61+
res.len();
62+
//~^ ERROR method `len` is private
63+
//~| HELP consider using `Option::expect` to unwrap the `Vec<{integer}>` value, panicking if the value is an `Option::None`
64+
}
65+
5966
fn main() {}

tests/ui/suggestions/enum-method-probe.stderr

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ help: consider using `Option::expect` to unwrap the `Foo` value, panicking if th
9494
LL | res.expect("REASON").get();
9595
| +++++++++++++++++
9696

97-
error: aborting due to 6 previous errors
97+
error[E0624]: method `len` is private
98+
--> $DIR/enum-method-probe.rs:61:9
99+
|
100+
LL | res.len();
101+
| ^^^ private method
102+
--> $SRC_DIR/core/src/option.rs:LL:COL
103+
|
104+
= note: private method defined here
105+
|
106+
note: the method `len` exists on the type `Vec<{integer}>`
107+
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
108+
help: consider using `Option::expect` to unwrap the `Vec<{integer}>` value, panicking if the value is an `Option::None`
109+
|
110+
LL | res.expect("REASON").len();
111+
| +++++++++++++++++
112+
113+
error: aborting due to 7 previous errors
98114

99-
For more information about this error, try `rustc --explain E0599`.
115+
Some errors have detailed explanations: E0599, E0624.
116+
For more information about an error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)