Make it into a structured suggestion, maybe-incorrect

This commit is contained in:
Michael Goulet 2024-06-28 15:56:06 -04:00
parent d526adad25
commit acc13e29d1
4 changed files with 110 additions and 2 deletions

View File

@ -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

View File

@ -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;
@ -93,11 +93,19 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
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(),
},
},
);
}
}
@ -107,4 +115,15 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
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,
}

View File

@ -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
}

View File

@ -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