From 2cd4a9182a4b4a1030594866414a3100b594343e Mon Sep 17 00:00:00 2001 From: Centri3 <114838443+Centri3@users.noreply.github.com> Date: Sat, 17 Jun 2023 20:00:36 -0500 Subject: [PATCH] Ignore functions annotated with `#[test]` --- book/src/lint_configuration.md | 1 + clippy_lints/src/single_call_fn.rs | 5 +++-- tests/ui/single_call_fn.rs | 9 +++++++++ tests/ui/single_call_fn.stderr | 16 ++++++++-------- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/book/src/lint_configuration.md b/book/src/lint_configuration.md index 78a5f9da4..cc4caae56 100644 --- a/book/src/lint_configuration.md +++ b/book/src/lint_configuration.md @@ -94,6 +94,7 @@ Suppress lints whenever the suggested change would cause breakage for other crat * [`linkedlist`](https://rust-lang.github.io/rust-clippy/master/index.html#linkedlist) * [`rc_mutex`](https://rust-lang.github.io/rust-clippy/master/index.html#rc_mutex) * [`unnecessary_box_returns`](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_box_returns) +* [`single_call_fn`](https://rust-lang.github.io/rust-clippy/master/index.html#single_call_fn) ## `msrv` diff --git a/clippy_lints/src/single_call_fn.rs b/clippy_lints/src/single_call_fn.rs index 55a19b813..42753d2e9 100644 --- a/clippy_lints/src/single_call_fn.rs +++ b/clippy_lints/src/single_call_fn.rs @@ -1,5 +1,5 @@ use clippy_utils::diagnostics::span_lint_and_help; -use clippy_utils::is_from_proc_macro; +use clippy_utils::{is_from_proc_macro, is_in_test_function}; use rustc_data_structures::fx::FxHashMap; use rustc_hir::def_id::LocalDefId; use rustc_hir::intravisit::{walk_expr, Visitor}; @@ -12,7 +12,7 @@ use rustc_span::Span; declare_clippy_lint! { /// ### What it does - /// Checks for functions that are only used once. + /// Checks for functions that are only used once. Does not lint tests. /// /// ### Why is this bad? /// It's usually not, splitting a function into multiple parts often improves readability and in @@ -73,6 +73,7 @@ impl<'tcx> LateLintPass<'tcx> for SingleCallFn { if self.avoid_breaking_exported_api && cx.effective_visibilities.is_exported(def_id) || in_external_macro(cx.sess(), span) || is_from_proc_macro(cx, &(&kind, body, cx.tcx.local_def_id_to_hir_id(def_id), span)) + || is_in_test_function(cx.tcx, body.value.hir_id) { return; } diff --git a/tests/ui/single_call_fn.rs b/tests/ui/single_call_fn.rs index e7f819cc8..bfb773187 100644 --- a/tests/ui/single_call_fn.rs +++ b/tests/ui/single_call_fn.rs @@ -1,4 +1,5 @@ //@aux-build:proc_macros.rs +//@compile-flags: --test #![allow(clippy::redundant_closure_call, unused)] #![warn(clippy::single_call_fn)] #![no_main] @@ -64,3 +65,11 @@ fn e() { b(); b(); } + +#[test] +fn k() {} + +#[test] +fn l() { + k(); +} diff --git a/tests/ui/single_call_fn.stderr b/tests/ui/single_call_fn.stderr index 9ef8c4878..bb92e3cf7 100644 --- a/tests/ui/single_call_fn.stderr +++ b/tests/ui/single_call_fn.stderr @@ -1,5 +1,5 @@ error: this function is only used once - --> $DIR/single_call_fn.rs:33:1 + --> $DIR/single_call_fn.rs:34:1 | LL | / fn c() { LL | | println!("really"); @@ -9,44 +9,44 @@ LL | | } | |_^ | help: used here - --> $DIR/single_call_fn.rs:40:5 + --> $DIR/single_call_fn.rs:41:5 | LL | c(); | ^ = note: `-D clippy::single-call-fn` implied by `-D warnings` error: this function is only used once - --> $DIR/single_call_fn.rs:12:1 + --> $DIR/single_call_fn.rs:13:1 | LL | fn i() {} | ^^^^^^^^^ | help: used here - --> $DIR/single_call_fn.rs:17:13 + --> $DIR/single_call_fn.rs:18:13 | LL | let a = i; | ^ error: this function is only used once - --> $DIR/single_call_fn.rs:43:1 + --> $DIR/single_call_fn.rs:44:1 | LL | fn a() {} | ^^^^^^^^^ | help: used here - --> $DIR/single_call_fn.rs:46:5 + --> $DIR/single_call_fn.rs:47:5 | LL | a(); | ^ error: this function is only used once - --> $DIR/single_call_fn.rs:13:1 + --> $DIR/single_call_fn.rs:14:1 | LL | fn j() {} | ^^^^^^^^^ | help: used here - --> $DIR/single_call_fn.rs:24:9 + --> $DIR/single_call_fn.rs:25:9 | LL | j(); | ^