Skip to content

Commit

Permalink
rm indoc, ignore expanded macros
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Sep 25, 2022
1 parent ba320c7 commit 6685596
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 163 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ clap = { version = "3.1", features = ["derive"] }
clippy_utils = { path = "clippy_utils" }
derive-new = "0.5"
if_chain = "1.0"
indoc = "1.0"
itertools = "0.10.1"
quote = "1.0"
serde = { version = "1.0.125", features = ["derive"] }
Expand Down
22 changes: 13 additions & 9 deletions clippy_lints/src/format_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ impl<'tcx> LateLintPass<'tcx> for FormatArgs {
}

fn check_uninlined_args(cx: &LateContext<'_>, args: &FormatArgsExpn<'_>, call_site: Span) {
if args.format_string.span.from_expansion() {
return;
}

let mut fixes = Vec::new();
// If any of the arguments are referenced by an index number,
// and that argument is not a simple variable and cannot be inlined,
Expand Down Expand Up @@ -204,17 +208,17 @@ fn check_one_arg(cx: &LateContext<'_>, param: &FormatParam<'_>, fixes: &mut Vec<
&& let [segment] = segments
{
let replacement = match param.usage {
FormatParamUsage::Argument => segment.ident.name.to_string(),
FormatParamUsage::Argument => segment.ident.name.to_string(),
FormatParamUsage::Width => format!("{}$", segment.ident.name),
FormatParamUsage::Precision => format!(".{}$", segment.ident.name),
};
fixes.push((param.span, replacement));
let arg_span = expand_past_previous_comma(cx, *span);
fixes.push((arg_span, String::new()));
true // successful inlining, continue checking
} else {
// if we can't inline a numbered argument, we can't continue
param.kind != Numbered
};
fixes.push((param.span, replacement));
let arg_span = expand_past_previous_comma(cx, *span);
fixes.push((arg_span, String::new()));
true // successful inlining, continue checking
} else {
// if we can't inline a numbered argument, we can't continue
param.kind != Numbered
}
}

Expand Down
3 changes: 0 additions & 3 deletions tests/compile-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ static TEST_DEPENDENCIES: &[&str] = &[
"tokio",
"parking_lot",
"rustc_semver",
"indoc",
];

// Test dependencies may need an `extern crate` here to ensure that they show up
Expand All @@ -53,8 +52,6 @@ extern crate futures;
#[allow(unused_extern_crates)]
extern crate if_chain;
#[allow(unused_extern_crates)]
extern crate indoc;
#[allow(unused_extern_crates)]
extern crate itertools;
#[allow(unused_extern_crates)]
extern crate parking_lot;
Expand Down
3 changes: 0 additions & 3 deletions tests/ui/format_args.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#![allow(clippy::print_literal)]
#![warn(clippy::to_string_in_format_args)]

use indoc::{indoc, printdoc};
use std::io::{stdout, Write};
use std::ops::Deref;
use std::panic::Location;
Expand Down Expand Up @@ -113,8 +112,6 @@ fn main() {
println!("error: something failed at {}", my_other_macro!());
// https://github.com/rust-lang/rust-clippy/issues/7903
println!("{foo}{foo:?}", foo = "foo".to_string());
println!(indoc!("{}"), Location::caller());
printdoc!("{}", Location::caller().to_string());
}

fn issue8643(vendor_id: usize, product_id: usize, name: &str) {
Expand Down
3 changes: 0 additions & 3 deletions tests/ui/format_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#![allow(clippy::print_literal)]
#![warn(clippy::to_string_in_format_args)]

use indoc::{indoc, printdoc};
use std::io::{stdout, Write};
use std::ops::Deref;
use std::panic::Location;
Expand Down Expand Up @@ -113,8 +112,6 @@ fn main() {
println!("error: something failed at {}", my_other_macro!());
// https://github.com/rust-lang/rust-clippy/issues/7903
println!("{foo}{foo:?}", foo = "foo".to_string());
println!(indoc!("{}"), Location::caller().to_string());
printdoc!("{}", Location::caller().to_string());
}

fn issue8643(vendor_id: usize, product_id: usize, name: &str) {
Expand Down
54 changes: 24 additions & 30 deletions tests/ui/format_args.stderr
Original file line number Diff line number Diff line change
@@ -1,148 +1,142 @@
error: `to_string` applied to a type that implements `Display` in `format!` args
--> $DIR/format_args.rs:75:72
--> $DIR/format_args.rs:74:72
|
LL | let _ = format!("error: something failed at {}", Location::caller().to_string());
| ^^^^^^^^^^^^ help: remove this
|
= note: `-D clippy::to-string-in-format-args` implied by `-D warnings`

error: `to_string` applied to a type that implements `Display` in `write!` args
--> $DIR/format_args.rs:79:27
--> $DIR/format_args.rs:78:27
|
LL | Location::caller().to_string()
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `writeln!` args
--> $DIR/format_args.rs:84:27
--> $DIR/format_args.rs:83:27
|
LL | Location::caller().to_string()
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `print!` args
--> $DIR/format_args.rs:86:63
--> $DIR/format_args.rs:85:63
|
LL | print!("error: something failed at {}", Location::caller().to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:87:65
--> $DIR/format_args.rs:86:65
|
LL | println!("error: something failed at {}", Location::caller().to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `eprint!` args
--> $DIR/format_args.rs:88:64
--> $DIR/format_args.rs:87:64
|
LL | eprint!("error: something failed at {}", Location::caller().to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `eprintln!` args
--> $DIR/format_args.rs:89:66
--> $DIR/format_args.rs:88:66
|
LL | eprintln!("error: something failed at {}", Location::caller().to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `format_args!` args
--> $DIR/format_args.rs:90:77
--> $DIR/format_args.rs:89:77
|
LL | let _ = format_args!("error: something failed at {}", Location::caller().to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `assert!` args
--> $DIR/format_args.rs:91:70
--> $DIR/format_args.rs:90:70
|
LL | assert!(true, "error: something failed at {}", Location::caller().to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `assert_eq!` args
--> $DIR/format_args.rs:92:73
--> $DIR/format_args.rs:91:73
|
LL | assert_eq!(0, 0, "error: something failed at {}", Location::caller().to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `assert_ne!` args
--> $DIR/format_args.rs:93:73
--> $DIR/format_args.rs:92:73
|
LL | assert_ne!(0, 0, "error: something failed at {}", Location::caller().to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `panic!` args
--> $DIR/format_args.rs:94:63
--> $DIR/format_args.rs:93:63
|
LL | panic!("error: something failed at {}", Location::caller().to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:95:20
--> $DIR/format_args.rs:94:20
|
LL | println!("{}", X(1).to_string());
| ^^^^^^^^^^^^^^^^ help: use this: `*X(1)`

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:96:20
--> $DIR/format_args.rs:95:20
|
LL | println!("{}", Y(&X(1)).to_string());
| ^^^^^^^^^^^^^^^^^^^^ help: use this: `***Y(&X(1))`

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:97:24
--> $DIR/format_args.rs:96:24
|
LL | println!("{}", Z(1).to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:98:20
--> $DIR/format_args.rs:97:20
|
LL | println!("{}", x.to_string());
| ^^^^^^^^^^^^^ help: use this: `**x`

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:99:20
--> $DIR/format_args.rs:98:20
|
LL | println!("{}", x_ref.to_string());
| ^^^^^^^^^^^^^^^^^ help: use this: `***x_ref`

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:101:39
--> $DIR/format_args.rs:100:39
|
LL | println!("{foo}{bar}", foo = "foo".to_string(), bar = "bar");
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:102:52
--> $DIR/format_args.rs:101:52
|
LL | println!("{foo}{bar}", foo = "foo", bar = "bar".to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:103:39
--> $DIR/format_args.rs:102:39
|
LL | println!("{foo}{bar}", bar = "bar".to_string(), foo = "foo");
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:104:52
--> $DIR/format_args.rs:103:52
|
LL | println!("{foo}{bar}", bar = "bar", foo = "foo".to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:116:46
|
LL | println!(indoc!("{}"), Location::caller().to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `format!` args
--> $DIR/format_args.rs:145:38
--> $DIR/format_args.rs:142:38
|
LL | let x = format!("{} {}", a, b.to_string());
| ^^^^^^^^^^^^ help: remove this

error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/format_args.rs:159:24
--> $DIR/format_args.rs:156:24
|
LL | println!("{}", original[..10].to_string());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use this: `&original[..10]`

error: aborting due to 24 previous errors
error: aborting due to 23 previous errors

6 changes: 0 additions & 6 deletions tests/ui/format_args_unfixable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#![warn(clippy::format_in_format_args)]
#![warn(clippy::to_string_in_format_args)]

use indoc::{indoc, printdoc};
use std::io::{stdout, Error, ErrorKind, Write};
use std::ops::Deref;
use std::panic::Location;
Expand Down Expand Up @@ -51,11 +50,6 @@ fn main() {
assert_eq!(0, 0, "error: {}", format!("something failed at {}", Location::caller()));
assert_ne!(0, 0, "error: {}", format!("something failed at {}", Location::caller()));
panic!("error: {}", format!("something failed at {}", Location::caller()));
panic!(
indoc!("error: {}"),
format!("something failed at {}", Location::caller())
);
printdoc!("error: {}", format!("something failed at {}", Location::caller()));

// negative tests
println!("error: {}", format_args!("something failed at {}", Location::caller()));
Expand Down
Loading

0 comments on commit 6685596

Please sign in to comment.