From 2b93d2cca61ede7af6201dc7a2be7c6957ebfb77 Mon Sep 17 00:00:00 2001 From: Jared Davis Date: Mon, 7 Jul 2025 10:38:29 -0400 Subject: [PATCH] skip exit late lint pass on tests When using the `--test` or `--all-targets` flag, the exit lint should not fail on the main function. --- clippy_lints/src/exit.rs | 4 +++- tests/ui/exit4.rs | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 tests/ui/exit4.rs diff --git a/clippy_lints/src/exit.rs b/clippy_lints/src/exit.rs index cc8e4d7d9e28..862dd6d64762 100644 --- a/clippy_lints/src/exit.rs +++ b/clippy_lints/src/exit.rs @@ -1,7 +1,7 @@ use clippy_utils::diagnostics::span_lint; use clippy_utils::is_entrypoint_fn; use rustc_hir::{Expr, ExprKind, Item, ItemKind, OwnerNode}; -use rustc_lint::{LateContext, LateLintPass}; +use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_session::declare_lint_pass; use rustc_span::sym; @@ -43,6 +43,8 @@ declare_lint_pass!(Exit => [EXIT]); impl<'tcx> LateLintPass<'tcx> for Exit { fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) { + if cx.sess().is_test_crate() { return; } + if let ExprKind::Call(path_expr, [_]) = e.kind && let ExprKind::Path(ref path) = path_expr.kind && let Some(def_id) = cx.qpath_res(path, path_expr.hir_id).opt_def_id() diff --git a/tests/ui/exit4.rs b/tests/ui/exit4.rs new file mode 100644 index 000000000000..d52cdbce0eac --- /dev/null +++ b/tests/ui/exit4.rs @@ -0,0 +1,8 @@ +//@ check-pass +//@compile-flags: --test + +#![warn(clippy::exit)] + +fn main() { + std::process::exit(0) +} \ No newline at end of file