d647696c1f
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 -.-
157 lines
5.7 KiB
Rust
157 lines
5.7 KiB
Rust
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
|
|
use clippy_utils::source::snippet_with_applicability;
|
|
use if_chain::if_chain;
|
|
use rustc_ast::ast;
|
|
use rustc_ast::visit as ast_visit;
|
|
use rustc_ast::visit::Visitor as AstVisitor;
|
|
use rustc_errors::Applicability;
|
|
use rustc_hir as hir;
|
|
use rustc_hir::intravisit as hir_visit;
|
|
use rustc_hir::intravisit::Visitor as HirVisitor;
|
|
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
|
|
use rustc_middle::hir::map::Map;
|
|
use rustc_middle::lint::in_external_macro;
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
|
|
|
declare_clippy_lint! {
|
|
/// ### What it does
|
|
/// Detects closures called in the same expression where they
|
|
/// are defined.
|
|
///
|
|
/// ### Why is this bad?
|
|
/// It is unnecessarily adding to the expression's
|
|
/// complexity.
|
|
///
|
|
/// ### Example
|
|
/// ```rust,ignore
|
|
/// // Bad
|
|
/// let a = (|| 42)()
|
|
///
|
|
/// // Good
|
|
/// let a = 42
|
|
/// ```
|
|
#[clippy::version = "pre 1.29.0"]
|
|
pub REDUNDANT_CLOSURE_CALL,
|
|
complexity,
|
|
"throwaway closures called in the expression they are defined"
|
|
}
|
|
|
|
declare_lint_pass!(RedundantClosureCall => [REDUNDANT_CLOSURE_CALL]);
|
|
|
|
// Used to find `return` statements or equivalents e.g., `?`
|
|
struct ReturnVisitor {
|
|
found_return: bool,
|
|
}
|
|
|
|
impl ReturnVisitor {
|
|
#[must_use]
|
|
fn new() -> Self {
|
|
Self { found_return: false }
|
|
}
|
|
}
|
|
|
|
impl<'ast> ast_visit::Visitor<'ast> for ReturnVisitor {
|
|
fn visit_expr(&mut self, ex: &'ast ast::Expr) {
|
|
if let ast::ExprKind::Ret(_) | ast::ExprKind::Try(_) = ex.kind {
|
|
self.found_return = true;
|
|
}
|
|
|
|
ast_visit::walk_expr(self, ex);
|
|
}
|
|
}
|
|
|
|
impl EarlyLintPass for RedundantClosureCall {
|
|
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
|
|
if in_external_macro(cx.sess, expr.span) {
|
|
return;
|
|
}
|
|
if_chain! {
|
|
if let ast::ExprKind::Call(ref paren, _) = expr.kind;
|
|
if let ast::ExprKind::Paren(ref closure) = paren.kind;
|
|
if let ast::ExprKind::Closure(_, _, _, ref decl, ref block, _) = closure.kind;
|
|
then {
|
|
let mut visitor = ReturnVisitor::new();
|
|
visitor.visit_expr(block);
|
|
if !visitor.found_return {
|
|
span_lint_and_then(
|
|
cx,
|
|
REDUNDANT_CLOSURE_CALL,
|
|
expr.span,
|
|
"try not to call a closure in the expression where it is declared",
|
|
|diag| {
|
|
if decl.inputs.is_empty() {
|
|
let mut app = Applicability::MachineApplicable;
|
|
let hint =
|
|
snippet_with_applicability(cx, block.span, "..", &mut app).into_owned();
|
|
diag.span_suggestion(expr.span, "try doing something like", hint, app);
|
|
}
|
|
},
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall {
|
|
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx hir::Block<'_>) {
|
|
fn count_closure_usage<'a, 'tcx>(
|
|
cx: &'a LateContext<'tcx>,
|
|
block: &'tcx hir::Block<'_>,
|
|
path: &'tcx hir::Path<'tcx>,
|
|
) -> usize {
|
|
struct ClosureUsageCount<'a, 'tcx> {
|
|
cx: &'a LateContext<'tcx>,
|
|
path: &'tcx hir::Path<'tcx>,
|
|
count: usize,
|
|
}
|
|
impl<'a, 'tcx> hir_visit::Visitor<'tcx> for ClosureUsageCount<'a, 'tcx> {
|
|
type Map = Map<'tcx>;
|
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
|
|
if_chain! {
|
|
if let hir::ExprKind::Call(closure, _) = expr.kind;
|
|
if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = closure.kind;
|
|
if self.path.segments[0].ident == path.segments[0].ident;
|
|
if self.path.res == path.res;
|
|
then {
|
|
self.count += 1;
|
|
}
|
|
}
|
|
hir_visit::walk_expr(self, expr);
|
|
}
|
|
|
|
fn nested_visit_map(&mut self) -> hir_visit::NestedVisitorMap<Self::Map> {
|
|
hir_visit::NestedVisitorMap::OnlyBodies(self.cx.tcx.hir())
|
|
}
|
|
}
|
|
let mut closure_usage_count = ClosureUsageCount { cx, path, count: 0 };
|
|
closure_usage_count.visit_block(block);
|
|
closure_usage_count.count
|
|
}
|
|
|
|
for w in block.stmts.windows(2) {
|
|
if_chain! {
|
|
if let hir::StmtKind::Local(local) = w[0].kind;
|
|
if let Option::Some(t) = local.init;
|
|
if let hir::ExprKind::Closure(..) = t.kind;
|
|
if let hir::PatKind::Binding(_, _, ident, _) = local.pat.kind;
|
|
if let hir::StmtKind::Semi(second) = w[1].kind;
|
|
if let hir::ExprKind::Assign(_, call, _) = second.kind;
|
|
if let hir::ExprKind::Call(closure, _) = call.kind;
|
|
if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = closure.kind;
|
|
if ident == path.segments[0].ident;
|
|
if count_closure_usage(cx, block, path) == 1;
|
|
then {
|
|
span_lint(
|
|
cx,
|
|
REDUNDANT_CLOSURE_CALL,
|
|
second.span,
|
|
"closure called just once immediately after it was declared",
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|