You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rollup merge of #151457 - enthropy7:fix-assert-macro-only, r=enthropy7
Improve error message for assert!() macro in functions returning bool
Detect `assert!()` macro in error messages and provide clearer diagnostics
When `assert!()` is used in a function returning a value, show a message explaining that `assert!()` doesn't return a value, instead of the generic "if may be missing an else clause" error.
Fixes#151446
| ---- expected `bool` because of this return type
6
+
LL | assert!(1 < 2)
7
+
| ^^^^^^^^^^^^^^ expected `bool`, found `()`
8
+
9
+
error[E0308]: mismatched types
10
+
--> $DIR/assert-macro-without-else.rs:9:5
11
+
|
12
+
LL | assert_eq!(1, 1)
13
+
| ^^^^^^^^^^^^^^^^ expected `i32`, found `()`
14
+
|
15
+
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
16
+
17
+
error[E0308]: mismatched types
18
+
--> $DIR/assert-macro-without-else.rs:14:5
19
+
|
20
+
LL | assert_ne!(1, 2)
21
+
| ^^^^^^^^^^^^^^^^ expected `bool`, found `()`
22
+
|
23
+
= note: this error originates in the macro `assert_ne` (in Nightly builds, run with -Z macro-backtrace for more info)
24
+
25
+
error[E0308]: mismatched types
26
+
--> $DIR/assert-macro-without-else.rs:26:9
27
+
|
28
+
LL | assert!(1 < 2)
29
+
| ^^^^^^^^^^^^^^ expected `bool`, found `()`
30
+
...
31
+
LL | fn nested() -> bool {
32
+
| ---- expected `bool` because of this return type
33
+
LL | g!()
34
+
| ---- in this macro invocation
35
+
|
36
+
= note: this error originates in the macro `assert` which comes from the expansion of the macro `g` (in Nightly builds, run with -Z macro-backtrace for more info)
37
+
38
+
error: aborting due to 4 previous errors
39
+
40
+
For more information about this error, try `rustc --explain E0308`.
0 commit comments