Skip to content
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

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

Closed
meithecatte opened this issue Feb 9, 2021 · 4 comments
Closed

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

meithecatte opened this issue Feb 9, 2021 · 4 comments
Assignees
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy

Comments

@meithecatte
Copy link
Contributor

meithecatte commented Feb 9, 2021

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!")
    }
}
@xFrednet
Copy link
Member

xFrednet commented Feb 9, 2021

@rustbot label +A-lint

@rustbot rustbot added the A-lint Area: New lints label Feb 9, 2021
@giraffate giraffate added the good-first-issue These issues are a good way to get started with Clippy label Feb 9, 2021
@unvalley
Copy link
Contributor

@rustbot claim

@unvalley
Copy link
Contributor

unvalley commented Jul 1, 2022

@NieDzejkob (cc: @xFrednet )
This may already be implemented.

ref:

Please tell me if I'm incorrect.

@xFrednet
Copy link
Member

xFrednet commented Jul 1, 2022

The lint looked familiar. Guess this was a duplicate issue. Thank you for pointing that out @unvalley! 🙃

@xFrednet xFrednet closed this as completed Jul 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

No branches or pull requests

5 participants