diff options
| author | dswij <dharmasw@outlook.com> | 2025-06-04 15:54:56 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-04 15:54:56 +0000 |
| commit | a76c2b34e718b9a04adf47fb1a4110e84c1557d2 (patch) | |
| tree | 8a0176aa03183ac2aee7e7bc2ce4cccd95384ff2 | |
| parent | ed143afc7f5ee4b3ce0c692b0528bdc51d0733ab (diff) | |
| parent | a4360d6fc3bf116379feebf6669ae8db5859847a (diff) | |
| download | rust-a76c2b34e718b9a04adf47fb1a4110e84c1557d2.tar.gz rust-a76c2b34e718b9a04adf47fb1a4110e84c1557d2.zip | |
Fix `unnecessary_debug_formatting` FP inside `Debug` impl (#14955)
Closes rust-lang/rust-clippy#14952 The lint suggests using `.display()` on `Path`, which is actually correct. changelog: [`unnecessary_debug_formatting`] fix FP inside `Debug` impl
| -rw-r--r-- | clippy_lints/src/format_args.rs | 12 | ||||
| -rw-r--r-- | tests/ui/format_args.fixed | 10 | ||||
| -rw-r--r-- | tests/ui/format_args.rs | 10 |
3 files changed, 31 insertions, 1 deletions
diff --git a/clippy_lints/src/format_args.rs b/clippy_lints/src/format_args.rs index ab6ae91a00f..0c39aae9ca9 100644 --- a/clippy_lints/src/format_args.rs +++ b/clippy_lints/src/format_args.rs @@ -11,7 +11,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, is_in_test}; +use clippy_utils::{is_diag_trait_item, is_from_proc_macro, is_in_test, trait_ref_of_method}; use itertools::Itertools; use rustc_ast::{ FormatArgPosition, FormatArgPositionKind, FormatArgsPiece, FormatArgumentKind, FormatCount, FormatOptions, @@ -542,6 +542,16 @@ impl<'tcx> FormatArgsExpr<'_, 'tcx> { && let ty = cx.typeck_results().expr_ty(value) && self.can_display_format(ty) { + // If the parent function is a method of `Debug`, we don't want to lint + // because it is likely that the user wants to use `Debug` formatting. + let parent_fn = cx.tcx.hir_get_parent_item(value.hir_id); + if let Some(trait_ref) = trait_ref_of_method(cx, parent_fn) + && let Some(trait_def_id) = trait_ref.trait_def_id() + && cx.tcx.is_diagnostic_item(sym::Debug, trait_def_id) + { + return; + } + let snippet = snippet(cx.sess(), value.span, ".."); span_lint_and_then( cx, diff --git a/tests/ui/format_args.fixed b/tests/ui/format_args.fixed index edfdaa23ca1..3cb6c0b4d97 100644 --- a/tests/ui/format_args.fixed +++ b/tests/ui/format_args.fixed @@ -192,3 +192,13 @@ mod issue_9256 { print_substring("Hello, world!"); } } + +mod issue14952 { + use std::path::Path; + struct Foo(Path); + impl std::fmt::Debug for Foo { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{:?}", &self.0) + } + } +} diff --git a/tests/ui/format_args.rs b/tests/ui/format_args.rs index 367560d577d..8a9c369fff3 100644 --- a/tests/ui/format_args.rs +++ b/tests/ui/format_args.rs @@ -192,3 +192,13 @@ mod issue_9256 { print_substring("Hello, world!"); } } + +mod issue14952 { + use std::path::Path; + struct Foo(Path); + impl std::fmt::Debug for Foo { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{:?}", &self.0) + } + } +} |
