Skip to content

Don't lint unnecessary_debug_formatting in tests #14345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kpreid opened this issue Mar 4, 2025 · 0 comments · Fixed by #14347
Closed

Don't lint unnecessary_debug_formatting in tests #14345

kpreid opened this issue Mar 4, 2025 · 0 comments · Fixed by #14347
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@kpreid
Copy link
Contributor

kpreid commented Mar 4, 2025

Summary

The issues checked for by unnecessary_debug_formatting (see #12674) don't apply to tests — tests are, in general, often going to print Debug format when they fail, whether explicitly or via .unwrap() or an internal assertion of the code under test, and their output is not user-facing output. So, satisfying the unnecessary_debug_formatting lint is usually wasted effort in tests; the lint should automatically skip tests.

Or, instead, it could be disabled in the format strings of the assert!() family of macros. I think this is a less good idea, because an assertion might fire while a program is in use, in which case the amount and format of information in it is relevant, but one could argue that in context of debug-fmt-detail, most asserts should be expected to be badly formatted anyway.

Lint Name

unnecessary_debug_formatting

Reproducer

I tried this code:

#![warn(clippy::unnecessary_debug_formatting)]

#[test]
fn my_test() {
    let input = std::path::Path::new("/foo/bar");
    assert!(input.ends_with("baz"), "{input:?}");
}

I saw this happen:

warning: unnecessary `Debug` formatting in `assert!` args
 --> src/lib.rs:6:39
  |
6 |     assert!(input.ends_with("baz"), "{input:?}");
  |                                       ^^^^^
  |
  = help: use `Display` formatting and change this to `input.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
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_debug_formatting
note: the lint level is defined here
 --> src/lib.rs:1:9
  |
1 | #![warn(clippy::unnecessary_debug_formatting)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I expected to see this happen: No lint.

Version

rustc 1.87.0-nightly (f4a216d28 2025-03-02)
binary: rustc
commit-hash: f4a216d28ee635afce685b4206e713579f66e130
commit-date: 2025-03-02
host: aarch64-apple-darwin
release: 1.87.0-nightly
LLVM version: 20.1.0

Additional Labels

No response

@kpreid kpreid added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Mar 4, 2025
github-merge-queue bot pushed a commit that referenced this issue Mar 4, 2025
close #14345

changelog: [`unnecessary_debug_formatting`]: don't lint in tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant