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

FP: print_with_newline triggers when there is no println macro #7195

Closed
DjLogozzo opened this issue May 8, 2021 · 2 comments · Fixed by #8518
Closed

FP: print_with_newline triggers when there is no println macro #7195

DjLogozzo opened this issue May 8, 2021 · 2 comments · Fixed by #8518
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

@DjLogozzo
Copy link

The clippy lint print_with_newline triggers during scenarios without access to a println macro, such as no_std environments

Lint name:
print_with_newline

I tried this code:

#![no_std]

fn main() {
    print!("Hello World!\n");
    // Replacing print with println, as suggested by the lint, fails to build as there is no println macro
    //println!("Hello World!")
}

#[macro_export]
macro_rules! print {
    ($($arg:tt)*) => {
        unimplemented!()
    }
}

I expected to see this happen:
No lint activation

Instead, this happened:

warning: using `print!()` with a format string that ends in a single newline
 --> src/main.rs:4:5
  |
4 |     print!("Hello World!\n");
  |     ^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(clippy::print_with_newline)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#print_with_newline
help: use `println!` instead
  |
4 |     println!("Hello World!");
  |     ^^^^^^^              --

warning: 1 warning emitted

    Finished dev [unoptimized + debuginfo] target(s) in 0.00s

Meta

  • cargo clippy -V: clippy 0.1.52 (9b471a3 2021-02-19)
  • rustc -Vv:
    rustc 1.52.0-nightly (9b471a3f5 2021-02-19)
    binary: rustc
    commit-hash: 9b471a3f5fe57e5c6e08acf665f2094422415a3d
    commit-date: 2021-02-19
    host: x86_64-unknown-linux-gnu
    release: 1.52.0-nightly
    LLVM version: 11.0.1
    
@DjLogozzo DjLogozzo 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 May 8, 2021
@llogiq
Copy link
Contributor

llogiq commented May 8, 2021

So the lint should not fire if the macro in question does resolve to anything but std::print.

@DjLogozzo
Copy link
Author

DjLogozzo commented May 9, 2021

Ideally, yes.
Especially since if someone is implementing a custom print macro, even if there is a println macro also implimented, it may not function identically to print + \n, it may have additional side effects such as injecting a carriage return along with the newline or scrolling a physical/pseudo terminal; so in a situation where the programmer want to print purely a newline, println may not be a suitable alternative

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.

2 participants