Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions clippy_lints/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,22 @@ declare_clippy_lint! {

pub struct Write {
format_args: FormatArgsStorage,
in_debug_impl: bool,
debug_impl_depth: u32,
allow_print_in_tests: bool,
}

impl Write {
pub fn new(conf: &'static Conf, format_args: FormatArgsStorage) -> Self {
Self {
format_args,
in_debug_impl: false,
debug_impl_depth: 0,
allow_print_in_tests: conf.allow_print_in_tests,
}
}

fn in_debug_impl(&self) -> bool {
self.debug_impl_depth != 0
}
}

impl_lint_pass!(Write => [
Expand All @@ -269,13 +273,13 @@ impl_lint_pass!(Write => [
impl<'tcx> LateLintPass<'tcx> for Write {
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
if is_debug_impl(cx, item) {
self.in_debug_impl = true;
self.debug_impl_depth += 1;
}
}

fn check_item_post(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
if is_debug_impl(cx, item) {
self.in_debug_impl = false;
self.debug_impl_depth -= 1;
}
}

Expand Down Expand Up @@ -329,7 +333,7 @@ impl<'tcx> LateLintPass<'tcx> for Write {

check_literal(cx, format_args, name);

if !self.in_debug_impl {
if !self.in_debug_impl() {
for piece in &format_args.template {
if let &FormatArgsPiece::Placeholder(FormatPlaceholder {
span: Some(span),
Expand Down
44 changes: 0 additions & 44 deletions tests/ui/print.rs

This file was deleted.

56 changes: 0 additions & 56 deletions tests/ui/print.stderr

This file was deleted.

23 changes: 23 additions & 0 deletions tests/ui/print_stdout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![expect(clippy::print_literal)]
#![warn(clippy::print_stdout)]

fn main() {
println!("Hello");
//~^ print_stdout

print!("Hello");
//~^ print_stdout

print!("Hello {}", "World");
//~^ print_stdout

print!("Hello {:?}", "World");
//~^ print_stdout

print!("Hello {:#?}", "#orld");
//~^ print_stdout

assert_eq!(42, 1337);

vec![1, 2];
}
35 changes: 35 additions & 0 deletions tests/ui/print_stdout.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
error: use of `println!`
--> tests/ui/print_stdout.rs:5:5
|
LL | println!("Hello");
| ^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::print-stdout` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::print_stdout)]`

error: use of `print!`
--> tests/ui/print_stdout.rs:8:5
|
LL | print!("Hello");
| ^^^^^^^^^^^^^^^

error: use of `print!`
--> tests/ui/print_stdout.rs:11:5
|
LL | print!("Hello {}", "World");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: use of `print!`
--> tests/ui/print_stdout.rs:14:5
|
LL | print!("Hello {:?}", "World");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: use of `print!`
--> tests/ui/print_stdout.rs:17:5
|
LL | print!("Hello {:#?}", "#orld");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 5 previous errors

50 changes: 50 additions & 0 deletions tests/ui/use_debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#![warn(clippy::use_debug)]

use std::fmt::{Debug, Display, Formatter, Result};

struct Foo;

impl Display for Foo {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "{:?}", 43.1415)
//~^ use_debug
}
}

impl Debug for Foo {
fn fmt(&self, f: &mut Formatter) -> Result {
// ok, we can use `Debug` formatting in `Debug` implementations
write!(f, "{:?}", 42.718)
}
}

fn main() {
print!("Hello {:?}", "World");
//~^ use_debug

print!("Hello {:#?}", "#orld");
//~^ use_debug

assert_eq!(42, 1337);

vec![1, 2];
}

// don't get confused by nested impls
fn issue15942() {
struct Bar;
impl Debug for Bar {
fn fmt(&self, f: &mut Formatter) -> Result {
struct Baz;
impl Debug for Baz {
fn fmt(&self, f: &mut Formatter) -> Result {
// ok, we can use `Debug` formatting in `Debug` implementations
write!(f, "{:?}", 42.718)
}
}

// ok, we can use `Debug` formatting in `Debug` implementations
write!(f, "{:?}", 42.718)
}
}
}
23 changes: 23 additions & 0 deletions tests/ui/use_debug.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error: use of `Debug`-based formatting
--> tests/ui/use_debug.rs:9:20
|
LL | write!(f, "{:?}", 43.1415)
| ^^^^
|
= note: `-D clippy::use-debug` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::use_debug)]`

error: use of `Debug`-based formatting
--> tests/ui/use_debug.rs:22:19
|
LL | print!("Hello {:?}", "World");
| ^^^^

error: use of `Debug`-based formatting
--> tests/ui/use_debug.rs:25:19
|
LL | print!("Hello {:#?}", "#orld");
| ^^^^^

error: aborting due to 3 previous errors