Skip to content

Warn on stdout output in a Debug or Display implementation #6705

@meithecatte

Description

@meithecatte

What it does

Detect uses of print (instead of write) and similar inside of implementations of formatting traits (Debug, Display, LowerHex, etc.)

Categories (optional)

  • Kind: correctness

What is the advantage of the recommended code over the original code

Using print will appear to work correctly until one tries to print somewhere other than stdout (a file, string buffer, ...)

Drawbacks

None.

Example

impl fmt::Display for Test {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        println!("Hello");
        writeln!(f, "there!")
    }
}

Should be written as:

impl fmt::Display for Test {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "Hello")?;
        writeln!(f, "there!")
    }
}

Metadata

Metadata

Assignees

Labels

A-lintArea: New lintsgood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions