Skip to content
Open
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
17 changes: 17 additions & 0 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,23 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
&& expected.starts_with("struct")
{
("`async` blocks are only allowed in Rust 2018 or later".to_string(), suggestion)
} else if item_str.as_str() == "void"
&& let Some((FnKind::Fn(.., fun), ..)) = self.diag_metadata.current_function
&& let ast::FnRetTy::Ty(ty) = &fun.sig.decl.output
&& ty.span == item_span
{
// check for accidental use of `void` as a return type
let return_ty_span =
self.r.tcx.sess.source_map().span_extend_to_prev_char(item_span, ')', true);

(
"functions with no return value use `()`".to_string(),
Some((
return_ty_span,
"omit the return type to return `()` implicitly",
"".to_owned(),
)),
)
} else {
// check if we are in situation of typo like `True` instead of `true`.
let override_suggestion =
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/suggestions/return-void.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![crate_type = "lib"]

pub fn foo() -> void {} //~ ERROR cannot find type `void`

pub fn bar(v: void) {} //~ ERROR cannot find type `void`
21 changes: 21 additions & 0 deletions tests/ui/suggestions/return-void.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0425]: cannot find type `void` in this scope
--> $DIR/return-void.rs:3:17
|
LL | pub fn foo() -> void {}
| ^^^^ functions with no return value use `()`
|
help: omit the return type to return `()` implicitly
|
LL - pub fn foo() -> void {}
LL + pub fn foo() {}
|

error[E0425]: cannot find type `void` in this scope
--> $DIR/return-void.rs:5:15
|
LL | pub fn bar(v: void) {}
| ^^^^ not found in this scope

error: aborting due to 2 previous errors

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