Skip to content

Commit 9c12717

Browse files
committed
Address additional review comments
1 parent 07a0c73 commit 9c12717

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

clippy_lints/src/format_args.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use arrayvec::ArrayVec;
22
use clippy_config::Conf;
3-
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg, span_lint_and_then};
3+
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
44
use clippy_utils::macros::{
55
FormatArgsStorage, FormatParamUsage, MacroCall, find_format_arg_expr, format_arg_removal_span,
66
format_placeholder_format_span, is_assert_macro, is_format_macro, is_panic, matching_root_macro_call,
@@ -60,9 +60,8 @@ declare_clippy_lint! {
6060
/// change in the future. `OsStr`s and `Path`s can be `Display` formatted
6161
/// using their `display` methods.
6262
///
63-
/// Note that switching from `Debug` formatting to `Display` formatting
64-
/// will change how the `OsStr` or `Path` is shown. Escaped characters will
65-
/// no longer be escaped, and enclosing quotes (`"`...`"`) will be removed.
63+
/// Furthermore, with `Debug` formatting, certain characters are escaped.
64+
/// Thus, a `Debug` formatted `Path` is less likely to be clickable.
6665
///
6766
/// ### Example
6867
/// ```no_run
@@ -490,13 +489,21 @@ impl<'tcx> FormatArgsExpr<'_, 'tcx> {
490489
&& self.can_display_format(ty)
491490
{
492491
let snippet = snippet(cx.sess(), value.span, "..");
493-
span_lint_and_help(
492+
span_lint_and_then(
494493
cx,
495494
UNNECESSARY_DEBUG_FORMATTING,
496495
value.span,
497496
format!("unnecessary `Debug` formatting in `{name}!` args"),
498-
None,
499-
format!("use `Display` formatting and change this to `{snippet}.display()`"),
497+
|diag| {
498+
diag.help(format!(
499+
"use `Display` formatting and change this to `{snippet}.display()`"
500+
));
501+
diag.note(
502+
"switching to `Display` formatting will change how the value is shown; \
503+
escaped characters will no longer be escaped and surrounding quotes will \
504+
be removed",
505+
);
506+
},
500507
);
501508
}
502509
}

tests/ui/unnecessary_os_str_debug_formatting.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | println!("{:?}", os_str);
55
| ^^^^^^
66
|
77
= help: use `Display` formatting and change this to `os_str.display()`
8+
= 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
89
= note: `-D clippy::unnecessary-debug-formatting` implied by `-D warnings`
910
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_debug_formatting)]`
1011

@@ -15,6 +16,7 @@ LL | println!("{:?}", os_string);
1516
| ^^^^^^^^^
1617
|
1718
= help: use `Display` formatting and change this to `os_string.display()`
19+
= 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
1820

1921
error: unnecessary `Debug` formatting in `println!` args
2022
--> tests/ui/unnecessary_os_str_debug_formatting.rs:18:16
@@ -23,6 +25,7 @@ LL | println!("{os_str:?}");
2325
| ^^^^^^
2426
|
2527
= help: use `Display` formatting and change this to `os_str.display()`
28+
= 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
2629

2730
error: unnecessary `Debug` formatting in `println!` args
2831
--> tests/ui/unnecessary_os_str_debug_formatting.rs:19:16
@@ -31,6 +34,7 @@ LL | println!("{os_string:?}");
3134
| ^^^^^^^^^
3235
|
3336
= help: use `Display` formatting and change this to `os_string.display()`
37+
= 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
3438

3539
error: unnecessary `Debug` formatting in `format!` args
3640
--> tests/ui/unnecessary_os_str_debug_formatting.rs:21:37
@@ -39,6 +43,7 @@ LL | let _: String = format!("{:?}", os_str);
3943
| ^^^^^^
4044
|
4145
= help: use `Display` formatting and change this to `os_str.display()`
46+
= 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
4247

4348
error: unnecessary `Debug` formatting in `format!` args
4449
--> tests/ui/unnecessary_os_str_debug_formatting.rs:22:37
@@ -47,6 +52,7 @@ LL | let _: String = format!("{:?}", os_string);
4752
| ^^^^^^^^^
4853
|
4954
= help: use `Display` formatting and change this to `os_string.display()`
55+
= 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
5056

5157
error: aborting due to 6 previous errors
5258

tests/ui/unnecessary_path_debug_formatting.stderr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | println!("{:?}", path);
55
| ^^^^
66
|
77
= help: use `Display` formatting and change this to `path.display()`
8+
= 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
89
= note: `-D clippy::unnecessary-debug-formatting` implied by `-D warnings`
910
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_debug_formatting)]`
1011

@@ -15,6 +16,7 @@ LL | println!("{:?}", path_buf);
1516
| ^^^^^^^^
1617
|
1718
= help: use `Display` formatting and change this to `path_buf.display()`
19+
= 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
1820

1921
error: unnecessary `Debug` formatting in `println!` args
2022
--> tests/ui/unnecessary_path_debug_formatting.rs:36:16
@@ -23,6 +25,7 @@ LL | println!("{path:?}");
2325
| ^^^^
2426
|
2527
= help: use `Display` formatting and change this to `path.display()`
28+
= 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
2629

2730
error: unnecessary `Debug` formatting in `println!` args
2831
--> tests/ui/unnecessary_path_debug_formatting.rs:37:16
@@ -31,6 +34,7 @@ LL | println!("{path_buf:?}");
3134
| ^^^^^^^^
3235
|
3336
= help: use `Display` formatting and change this to `path_buf.display()`
37+
= 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
3438

3539
error: unnecessary `Debug` formatting in `format!` args
3640
--> tests/ui/unnecessary_path_debug_formatting.rs:39:37
@@ -39,6 +43,7 @@ LL | let _: String = format!("{:?}", path);
3943
| ^^^^
4044
|
4145
= help: use `Display` formatting and change this to `path.display()`
46+
= 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
4247

4348
error: unnecessary `Debug` formatting in `format!` args
4449
--> tests/ui/unnecessary_path_debug_formatting.rs:40:37
@@ -47,6 +52,7 @@ LL | let _: String = format!("{:?}", path_buf);
4752
| ^^^^^^^^
4853
|
4954
= help: use `Display` formatting and change this to `path_buf.display()`
55+
= 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
5056

5157
error: unnecessary `Debug` formatting in `println!` args
5258
--> tests/ui/unnecessary_path_debug_formatting.rs:43:22
@@ -55,6 +61,7 @@ LL | println!("{:?}", &*deref_path);
5561
| ^^^^^^^^^^^^
5662
|
5763
= help: use `Display` formatting and change this to `&*deref_path.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
5865

5966
error: aborting due to 7 previous errors
6067

0 commit comments

Comments
 (0)