about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/format_args_unfixable.rs44
-rw-r--r--tests/ui/format_args_unfixable.stderr36
-rw-r--r--tests/ui/uninlined_format_args.fixed86
-rw-r--r--tests/ui/uninlined_format_args.rs86
4 files changed, 232 insertions, 20 deletions
diff --git a/tests/ui/format_args_unfixable.rs b/tests/ui/format_args_unfixable.rs
index eb0ac15bfbf..423bfaf9796 100644
--- a/tests/ui/format_args_unfixable.rs
+++ b/tests/ui/format_args_unfixable.rs
@@ -1,4 +1,5 @@
 #![warn(clippy::format_in_format_args, clippy::to_string_in_format_args)]
+#![allow(unused)]
 #![allow(clippy::assertions_on_constants, clippy::eq_op, clippy::uninlined_format_args)]
 
 use std::io::{stdout, Error, ErrorKind, Write};
@@ -57,3 +58,46 @@ fn main() {
     my_macro!();
     println!("error: {}", my_other_macro!());
 }
+
+macro_rules! _internal {
+    ($($args:tt)*) => {
+        println!("{}", format_args!($($args)*))
+    };
+}
+
+macro_rules! my_println2 {
+   ($target:expr, $($args:tt)+) => {{
+       if $target {
+           _internal!($($args)+)
+       }
+    }};
+}
+
+macro_rules! my_println2_args {
+    ($target:expr, $($args:tt)+) => {{
+       if $target {
+           _internal!("foo: {}", format_args!($($args)+))
+       }
+    }};
+}
+
+fn test2() {
+    let error = Error::new(ErrorKind::Other, "bad thing");
+
+    // None of these should be linted without the config change
+    my_println2!(true, "error: {}", format!("something failed at {}", Location::caller()));
+    my_println2!(
+        true,
+        "{}: {}",
+        error,
+        format!("something failed at {}", Location::caller())
+    );
+
+    my_println2_args!(true, "error: {}", format!("something failed at {}", Location::caller()));
+    my_println2_args!(
+        true,
+        "{}: {}",
+        error,
+        format!("something failed at {}", Location::caller())
+    );
+}
diff --git a/tests/ui/format_args_unfixable.stderr b/tests/ui/format_args_unfixable.stderr
index b291d475ad9..c1be48c3b72 100644
--- a/tests/ui/format_args_unfixable.stderr
+++ b/tests/ui/format_args_unfixable.stderr
@@ -1,5 +1,5 @@
 error: `format!` in `println!` args
-  --> $DIR/format_args_unfixable.rs:25:5
+  --> $DIR/format_args_unfixable.rs:26:5
    |
 LL |     println!("error: {}", format!("something failed at {}", Location::caller()));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -9,7 +9,7 @@ LL |     println!("error: {}", format!("something failed at {}", Location::calle
    = note: `-D clippy::format-in-format-args` implied by `-D warnings`
 
 error: `format!` in `println!` args
-  --> $DIR/format_args_unfixable.rs:26:5
+  --> $DIR/format_args_unfixable.rs:27:5
    |
 LL |     println!("{}: {}", error, format!("something failed at {}", Location::caller()));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -18,7 +18,7 @@ LL |     println!("{}: {}", error, format!("something failed at {}", Location::c
    = help: or consider changing `format!` to `format_args!`
 
 error: `format!` in `println!` args
-  --> $DIR/format_args_unfixable.rs:27:5
+  --> $DIR/format_args_unfixable.rs:28:5
    |
 LL |     println!("{:?}: {}", error, format!("something failed at {}", Location::caller()));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -27,7 +27,7 @@ LL |     println!("{:?}: {}", error, format!("something failed at {}", Location:
    = help: or consider changing `format!` to `format_args!`
 
 error: `format!` in `println!` args
-  --> $DIR/format_args_unfixable.rs:28:5
+  --> $DIR/format_args_unfixable.rs:29:5
    |
 LL |     println!("{{}}: {}", format!("something failed at {}", Location::caller()));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -36,7 +36,7 @@ LL |     println!("{{}}: {}", format!("something failed at {}", Location::caller
    = help: or consider changing `format!` to `format_args!`
 
 error: `format!` in `println!` args
-  --> $DIR/format_args_unfixable.rs:29:5
+  --> $DIR/format_args_unfixable.rs:30:5
    |
 LL |     println!(r#"error: "{}""#, format!("something failed at {}", Location::caller()));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -45,7 +45,7 @@ LL |     println!(r#"error: "{}""#, format!("something failed at {}", Location::
    = help: or consider changing `format!` to `format_args!`
 
 error: `format!` in `println!` args
-  --> $DIR/format_args_unfixable.rs:30:5
+  --> $DIR/format_args_unfixable.rs:31:5
    |
 LL |     println!("error: {}", format!(r#"something failed at "{}""#, Location::caller()));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -54,7 +54,7 @@ LL |     println!("error: {}", format!(r#"something failed at "{}""#, Location::
    = help: or consider changing `format!` to `format_args!`
 
 error: `format!` in `println!` args
-  --> $DIR/format_args_unfixable.rs:31:5
+  --> $DIR/format_args_unfixable.rs:32:5
    |
 LL |     println!("error: {}", format!("something failed at {} {0}", Location::caller()));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -63,7 +63,7 @@ LL |     println!("error: {}", format!("something failed at {} {0}", Location::c
    = help: or consider changing `format!` to `format_args!`
 
 error: `format!` in `format!` args
-  --> $DIR/format_args_unfixable.rs:32:13
+  --> $DIR/format_args_unfixable.rs:33:13
    |
 LL |     let _ = format!("error: {}", format!("something failed at {}", Location::caller()));
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -72,7 +72,7 @@ LL |     let _ = format!("error: {}", format!("something failed at {}", Location
    = help: or consider changing `format!` to `format_args!`
 
 error: `format!` in `write!` args
-  --> $DIR/format_args_unfixable.rs:33:13
+  --> $DIR/format_args_unfixable.rs:34:13
    |
 LL |       let _ = write!(
    |  _____________^
@@ -86,7 +86,7 @@ LL | |     );
    = help: or consider changing `format!` to `format_args!`
 
 error: `format!` in `writeln!` args
-  --> $DIR/format_args_unfixable.rs:38:13
+  --> $DIR/format_args_unfixable.rs:39:13
    |
 LL |       let _ = writeln!(
    |  _____________^
@@ -100,7 +100,7 @@ LL | |     );
    = help: or consider changing `format!` to `format_args!`
 
 error: `format!` in `print!` args
-  --> $DIR/format_args_unfixable.rs:43:5
+  --> $DIR/format_args_unfixable.rs:44:5
    |
 LL |     print!("error: {}", format!("something failed at {}", Location::caller()));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -109,7 +109,7 @@ LL |     print!("error: {}", format!("something failed at {}", Location::caller(
    = help: or consider changing `format!` to `format_args!`
 
 error: `format!` in `eprint!` args
-  --> $DIR/format_args_unfixable.rs:44:5
+  --> $DIR/format_args_unfixable.rs:45:5
    |
 LL |     eprint!("error: {}", format!("something failed at {}", Location::caller()));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -118,7 +118,7 @@ LL |     eprint!("error: {}", format!("something failed at {}", Location::caller
    = help: or consider changing `format!` to `format_args!`
 
 error: `format!` in `eprintln!` args
-  --> $DIR/format_args_unfixable.rs:45:5
+  --> $DIR/format_args_unfixable.rs:46:5
    |
 LL |     eprintln!("error: {}", format!("something failed at {}", Location::caller()));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -127,7 +127,7 @@ LL |     eprintln!("error: {}", format!("something failed at {}", Location::call
    = help: or consider changing `format!` to `format_args!`
 
 error: `format!` in `format_args!` args
-  --> $DIR/format_args_unfixable.rs:46:13
+  --> $DIR/format_args_unfixable.rs:47:13
    |
 LL |     let _ = format_args!("error: {}", format!("something failed at {}", Location::caller()));
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -136,7 +136,7 @@ LL |     let _ = format_args!("error: {}", format!("something failed at {}", Loc
    = help: or consider changing `format!` to `format_args!`
 
 error: `format!` in `assert!` args
-  --> $DIR/format_args_unfixable.rs:47:5
+  --> $DIR/format_args_unfixable.rs:48:5
    |
 LL |     assert!(true, "error: {}", format!("something failed at {}", Location::caller()));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -145,7 +145,7 @@ LL |     assert!(true, "error: {}", format!("something failed at {}", Location::
    = help: or consider changing `format!` to `format_args!`
 
 error: `format!` in `assert_eq!` args
-  --> $DIR/format_args_unfixable.rs:48:5
+  --> $DIR/format_args_unfixable.rs:49:5
    |
 LL |     assert_eq!(0, 0, "error: {}", format!("something failed at {}", Location::caller()));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -154,7 +154,7 @@ LL |     assert_eq!(0, 0, "error: {}", format!("something failed at {}", Locatio
    = help: or consider changing `format!` to `format_args!`
 
 error: `format!` in `assert_ne!` args
-  --> $DIR/format_args_unfixable.rs:49:5
+  --> $DIR/format_args_unfixable.rs:50:5
    |
 LL |     assert_ne!(0, 0, "error: {}", format!("something failed at {}", Location::caller()));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -163,7 +163,7 @@ LL |     assert_ne!(0, 0, "error: {}", format!("something failed at {}", Locatio
    = help: or consider changing `format!` to `format_args!`
 
 error: `format!` in `panic!` args
-  --> $DIR/format_args_unfixable.rs:50:5
+  --> $DIR/format_args_unfixable.rs:51:5
    |
 LL |     panic!("error: {}", format!("something failed at {}", Location::caller()));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/tests/ui/uninlined_format_args.fixed b/tests/ui/uninlined_format_args.fixed
index 5ecb9a5bf9e..3122081a44f 100644
--- a/tests/ui/uninlined_format_args.fixed
+++ b/tests/ui/uninlined_format_args.fixed
@@ -1,7 +1,7 @@
 // aux-build:proc_macros.rs
 // run-rustfix
 #![warn(clippy::uninlined_format_args)]
-#![allow(named_arguments_used_positionally, unused_imports, unused_macros, unused_variables)]
+#![allow(named_arguments_used_positionally, unused)]
 #![allow(clippy::eq_op, clippy::format_in_format_args, clippy::print_literal)]
 
 extern crate proc_macros;
@@ -178,3 +178,87 @@ fn _meets_msrv() {
 fn _do_not_fire() {
     println!("{:?}", None::<()>);
 }
+
+macro_rules! _internal {
+    ($($args:tt)*) => {
+        println!("{}", format_args!($($args)*))
+    };
+}
+
+macro_rules! my_println2 {
+   ($target:expr, $($args:tt)+) => {{
+       if $target {
+           _internal!($($args)+)
+       }
+    }};
+}
+
+macro_rules! my_println2_args {
+    ($target:expr, $($args:tt)+) => {{
+       if $target {
+           _internal!("foo: {}", format_args!($($args)+))
+       }
+    }};
+}
+
+macro_rules! my_concat {
+    ($fmt:literal $(, $e:expr)*) => {
+        println!(concat!("ERROR: ", $fmt), $($e,)*)
+    }
+}
+
+macro_rules! my_good_macro {
+    ($fmt:literal $(, $e:expr)* $(,)?) => {
+        println!($fmt $(, $e)*)
+    }
+}
+
+macro_rules! my_bad_macro {
+    ($fmt:literal, $($e:expr),*) => {
+        println!($fmt, $($e,)*)
+    }
+}
+
+macro_rules! my_bad_macro2 {
+    ($fmt:literal) => {
+        let s = $fmt.clone();
+        println!("{}", s);
+    };
+    ($fmt:literal, $($e:expr)+) => {
+        println!($fmt, $($e,)*)
+    };
+}
+
+// This abomination was suggested by @Alexendoo, may the Rust gods have mercy on their soul...
+// https://github.com/rust-lang/rust-clippy/pull/9948#issuecomment-1327965962
+macro_rules! used_twice {
+    (
+        large = $large:literal,
+        small = $small:literal,
+        $val:expr,
+    ) => {
+        if $val < 5 {
+            println!($small, $val);
+        } else {
+            println!($large, $val);
+        }
+    };
+}
+
+fn tester2() {
+    let local_i32 = 1;
+    my_println2_args!(true, "{}", local_i32);
+    my_println2!(true, "{}", local_i32);
+    my_concat!("{}", local_i32);
+    my_good_macro!("{}", local_i32);
+    my_good_macro!("{}", local_i32,);
+
+    // FIXME: Broken false positives, currently unhandled
+    my_bad_macro!("{}", local_i32);
+    my_bad_macro2!("{}", local_i32);
+    used_twice! {
+        large = "large value: {}",
+        small = "small value: {}",
+        local_i32,
+    };
+}
diff --git a/tests/ui/uninlined_format_args.rs b/tests/ui/uninlined_format_args.rs
index 835afac393f..b153ef256e0 100644
--- a/tests/ui/uninlined_format_args.rs
+++ b/tests/ui/uninlined_format_args.rs
@@ -1,7 +1,7 @@
 // aux-build:proc_macros.rs
 // run-rustfix
 #![warn(clippy::uninlined_format_args)]
-#![allow(named_arguments_used_positionally, unused_imports, unused_macros, unused_variables)]
+#![allow(named_arguments_used_positionally, unused)]
 #![allow(clippy::eq_op, clippy::format_in_format_args, clippy::print_literal)]
 
 extern crate proc_macros;
@@ -183,3 +183,87 @@ fn _meets_msrv() {
 fn _do_not_fire() {
     println!("{:?}", None::<()>);
 }
+
+macro_rules! _internal {
+    ($($args:tt)*) => {
+        println!("{}", format_args!($($args)*))
+    };
+}
+
+macro_rules! my_println2 {
+   ($target:expr, $($args:tt)+) => {{
+       if $target {
+           _internal!($($args)+)
+       }
+    }};
+}
+
+macro_rules! my_println2_args {
+    ($target:expr, $($args:tt)+) => {{
+       if $target {
+           _internal!("foo: {}", format_args!($($args)+))
+       }
+    }};
+}
+
+macro_rules! my_concat {
+    ($fmt:literal $(, $e:expr)*) => {
+        println!(concat!("ERROR: ", $fmt), $($e,)*)
+    }
+}
+
+macro_rules! my_good_macro {
+    ($fmt:literal $(, $e:expr)* $(,)?) => {
+        println!($fmt $(, $e)*)
+    }
+}
+
+macro_rules! my_bad_macro {
+    ($fmt:literal, $($e:expr),*) => {
+        println!($fmt, $($e,)*)
+    }
+}
+
+macro_rules! my_bad_macro2 {
+    ($fmt:literal) => {
+        let s = $fmt.clone();
+        println!("{}", s);
+    };
+    ($fmt:literal, $($e:expr)+) => {
+        println!($fmt, $($e,)*)
+    };
+}
+
+// This abomination was suggested by @Alexendoo, may the Rust gods have mercy on their soul...
+// https://github.com/rust-lang/rust-clippy/pull/9948#issuecomment-1327965962
+macro_rules! used_twice {
+    (
+        large = $large:literal,
+        small = $small:literal,
+        $val:expr,
+    ) => {
+        if $val < 5 {
+            println!($small, $val);
+        } else {
+            println!($large, $val);
+        }
+    };
+}
+
+fn tester2() {
+    let local_i32 = 1;
+    my_println2_args!(true, "{}", local_i32);
+    my_println2!(true, "{}", local_i32);
+    my_concat!("{}", local_i32);
+    my_good_macro!("{}", local_i32);
+    my_good_macro!("{}", local_i32,);
+
+    // FIXME: Broken false positives, currently unhandled
+    my_bad_macro!("{}", local_i32);
+    my_bad_macro2!("{}", local_i32);
+    used_twice! {
+        large = "large value: {}",
+        small = "small value: {}",
+        local_i32,
+    };
+}