Skip to content

Commit

Permalink
Partial no-op refactoring of rust-lang#9948
Browse files Browse the repository at this point in the history
This contains preparatory work for rust-lang#9948 to keep that change to the
minimum, and make it easier to review it.
  • Loading branch information
nyurik authored and J-ZhengLi committed Aug 26, 2023
1 parent 3490145 commit cf78845
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 35 deletions.
8 changes: 2 additions & 6 deletions clippy_dev/src/new_lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,7 @@ fn create_lint_for_ty(lint: &LintData<'_>, enable_msrv: bool, ty: &str) -> io::R
}}
todo!();
}}
"#,
context_import = context_import,
name_upper = name_upper,
"#
);
} else {
let _: fmt::Result = writedoc!(
Expand All @@ -385,9 +383,7 @@ fn create_lint_for_ty(lint: &LintData<'_>, enable_msrv: bool, ty: &str) -> io::R
pub(super) fn check(cx: &{context_import}) {{
todo!();
}}
"#,
context_import = context_import,
name_upper = name_upper,
"#
);
}

Expand Down
14 changes: 5 additions & 9 deletions clippy_dev/src/update_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,17 +537,13 @@ fn declare_deprecated(name: &str, path: &Path, reason: &str) -> io::Result<()> {
/// Nothing. This lint has been deprecated.
///
/// ### Deprecation reason
/// {}
#[clippy::version = \"{}\"]
pub {},
\"{}\"
/// {deprecation_reason}
#[clippy::version = \"{version}\"]
pub {name},
\"{reason}\"
}}
",
deprecation_reason,
version,
name,
reason,
"
)
}

Expand Down
44 changes: 44 additions & 0 deletions tests/ui/format_args_unfixable.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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())
);
}
36 changes: 18 additions & 18 deletions tests/ui/format_args_unfixable.stderr
Original file line number Diff line number Diff line change
@@ -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()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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!(
| _____________^
Expand All @@ -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!(
| _____________^
Expand All @@ -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()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
86 changes: 85 additions & 1 deletion tests/ui/uninlined_format_args.fixed
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
};
}
Loading

0 comments on commit cf78845

Please sign in to comment.