Skip to content

Commit 9c08269

Browse files
committed
add_type_neq_err_label: don't .unwrap
1 parent 537ccdf commit 9c08269

File tree

3 files changed

+65
-18
lines changed

3 files changed

+65
-18
lines changed

src/librustc_typeck/check/op.rs

+14-18
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
481481
}
482482

483483
/// If one of the types is an uncalled function and calling it would yield the other type,
484-
/// suggest calling the function. Returns whether a suggestion was given.
484+
/// suggest calling the function. Returns `true` if suggestion would apply (even if not given).
485485
fn add_type_neq_err_label(
486486
&self,
487487
err: &mut rustc_errors::DiagnosticBuilder<'_>,
@@ -514,24 +514,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
514514
.lookup_op_method(fn_sig.output(), &[other_ty], Op::Binary(op, is_assign))
515515
.is_ok()
516516
{
517-
let (variable_snippet, applicability) = if !fn_sig.inputs().is_empty() {
518-
(
519-
format!("{}( /* arguments */ )", source_map.span_to_snippet(span).unwrap()),
520-
Applicability::HasPlaceholders,
521-
)
522-
} else {
523-
(
524-
format!("{}()", source_map.span_to_snippet(span).unwrap()),
525-
Applicability::MaybeIncorrect,
526-
)
527-
};
517+
if let Ok(snippet) = source_map.span_to_snippet(span) {
518+
let (variable_snippet, applicability) = if !fn_sig.inputs().is_empty() {
519+
(format!("{}( /* arguments */ )", snippet), Applicability::HasPlaceholders)
520+
} else {
521+
(format!("{}()", snippet), Applicability::MaybeIncorrect)
522+
};
528523

529-
err.span_suggestion(
530-
span,
531-
"you might have forgotten to call this function",
532-
variable_snippet,
533-
applicability,
534-
);
524+
err.span_suggestion(
525+
span,
526+
"you might have forgotten to call this function",
527+
variable_snippet,
528+
applicability,
529+
);
530+
}
535531
return true;
536532
}
537533
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn a() -> i32 {
2+
3
3+
}
4+
5+
pub fn main() {
6+
assert_eq!(a, 0);
7+
//~^ ERROR binary operation `==` cannot
8+
//~| ERROR mismatched types
9+
//~| ERROR doesn't implement
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
error[E0369]: binary operation `==` cannot be applied to type `fn() -> i32 {a}`
2+
--> $DIR/issue-70724-add_type_neq_err_label-unwrap.rs:6:5
3+
|
4+
LL | assert_eq!(a, 0);
5+
| ^^^^^^^^^^^^^^^^^
6+
| |
7+
| fn() -> i32 {a}
8+
| {integer}
9+
| help: you might have forgotten to call this function: `*left_val()`
10+
|
11+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/issue-70724-add_type_neq_err_label-unwrap.rs:6:5
15+
|
16+
LL | assert_eq!(a, 0);
17+
| ^^^^^^^^^^^^^^^^^ expected fn item, found integer
18+
|
19+
= note: expected fn item `fn() -> i32 {a}`
20+
found type `i32`
21+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
22+
23+
error[E0277]: `fn() -> i32 {a}` doesn't implement `std::fmt::Debug`
24+
--> $DIR/issue-70724-add_type_neq_err_label-unwrap.rs:6:5
25+
|
26+
LL | fn a() -> i32 {
27+
| - consider calling this function
28+
...
29+
LL | assert_eq!(a, 0);
30+
| ^^^^^^^^^^^^^^^^^ `fn() -> i32 {a}` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`
31+
|
32+
= help: the trait `std::fmt::Debug` is not implemented for `fn() -> i32 {a}`
33+
= help: use parentheses to call the function: `a()`
34+
= note: required because of the requirements on the impl of `std::fmt::Debug` for `&fn() -> i32 {a}`
35+
= note: required by `std::fmt::Debug::fmt`
36+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
37+
38+
error: aborting due to 3 previous errors
39+
40+
Some errors have detailed explanations: E0277, E0308, E0369.
41+
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)