diff --git a/book/src/lint_configuration.md b/book/src/lint_configuration.md index 7c850b4b023a..9c56de106891 100644 --- a/book/src/lint_configuration.md +++ b/book/src/lint_configuration.md @@ -866,6 +866,7 @@ The minimum rust version that the project supports. Defaults to the `rust-versio * [`type_repetition_in_bounds`](https://rust-lang.github.io/rust-clippy/master/index.html#type_repetition_in_bounds) * [`unchecked_duration_subtraction`](https://rust-lang.github.io/rust-clippy/master/index.html#unchecked_duration_subtraction) * [`uninlined_format_args`](https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args) +* [`unnecessary_debug_formatting`](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_debug_formatting) * [`unnecessary_lazy_evaluations`](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_lazy_evaluations) * [`unnested_or_patterns`](https://rust-lang.github.io/rust-clippy/master/index.html#unnested_or_patterns) * [`unused_trait_names`](https://rust-lang.github.io/rust-clippy/master/index.html#unused_trait_names) diff --git a/clippy_config/src/conf.rs b/clippy_config/src/conf.rs index 87158cec42b2..c70eb253760c 100644 --- a/clippy_config/src/conf.rs +++ b/clippy_config/src/conf.rs @@ -769,6 +769,7 @@ define_Conf! { type_repetition_in_bounds, unchecked_duration_subtraction, uninlined_format_args, + unnecessary_debug_formatting, unnecessary_lazy_evaluations, unnested_or_patterns, unused_trait_names, diff --git a/clippy_lints/src/format_args.rs b/clippy_lints/src/format_args.rs index 0c39aae9ca91..3ad1dc52277d 100644 --- a/clippy_lints/src/format_args.rs +++ b/clippy_lints/src/format_args.rs @@ -541,6 +541,7 @@ impl<'tcx> FormatArgsExpr<'_, 'tcx> { && !is_from_proc_macro(cx, value) && let ty = cx.typeck_results().expr_ty(value) && self.can_display_format(ty) + && self.msrv.meets(cx, msrvs::UNNECESSARY_DEBUG_FORMATTING) { // 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. diff --git a/clippy_utils/src/msrvs.rs b/clippy_utils/src/msrvs.rs index a5e66ad463bb..75e6d3931d23 100644 --- a/clippy_utils/src/msrvs.rs +++ b/clippy_utils/src/msrvs.rs @@ -24,7 +24,7 @@ macro_rules! msrv_aliases { // names may refer to stabilized feature flags or library items msrv_aliases! { 1,88,0 { LET_CHAINS } - 1,87,0 { OS_STR_DISPLAY, INT_MIDPOINT, CONST_CHAR_IS_DIGIT } + 1,87,0 { OS_STR_DISPLAY, INT_MIDPOINT, CONST_CHAR_IS_DIGIT, UNNECESSARY_DEBUG_FORMATTING } 1,85,0 { UINT_FLOAT_MIDPOINT, CONST_SIZE_OF_VAL } 1,84,0 { CONST_OPTION_AS_SLICE, MANUAL_DANGLING_PTR } 1,83,0 { CONST_EXTERN_FN, CONST_FLOAT_BITS_CONV, CONST_FLOAT_CLASSIFY, CONST_MUT_REFS, CONST_UNWRAP } diff --git a/tests/ui/unnecessary_os_str_debug_formatting.rs b/tests/ui/unnecessary_os_str_debug_formatting.rs index 6652efd9ae1d..5ccd18c701f8 100644 --- a/tests/ui/unnecessary_os_str_debug_formatting.rs +++ b/tests/ui/unnecessary_os_str_debug_formatting.rs @@ -21,3 +21,20 @@ fn main() { let _: String = format!("{:?}", os_str); //~ unnecessary_debug_formatting let _: String = format!("{:?}", os_string); //~ unnecessary_debug_formatting } + +#[clippy::msrv = "1.86"] +fn msrv_1_86() { + let os_str = OsStr::new("test"); + + // Should not lint because MSRV is 1.86, but display() was stabilized in 1.87 + println!("{:?}", os_str); +} + +#[clippy::msrv = "1.87"] +fn msrv_1_87() { + let os_str = OsStr::new("test"); + + // Should lint because MSRV is 1.87 and display() is available + println!("{:?}", os_str); + //~^ unnecessary_debug_formatting +} diff --git a/tests/ui/unnecessary_os_str_debug_formatting.stderr b/tests/ui/unnecessary_os_str_debug_formatting.stderr index 382e59b04619..d385cef37b84 100644 --- a/tests/ui/unnecessary_os_str_debug_formatting.stderr +++ b/tests/ui/unnecessary_os_str_debug_formatting.stderr @@ -54,5 +54,14 @@ LL | let _: String = format!("{:?}", os_string); = help: use `Display` formatting and change this to `os_string.display()` = note: switching to `Display` formatting will change how the value is shown; escaped characters will no longer be escaped and surrounding quotes will be removed -error: aborting due to 6 previous errors +error: unnecessary `Debug` formatting in `println!` args + --> tests/ui/unnecessary_os_str_debug_formatting.rs:38:22 + | +LL | println!("{:?}", os_str); + | ^^^^^^ + | + = help: use `Display` formatting and change this to `os_str.display()` + = note: switching to `Display` formatting will change how the value is shown; escaped characters will no longer be escaped and surrounding quotes will be removed + +error: aborting due to 7 previous errors diff --git a/tests/ui/unnecessary_path_debug_formatting.rs b/tests/ui/unnecessary_path_debug_formatting.rs index 215e0d5d7802..733da1b45afd 100644 --- a/tests/ui/unnecessary_path_debug_formatting.rs +++ b/tests/ui/unnecessary_path_debug_formatting.rs @@ -48,3 +48,25 @@ fn issue_14345() { let input = std::path::Path::new("/foo/bar"); assert!(input.ends_with("baz"), "{input:?}"); } + +#[clippy::msrv = "1.86"] +fn msrv_1_86() { + let path = Path::new("/test"); + let path_buf = PathBuf::from("/test"); + + // Should not lint because MSRV is 1.86, but display() was stabilized in 1.87 + println!("{:?}", path); + println!("{:?}", path_buf); +} + +#[clippy::msrv = "1.87"] +fn msrv_1_87() { + let path = Path::new("/test"); + let path_buf = PathBuf::from("/test"); + + // Should lint because MSRV is 1.87 and display() is available + println!("{:?}", path); + //~^ unnecessary_debug_formatting + println!("{:?}", path_buf); + //~^ unnecessary_debug_formatting +} diff --git a/tests/ui/unnecessary_path_debug_formatting.stderr b/tests/ui/unnecessary_path_debug_formatting.stderr index d244b9ad6716..bbda4509cd6b 100644 --- a/tests/ui/unnecessary_path_debug_formatting.stderr +++ b/tests/ui/unnecessary_path_debug_formatting.stderr @@ -81,5 +81,23 @@ LL | println!("{:?}", &*deref_path); = help: use `Display` formatting and change this to `&*deref_path.display()` = note: switching to `Display` formatting will change how the value is shown; escaped characters will no longer be escaped and surrounding quotes will be removed -error: aborting due to 9 previous errors +error: unnecessary `Debug` formatting in `println!` args + --> tests/ui/unnecessary_path_debug_formatting.rs:68:22 + | +LL | println!("{:?}", path); + | ^^^^ + | + = help: use `Display` formatting and change this to `path.display()` + = note: switching to `Display` formatting will change how the value is shown; escaped characters will no longer be escaped and surrounding quotes will be removed + +error: unnecessary `Debug` formatting in `println!` args + --> tests/ui/unnecessary_path_debug_formatting.rs:70:22 + | +LL | println!("{:?}", path_buf); + | ^^^^^^^^ + | + = help: use `Display` formatting and change this to `path_buf.display()` + = note: switching to `Display` formatting will change how the value is shown; escaped characters will no longer be escaped and surrounding quotes will be removed + +error: aborting due to 11 previous errors