2021-03-15 19:55:45 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2021-03-14 18:17:44 -05:00
|
|
|
use clippy_utils::source::snippet;
|
2021-03-13 17:01:03 -06:00
|
|
|
use clippy_utils::ty::implements_trait;
|
2020-08-14 19:30:48 -05:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::{AsyncGeneratorKind, Body, BodyId, ExprKind, GeneratorKind, QPath};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-02 13:37:11 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for async blocks that yield values of types
|
2020-08-22 23:40:01 -05:00
|
|
|
/// that can themselves be awaited.
|
2020-08-14 19:30:48 -05:00
|
|
|
///
|
2021-07-02 13:37:11 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// An await is likely missing.
|
2020-08-14 19:30:48 -05:00
|
|
|
///
|
2021-07-02 13:37:11 -05:00
|
|
|
/// ### Example
|
2020-08-14 19:30:48 -05:00
|
|
|
/// ```rust
|
|
|
|
/// async fn foo() {}
|
|
|
|
///
|
|
|
|
/// fn bar() {
|
|
|
|
/// let x = async {
|
|
|
|
/// foo()
|
|
|
|
/// };
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// async fn foo() {}
|
|
|
|
///
|
|
|
|
/// fn bar() {
|
|
|
|
/// let x = async {
|
|
|
|
/// foo().await
|
|
|
|
/// };
|
|
|
|
/// }
|
|
|
|
/// ```
|
Added `clippy::version` attribute to all normal lints
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`...
And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun...
Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work.
```nu
mv v0.0.212 rust-1.00.0;
mv beta rust-1.57.0;
mv master rust-1.58.0;
let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path);
let versions = (
ls | where name =~ "rust-" | select name | format {name}/lints.json |
each { open $it | select id | insert version $it | str substring "5,11" version} |
group-by id | rotate counter-clockwise id version |
update version {get version | first 1} | flatten | select id version);
$paths | each { |row|
let version = ($versions | where id == ($row.id) | format {version})
let idu = ($row.id | str upcase)
$"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)"
} | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh";
```
And this still has some problems, but at this point I just want to be done -.-
2021-10-21 14:06:26 -05:00
|
|
|
#[clippy::version = "1.48.0"]
|
2020-08-14 19:30:48 -05:00
|
|
|
pub ASYNC_YIELDS_ASYNC,
|
|
|
|
correctness,
|
|
|
|
"async blocks that return a type that can be awaited"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(AsyncYieldsAsync => [ASYNC_YIELDS_ASYNC]);
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for AsyncYieldsAsync {
|
|
|
|
fn check_body(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
|
|
|
|
use AsyncGeneratorKind::{Block, Closure};
|
|
|
|
// For functions, with explicitly defined types, don't warn.
|
|
|
|
// XXXkhuey maybe we should?
|
|
|
|
if let Some(GeneratorKind::Async(Block | Closure)) = body.generator_kind {
|
|
|
|
if let Some(future_trait_def_id) = cx.tcx.lang_items().future_trait() {
|
|
|
|
let body_id = BodyId {
|
|
|
|
hir_id: body.value.hir_id,
|
|
|
|
};
|
2021-03-05 10:35:39 -06:00
|
|
|
let typeck_results = cx.tcx.typeck_body(body_id);
|
2020-08-14 19:30:48 -05:00
|
|
|
let expr_ty = typeck_results.expr_ty(&body.value);
|
|
|
|
|
|
|
|
if implements_trait(cx, expr_ty, future_trait_def_id, &[]) {
|
|
|
|
let return_expr_span = match &body.value.kind {
|
|
|
|
// XXXkhuey there has to be a better way.
|
|
|
|
ExprKind::Block(block, _) => block.expr.map(|e| e.span),
|
|
|
|
ExprKind::Path(QPath::Resolved(_, path)) => Some(path.span),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
if let Some(return_expr_span) = return_expr_span {
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
ASYNC_YIELDS_ASYNC,
|
|
|
|
return_expr_span,
|
|
|
|
"an async construct yields a type which is itself awaitable",
|
|
|
|
|db| {
|
|
|
|
db.span_label(body.value.span, "outer async construct");
|
|
|
|
db.span_label(return_expr_span, "awaitable value not awaited");
|
|
|
|
db.span_suggestion(
|
|
|
|
return_expr_span,
|
|
|
|
"consider awaiting this value",
|
|
|
|
format!("{}.await", snippet(cx, return_expr_span, "..")),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|