Skip to content

Don't lint unnecessary_debug_formatting in tests #14345

Closed
@kpreid

Description

@kpreid

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions