about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCatherine Flores <catherine.3.flores@gmail.com>2025-03-04 16:35:09 +0000
committerGitHub <noreply@github.com>2025-03-04 16:35:09 +0000
commit2440f516965ea948e9d04e56c63beee36deb8848 (patch)
tree09b629b9b6b5dad94a57fad4267dd7d1b4ecde56
parentdd8cf052c247c02cdd8add6579df86342cf580b7 (diff)
parente6a4cf63adfaed64b74ed04441e902ce64a47116 (diff)
don't trigger `unnecessary_debug_formatting` in tests (#14347)
close #14345

changelog: [`unnecessary_debug_formatting`]: don't lint in tests
-rw-r--r--clippy_lints/src/format_args.rs5
-rw-r--r--tests/ui/unnecessary_path_debug_formatting.rs6
2 files changed, 9 insertions, 2 deletions
diff --git a/clippy_lints/src/format_args.rs b/clippy_lints/src/format_args.rs
index fc5f76179f9..3862ff7921d 100644
--- a/clippy_lints/src/format_args.rs
+++ b/clippy_lints/src/format_args.rs
@@ -9,7 +9,7 @@ use clippy_utils::macros::{
 use clippy_utils::msrvs::{self, Msrv};
 use clippy_utils::source::{SpanRangeExt, snippet};
 use clippy_utils::ty::{implements_trait, is_type_lang_item};
-use clippy_utils::{is_diag_trait_item, is_from_proc_macro};
+use clippy_utils::{is_diag_trait_item, is_from_proc_macro, is_in_test};
 use itertools::Itertools;
 use rustc_ast::{
     FormatArgPosition, FormatArgPositionKind, FormatArgsPiece, FormatArgumentKind, FormatCount, FormatOptions,
@@ -484,7 +484,8 @@ impl<'tcx> FormatArgsExpr<'_, 'tcx> {
 
     fn check_unnecessary_debug_formatting(&self, name: Symbol, value: &Expr<'tcx>) {
         let cx = self.cx;
-        if !value.span.from_expansion()
+        if !is_in_test(cx.tcx, value.hir_id)
+            && !value.span.from_expansion()
             && !is_from_proc_macro(cx, value)
             && let ty = cx.typeck_results().expr_ty(value)
             && self.can_display_format(ty)
diff --git a/tests/ui/unnecessary_path_debug_formatting.rs b/tests/ui/unnecessary_path_debug_formatting.rs
index 02adeece280..f14f6085c9a 100644
--- a/tests/ui/unnecessary_path_debug_formatting.rs
+++ b/tests/ui/unnecessary_path_debug_formatting.rs
@@ -41,3 +41,9 @@ fn main() {
     let deref_path = DerefPath { path };
     println!("{:?}", &*deref_path); //~ unnecessary_debug_formatting
 }
+
+#[test]
+fn issue_14345() {
+    let input = std::path::Path::new("/foo/bar");
+    assert!(input.ends_with("baz"), "{input:?}");
+}