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

false negative write_with_newline: format string that starts with \n #8741

Closed
matthiaskrgr opened this issue Apr 24, 2022 · 9 comments
Closed
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Apr 24, 2022

Summary

If a format string starts with a \n, the lint does not seem to fire.
I had a quick glance at the lint code but I didn't see anything that would indicate that this is intentional.

Lint Name

clippy::write_with_newline

Reproducer

I tried this code:

use std::fmt::Write as _;

pub fn main() {
    let mut s = String::new();
    let x = "a";
    write!(s, "\n{}\n", x).unwrap(); // should warn
    dbg!(s);
}

would expect a warning here since we can write writeln!(s, "\n{}")

Version

clippy 0.1.62 (7b8a08cf1 2022-04-21)
@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Apr 24, 2022
@Serial-ATA
Copy link
Contributor

@rustbot label +good-first-issue

@rustbot rustbot added the good-first-issue These issues are a good way to get started with Clippy label Apr 24, 2022
@TennyZhuang
Copy link
Contributor

@rustbot claim

@TennyZhuang
Copy link
Contributor

Here are some positive test cases:

// These should be fine
    write!(v, "");
    write!(v, "Hello");
    writeln!(v, "Hello");
    writeln!(v, "Hello\n");
    writeln!(v, "Hello {}\n", "world");
    write!(v, "Issue\n{}", 1265);
    write!(v, "{}", 1265);
    write!(v, "\n{}", 1275);
    write!(v, "\n\n");
    write!(v, "like eof\n\n");
    write!(v, "Hello {} {}\n\n", "world", "#2");
    writeln!(v, "\ndon't\nwarn\nfor\nmultiple\nnewlines\n"); // #3126
    writeln!(v, "\nbla\n\n"); // #3126

It seems that write! with multiple \n is allowed by the rule, and it seems like a feature not a bug? I'm not sure whether to fix it.

The variable has_internal_newline produces the result.

@matthiaskrgr
Copy link
Member Author

Hmm, so it looks like we bail out as soon as there's more than one \n?
I agree that it doesn't bring much value to change write!(.."\n\n") to writeln!(.."\n") but it also doesn't look like we have an explicit \n{]\n test :)
In my case the example was a bit longer originally like

 write!(
        output,
        "\nSummary of: {} ({} total)\n",
        path.display(),
        bin_cache
            .total_size()
            .file_size(file_size_opts::DECIMAL)
            .unwrap()
    ));
    )
    .unwrap();

@TennyZhuang
Copy link
Contributor

Hmm, so it looks like we bail out as soon as there's more than one \n? I agree that it doesn't bring much value to change write!(.."\n\n") to writeln!(.."\n") but it also doesn't look like we have an explicit \n{]\n test :) In my case the example was a bit longer originally like

 write!(
        output,
        "\nSummary of: {} ({} total)\n",
        path.display(),
        bin_cache
            .total_size()
            .file_size(file_size_opts::DECIMAL)
            .unwrap()
    ));
    )
    .unwrap();

I’m not sure whether it’s better that only allow multiple line-breaks at the end, instead of allow an internal/begin line-break and a line-break at the end.

@TennyZhuang
Copy link
Contributor

If the answer is yes, I’d like to implement it :(

@Alexendoo
Copy link
Member

FWIW there's a pending rewrite of write.rs in #8518, I kept the behaviour of ignoring strings that contain multiple newlines, personally I think it's clearer that way

We could also ask the author of #3138 😉

@matthiaskrgr
Copy link
Member Author

🤣 I don't even remember writing that... it has been some time

@km274
Copy link

km274 commented Jan 30, 2024

Hey everyone 👋🏻, I ended up here because I was looking for an issue to work on, but it looks like #8518 addressed this. Ignoring strings with multiple newlines makes sense to me, FWIW.

I guess this issue should be closed now (?).

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 good-first-issue These issues are a good way to get started with Clippy I-false-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

No branches or pull requests

6 participants