-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #76574 - flip1995:clippyup, r=Manishearth
Update Clippy Biweekly Clippy update r? `@Manishearth`
- Loading branch information
Showing
54 changed files
with
1,360 additions
and
416 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use crate::utils::{implements_trait, snippet, span_lint_and_then}; | ||
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! { | ||
/// **What it does:** Checks for async blocks that yield values of types | ||
/// that can themselves be awaited. | ||
/// | ||
/// **Why is this bad?** An await is likely missing. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust | ||
/// async fn foo() {} | ||
/// | ||
/// fn bar() { | ||
/// let x = async { | ||
/// foo() | ||
/// }; | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// async fn foo() {} | ||
/// | ||
/// fn bar() { | ||
/// let x = async { | ||
/// foo().await | ||
/// }; | ||
/// } | ||
/// ``` | ||
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, | ||
}; | ||
let def_id = cx.tcx.hir().body_owner_def_id(body_id); | ||
let typeck_results = cx.tcx.typeck(def_id); | ||
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, | ||
); | ||
}, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use crate::utils::{match_def_path, paths, snippet, span_lint_and_sugg}; | ||
use if_chain::if_chain; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Checks usage of `std::fs::create_dir` and suggest using `std::fs::create_dir_all` instead. | ||
/// | ||
/// **Why is this bad?** Sometimes `std::fs::crate_dir` is mistakenly chosen over `std::fs::create_dir_all`. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust | ||
/// std::fs::create_dir("foo"); | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// std::fs::create_dir_all("foo"); | ||
/// ``` | ||
pub CREATE_DIR, | ||
restriction, | ||
"calling `std::fs::create_dir` instead of `std::fs::create_dir_all`" | ||
} | ||
|
||
declare_lint_pass!(CreateDir => [CREATE_DIR]); | ||
|
||
impl LateLintPass<'_> for CreateDir { | ||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { | ||
if_chain! { | ||
if let ExprKind::Call(ref func, ref args) = expr.kind; | ||
if let ExprKind::Path(ref path) = func.kind; | ||
if let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id(); | ||
if match_def_path(cx, def_id, &paths::STD_FS_CREATE_DIR); | ||
then { | ||
span_lint_and_sugg( | ||
cx, | ||
CREATE_DIR, | ||
expr.span, | ||
"calling `std::fs::create_dir` where there may be a better way", | ||
"consider calling `std::fs::create_dir_all` instead", | ||
format!("create_dir_all({})", snippet(cx, args[0].span, "..")), | ||
Applicability::MaybeIncorrect, | ||
) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.