-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
86 additions
and
4 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
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,48 @@ | ||
use crate::utils::{match_def_path, paths, span_lint}; | ||
use if_chain::if_chain; | ||
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 for empty closure when calling `std::thread::spawn`. | ||
/// | ||
/// **Why is this bad?** Empty closure does nothing, it can be removed. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust | ||
/// std::thread::spawn(|| {}); | ||
/// ``` | ||
pub EMPTY_CLOSURE, | ||
style, | ||
"closure with empty body" | ||
} | ||
|
||
declare_lint_pass!(EmptyClosure => [EMPTY_CLOSURE]); | ||
|
||
impl LateLintPass<'_, '_> for EmptyClosure { | ||
fn check_expr(&mut self, cx: &LateContext<'_, '_>, expr: &'_ Expr<'_>) { | ||
if_chain! { | ||
if let ExprKind::Call(ref path_expr, ref args) = expr.kind; | ||
if args.len() == 1; | ||
if let ExprKind::Path(ref qpath) = path_expr.kind; | ||
if let Some(def_id) = cx.tables.qpath_res(qpath, path_expr.hir_id).opt_def_id(); | ||
if match_def_path(cx, def_id, &paths::THREAD_SPAWN); | ||
if let ExprKind::Closure(_, _, body_id, ..) = args[0].kind; | ||
let body = cx.tcx.hir().body(body_id); | ||
if let ExprKind::Block(ref b, _) = body.value.kind; | ||
if b.stmts.is_empty() && b.expr.is_none(); | ||
then { | ||
span_lint( | ||
cx, | ||
EMPTY_CLOSURE, | ||
args[0].span, | ||
"Empty closure may be removed", | ||
); | ||
} | ||
} | ||
} | ||
} |
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
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,8 @@ | ||
#![warn(clippy::empty_closure)] | ||
|
||
fn main() { | ||
// Lint | ||
std::thread::spawn(|| {}); | ||
// No lint | ||
vec![0, 1, 2].iter().map(|_| {}).collect::<Vec<()>>(); | ||
} |
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,10 @@ | ||
error: Empty closure may be removed | ||
--> $DIR/empty_closure.rs:5:24 | ||
| | ||
LL | std::thread::spawn(|| {}); | ||
| ^^^^^ | ||
| | ||
= note: `-D clippy::empty-closure` implied by `-D warnings` | ||
|
||
error: aborting due to previous error | ||
|
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
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