From c96cd359f532bb37865ef91d0935655a0af7e7f4 Mon Sep 17 00:00:00 2001 From: Serial <69764315+Serial-ATA@users.noreply.github.com> Date: Thu, 28 Oct 2021 21:39:41 -0400 Subject: [PATCH] Extend author lint --- clippy_lints/src/utils/author.rs | 611 ++++++++++++++++++++++++------- tests/ui/author.stdout | 2 +- tests/ui/author/blocks.rs | 9 + tests/ui/author/blocks.stdout | 34 +- tests/ui/author/call.stdout | 4 +- tests/ui/author/for_loop.rs | 8 - tests/ui/author/for_loop.stdout | 64 ---- tests/ui/author/if.rs | 7 + tests/ui/author/if.stdout | 40 +- tests/ui/author/loop.rs | 36 ++ tests/ui/author/loop.stdout | 112 ++++++ tests/ui/author/matches.stdout | 14 +- tests/ui/author/repeat.rs | 5 + tests/ui/author/repeat.stdout | 10 + tests/ui/author/struct.rs | 40 ++ tests/ui/author/struct.stdout | 61 +++ 16 files changed, 827 insertions(+), 230 deletions(-) delete mode 100644 tests/ui/author/for_loop.rs delete mode 100644 tests/ui/author/for_loop.stdout create mode 100644 tests/ui/author/loop.rs create mode 100644 tests/ui/author/loop.stdout create mode 100644 tests/ui/author/repeat.rs create mode 100644 tests/ui/author/repeat.stdout create mode 100644 tests/ui/author/struct.rs create mode 100644 tests/ui/author/struct.stdout diff --git a/clippy_lints/src/utils/author.rs b/clippy_lints/src/utils/author.rs index f93d7782e..38157cc16 100644 --- a/clippy_lints/src/utils/author.rs +++ b/clippy_lints/src/utils/author.rs @@ -1,13 +1,13 @@ //! A group of attributes that can be attached to Rust code in order //! to generate a clippy lint detecting said code automatically. -use clippy_utils::get_attr; +use clippy_utils::{get_attr, higher}; use rustc_ast::ast::{LitFloatType, LitKind}; -use rustc_ast::walk_list; +use rustc_ast::{walk_list, Label, LitIntType}; use rustc_data_structures::fx::FxHashMap; use rustc_hir as hir; use rustc_hir::intravisit::{NestedVisitorMap, Visitor}; -use rustc_hir::{Block, Expr, ExprKind, Pat, PatKind, QPath, Stmt, StmtKind, TyKind}; +use rustc_hir::{Arm, Block, Expr, ExprKind, FnRetTy, Lit, MatchSource, Pat, PatKind, QPath, Stmt, StmtKind, TyKind}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::hir::map::Map; use rustc_session::{declare_lint_pass, declare_tool_lint}; @@ -70,7 +70,7 @@ impl<'tcx> LateLintPass<'tcx> for Author { return; } prelude(); - PrintVisitor::new("item").visit_item(item); + PrintVisitor::new("item", cx).visit_item(item); done(); } @@ -79,7 +79,7 @@ impl<'tcx> LateLintPass<'tcx> for Author { return; } prelude(); - PrintVisitor::new("item").visit_impl_item(item); + PrintVisitor::new("item", cx).visit_impl_item(item); done(); } @@ -88,7 +88,7 @@ impl<'tcx> LateLintPass<'tcx> for Author { return; } prelude(); - PrintVisitor::new("item").visit_trait_item(item); + PrintVisitor::new("item", cx).visit_trait_item(item); done(); } @@ -98,7 +98,7 @@ impl<'tcx> LateLintPass<'tcx> for Author { } prelude(); let parent_hir_id = cx.tcx.hir().get_parent_node(var.id); - PrintVisitor::new("var").visit_variant(var, &hir::Generics::empty(), parent_hir_id); + PrintVisitor::new("var", cx).visit_variant(var, &hir::Generics::empty(), parent_hir_id); done(); } @@ -107,7 +107,7 @@ impl<'tcx> LateLintPass<'tcx> for Author { return; } prelude(); - PrintVisitor::new("field").visit_field_def(field); + PrintVisitor::new("field", cx).visit_field_def(field); done(); } @@ -116,7 +116,7 @@ impl<'tcx> LateLintPass<'tcx> for Author { return; } prelude(); - PrintVisitor::new("expr").visit_expr(expr); + PrintVisitor::new("expr", cx).visit_expr(expr); done(); } @@ -125,7 +125,7 @@ impl<'tcx> LateLintPass<'tcx> for Author { return; } prelude(); - PrintVisitor::new("arm").visit_arm(arm); + PrintVisitor::new("arm", cx).visit_arm(arm); done(); } @@ -138,7 +138,7 @@ impl<'tcx> LateLintPass<'tcx> for Author { _ => {}, } prelude(); - PrintVisitor::new("stmt").visit_stmt(stmt); + PrintVisitor::new("stmt", cx).visit_stmt(stmt); done(); } @@ -147,17 +147,18 @@ impl<'tcx> LateLintPass<'tcx> for Author { return; } prelude(); - PrintVisitor::new("item").visit_foreign_item(item); + PrintVisitor::new("item", cx).visit_foreign_item(item); done(); } } -impl PrintVisitor { +impl<'a, 'tcx> PrintVisitor<'a, 'tcx> { #[must_use] - fn new(s: &'static str) -> Self { + fn new(s: &'static str, cx: &'a LateContext<'tcx>) -> Self { Self { ids: FxHashMap::default(), current: s.to_owned(), + cx, } } @@ -190,43 +191,299 @@ impl PrintVisitor { println!("]);"); } } + + fn print_label(&mut self, label: Option