Skip to content

Commit b2d176f

Browse files
committed
unnecessary_debug_formatting respect MSRV
`unnecessary_debug_formatting` suggested display() regardless of MSRV. This adds MSRV check for OsStr and Path. changelog: [`unnecessary_debug_formatting`]: respects MSRV
1 parent c90c7c4 commit b2d176f

File tree

8 files changed

+72
-3
lines changed

8 files changed

+72
-3
lines changed

book/src/lint_configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,7 @@ The minimum rust version that the project supports. Defaults to the `rust-versio
866866
* [`type_repetition_in_bounds`](https://rust-lang.github.io/rust-clippy/master/index.html#type_repetition_in_bounds)
867867
* [`unchecked_duration_subtraction`](https://rust-lang.github.io/rust-clippy/master/index.html#unchecked_duration_subtraction)
868868
* [`uninlined_format_args`](https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args)
869+
* [`unnecessary_debug_formatting`](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_debug_formatting)
869870
* [`unnecessary_lazy_evaluations`](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_lazy_evaluations)
870871
* [`unnested_or_patterns`](https://rust-lang.github.io/rust-clippy/master/index.html#unnested_or_patterns)
871872
* [`unused_trait_names`](https://rust-lang.github.io/rust-clippy/master/index.html#unused_trait_names)

clippy_config/src/conf.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,7 @@ define_Conf! {
769769
type_repetition_in_bounds,
770770
unchecked_duration_subtraction,
771771
uninlined_format_args,
772+
unnecessary_debug_formatting,
772773
unnecessary_lazy_evaluations,
773774
unnested_or_patterns,
774775
unused_trait_names,

clippy_lints/src/format_args.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@ impl<'tcx> FormatArgsExpr<'_, 'tcx> {
541541
&& !is_from_proc_macro(cx, value)
542542
&& let ty = cx.typeck_results().expr_ty(value)
543543
&& self.can_display_format(ty)
544+
&& self.msrv.meets(cx, msrvs::UNNECESSARY_DEBUG_FORMATTING)
544545
{
545546
// If the parent function is a method of `Debug`, we don't want to lint
546547
// because it is likely that the user wants to use `Debug` formatting.

clippy_utils/src/msrvs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ macro_rules! msrv_aliases {
2424
// names may refer to stabilized feature flags or library items
2525
msrv_aliases! {
2626
1,88,0 { LET_CHAINS }
27-
1,87,0 { OS_STR_DISPLAY, INT_MIDPOINT, CONST_CHAR_IS_DIGIT }
27+
1,87,0 { OS_STR_DISPLAY, INT_MIDPOINT, CONST_CHAR_IS_DIGIT, UNNECESSARY_DEBUG_FORMATTING }
2828
1,85,0 { UINT_FLOAT_MIDPOINT, CONST_SIZE_OF_VAL }
2929
1,84,0 { CONST_OPTION_AS_SLICE, MANUAL_DANGLING_PTR }
3030
1,83,0 { CONST_EXTERN_FN, CONST_FLOAT_BITS_CONV, CONST_FLOAT_CLASSIFY, CONST_MUT_REFS, CONST_UNWRAP }

tests/ui/unnecessary_os_str_debug_formatting.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,20 @@ fn main() {
2121
let _: String = format!("{:?}", os_str); //~ unnecessary_debug_formatting
2222
let _: String = format!("{:?}", os_string); //~ unnecessary_debug_formatting
2323
}
24+
25+
#[clippy::msrv = "1.86"]
26+
fn msrv_1_86() {
27+
let os_str = OsStr::new("test");
28+
29+
// Should not lint because MSRV is 1.86, but display() was stabilized in 1.87
30+
println!("{:?}", os_str);
31+
}
32+
33+
#[clippy::msrv = "1.87"]
34+
fn msrv_1_87() {
35+
let os_str = OsStr::new("test");
36+
37+
// Should lint because MSRV is 1.87 and display() is available
38+
println!("{:?}", os_str);
39+
//~^ unnecessary_debug_formatting
40+
}

tests/ui/unnecessary_os_str_debug_formatting.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,14 @@ LL | let _: String = format!("{:?}", os_string);
5454
= help: use `Display` formatting and change this to `os_string.display()`
5555
= 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
5656

57-
error: aborting due to 6 previous errors
57+
error: unnecessary `Debug` formatting in `println!` args
58+
--> tests/ui/unnecessary_os_str_debug_formatting.rs:38:22
59+
|
60+
LL | println!("{:?}", os_str);
61+
| ^^^^^^
62+
|
63+
= help: use `Display` formatting and change this to `os_str.display()`
64+
= 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
65+
66+
error: aborting due to 7 previous errors
5867

tests/ui/unnecessary_path_debug_formatting.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,25 @@ fn issue_14345() {
4848
let input = std::path::Path::new("/foo/bar");
4949
assert!(input.ends_with("baz"), "{input:?}");
5050
}
51+
52+
#[clippy::msrv = "1.86"]
53+
fn msrv_1_86() {
54+
let path = Path::new("/test");
55+
let path_buf = PathBuf::from("/test");
56+
57+
// Should not lint because MSRV is 1.86, but display() was stabilized in 1.87
58+
println!("{:?}", path);
59+
println!("{:?}", path_buf);
60+
}
61+
62+
#[clippy::msrv = "1.87"]
63+
fn msrv_1_87() {
64+
let path = Path::new("/test");
65+
let path_buf = PathBuf::from("/test");
66+
67+
// Should lint because MSRV is 1.87 and display() is available
68+
println!("{:?}", path);
69+
//~^ unnecessary_debug_formatting
70+
println!("{:?}", path_buf);
71+
//~^ unnecessary_debug_formatting
72+
}

tests/ui/unnecessary_path_debug_formatting.stderr

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,23 @@ LL | println!("{:?}", &*deref_path);
8181
= help: use `Display` formatting and change this to `&*deref_path.display()`
8282
= 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
8383

84-
error: aborting due to 9 previous errors
84+
error: unnecessary `Debug` formatting in `println!` args
85+
--> tests/ui/unnecessary_path_debug_formatting.rs:68:22
86+
|
87+
LL | println!("{:?}", path);
88+
| ^^^^
89+
|
90+
= help: use `Display` formatting and change this to `path.display()`
91+
= 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
92+
93+
error: unnecessary `Debug` formatting in `println!` args
94+
--> tests/ui/unnecessary_path_debug_formatting.rs:70:22
95+
|
96+
LL | println!("{:?}", path_buf);
97+
| ^^^^^^^^
98+
|
99+
= help: use `Display` formatting and change this to `path_buf.display()`
100+
= 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
101+
102+
error: aborting due to 11 previous errors
85103

0 commit comments

Comments
 (0)