Skip to content

Commit

Permalink
Make it into a structured suggestion, maybe-incorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Jun 28, 2024
1 parent 8960a12 commit 284d3cf
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 2 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ lint_check_name_unknown_tool = unknown lint tool: `{$tool_name}`
lint_closure_returning_async_block = closure returning async block can be made into an async closure
.label = this async block can be removed, and the closure can be turned into an async closure
.suggestion = turn this into an async closure
lint_command_line_source = `forbid` lint level was set on command line
Expand Down
23 changes: 21 additions & 2 deletions compiler/rustc_lint/src/async_closures.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustc_hir as hir;
use rustc_macros::LintDiagnostic;
use rustc_macros::{LintDiagnostic, Subdiagnostic};
use rustc_session::{declare_lint, declare_lint_pass};
use rustc_span::Span;

Expand Down Expand Up @@ -91,11 +91,19 @@ impl<'tcx> LateLintPass<'tcx> for AsyncClosureUsage {
return;
};

let deletion_span = cx.tcx.sess.source_map().span_extend_while_whitespace(async_decl_span);

cx.tcx.emit_node_span_lint(
CLOSURE_RETURNING_ASYNC_BLOCK,
expr.hir_id,
fn_decl_span,
ClosureReturningAsyncBlock { async_decl_span },
ClosureReturningAsyncBlock {
async_decl_span,
sugg: AsyncClosureSugg {
deletion_span,
insertion_span: fn_decl_span.shrink_to_lo(),
},
},
);
}
}
Expand All @@ -105,4 +113,15 @@ impl<'tcx> LateLintPass<'tcx> for AsyncClosureUsage {
struct ClosureReturningAsyncBlock {
#[label]
async_decl_span: Span,
#[subdiagnostic]
sugg: AsyncClosureSugg,
}

#[derive(Subdiagnostic)]
#[multipart_suggestion(lint_suggestion, applicability = "maybe-incorrect")]
struct AsyncClosureSugg {
#[suggestion_part(code = "")]
deletion_span: Span,
#[suggestion_part(code = "async ")]
insertion_span: Span,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//@ edition: 2021

#![feature(async_closure)]
#![deny(closure_returning_async_block)]

fn main() {
let x = || async {};
//~^ ERROR closure returning async block can be made into an async closure

let x = || async move {};
//~^ ERROR closure returning async block can be made into an async closure

let x = move || async move {};
//~^ ERROR closure returning async block can be made into an async closure

let x = move || async {};
//~^ ERROR closure returning async block can be made into an async closure

let x = || {{ async {} }};
//~^ ERROR closure returning async block can be made into an async closure
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
error: closure returning async block can be made into an async closure
--> $DIR/lint-closure-returning-async-block.rs:7:13
|
LL | let x = || async {};
| ^^ ----- this async block can be removed, and the closure can be turned into an async closure
|
note: the lint level is defined here
--> $DIR/lint-closure-returning-async-block.rs:4:9
|
LL | #![deny(closure_returning_async_block)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: turn this into an async closure
|
LL - let x = || async {};
LL + let x = async || {};
|

error: closure returning async block can be made into an async closure
--> $DIR/lint-closure-returning-async-block.rs:10:13
|
LL | let x = || async move {};
| ^^ ---------- this async block can be removed, and the closure can be turned into an async closure
|
help: turn this into an async closure
|
LL - let x = || async move {};
LL + let x = async || {};
|

error: closure returning async block can be made into an async closure
--> $DIR/lint-closure-returning-async-block.rs:13:13
|
LL | let x = move || async move {};
| ^^^^^^^ ---------- this async block can be removed, and the closure can be turned into an async closure
|
help: turn this into an async closure
|
LL - let x = move || async move {};
LL + let x = async move || {};
|

error: closure returning async block can be made into an async closure
--> $DIR/lint-closure-returning-async-block.rs:16:13
|
LL | let x = move || async {};
| ^^^^^^^ ----- this async block can be removed, and the closure can be turned into an async closure
|
help: turn this into an async closure
|
LL - let x = move || async {};
LL + let x = async move || {};
|

error: closure returning async block can be made into an async closure
--> $DIR/lint-closure-returning-async-block.rs:19:13
|
LL | let x = || {{ async {} }};
| ^^ ----- this async block can be removed, and the closure can be turned into an async closure
|
help: turn this into an async closure
|
LL - let x = || {{ async {} }};
LL + let x = async || {{ {} }};
|

error: aborting due to 5 previous errors

0 comments on commit 284d3cf

Please sign in to comment.