Auto merge of #6821 - Jarcho:write_literal_suggestion, r=flip1995
Write literal suggestion fixes: #6768 changelog: Add suggestion to `write_literal` and `print_literal` lints changelog: Change `use_debug` to point only at the format string
This commit is contained in:
commit
d695bfc56f
6 changed files with 454 additions and 115 deletions
|
|
@ -1,8 +1,8 @@
|
|||
error: use of `Debug`-based formatting
|
||||
--> $DIR/print.rs:11:19
|
||||
--> $DIR/print.rs:11:20
|
||||
|
|
||||
LL | write!(f, "{:?}", 43.1415)
|
||||
| ^^^^^^
|
||||
| ^^^^
|
||||
|
|
||||
= note: `-D clippy::use-debug` implied by `-D warnings`
|
||||
|
||||
|
|
@ -33,10 +33,10 @@ LL | print!("Hello {:?}", "World");
|
|||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: use of `Debug`-based formatting
|
||||
--> $DIR/print.rs:28:12
|
||||
--> $DIR/print.rs:28:19
|
||||
|
|
||||
LL | print!("Hello {:?}", "World");
|
||||
| ^^^^^^^^^^^^
|
||||
| ^^^^
|
||||
|
||||
error: use of `print!`
|
||||
--> $DIR/print.rs:30:5
|
||||
|
|
@ -45,10 +45,10 @@ LL | print!("Hello {:#?}", "#orld");
|
|||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: use of `Debug`-based formatting
|
||||
--> $DIR/print.rs:30:12
|
||||
--> $DIR/print.rs:30:19
|
||||
|
|
||||
LL | print!("Hello {:#?}", "#orld");
|
||||
| ^^^^^^^^^^^^^
|
||||
| ^^^^^
|
||||
|
||||
error: aborting due to 8 previous errors
|
||||
|
||||
|
|
|
|||
|
|
@ -5,66 +5,120 @@ LL | print!("Hello {}", "world");
|
|||
| ^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::print-literal` implied by `-D warnings`
|
||||
help: try this
|
||||
|
|
||||
LL | print!("Hello world");
|
||||
| ^^^^^--
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/print_literal.rs:26:36
|
||||
|
|
||||
LL | println!("Hello {} {}", world, "world");
|
||||
| ^^^^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | println!("Hello {} world", world);
|
||||
| ^^^^^ --
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/print_literal.rs:27:26
|
||||
|
|
||||
LL | println!("Hello {}", "world");
|
||||
| ^^^^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | println!("Hello world");
|
||||
| ^^^^^--
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/print_literal.rs:32:25
|
||||
|
|
||||
LL | println!("{0} {1}", "hello", "world");
|
||||
| ^^^^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | println!("hello {1}", "world");
|
||||
| ^^^^^ --
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/print_literal.rs:32:34
|
||||
|
|
||||
LL | println!("{0} {1}", "hello", "world");
|
||||
| ^^^^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | println!("{0} world", "hello");
|
||||
| ^^^^^ --
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/print_literal.rs:33:25
|
||||
|
|
||||
LL | println!("{1} {0}", "hello", "world");
|
||||
| ^^^^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | println!("{1} hello", "world");
|
||||
| ^^^^^--
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/print_literal.rs:33:34
|
||||
|
|
||||
LL | println!("{1} {0}", "hello", "world");
|
||||
| ^^^^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | println!("world {0}", "hello");
|
||||
| ^^^^^ --
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/print_literal.rs:36:35
|
||||
--> $DIR/print_literal.rs:36:29
|
||||
|
|
||||
LL | println!("{foo} {bar}", foo = "hello", bar = "world");
|
||||
| ^^^^^^^
|
||||
| ^^^^^^^^^^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | println!("hello {bar}", bar = "world");
|
||||
| ^^^^^ --
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/print_literal.rs:36:50
|
||||
--> $DIR/print_literal.rs:36:44
|
||||
|
|
||||
LL | println!("{foo} {bar}", foo = "hello", bar = "world");
|
||||
| ^^^^^^^
|
||||
| ^^^^^^^^^^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | println!("{foo} world", foo = "hello");
|
||||
| ^^^^^ --
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/print_literal.rs:37:35
|
||||
--> $DIR/print_literal.rs:37:29
|
||||
|
|
||||
LL | println!("{bar} {foo}", foo = "hello", bar = "world");
|
||||
| ^^^^^^^
|
||||
| ^^^^^^^^^^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | println!("{bar} hello", bar = "world");
|
||||
| ^^^^^--
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/print_literal.rs:37:50
|
||||
--> $DIR/print_literal.rs:37:44
|
||||
|
|
||||
LL | println!("{bar} {foo}", foo = "hello", bar = "world");
|
||||
| ^^^^^^^
|
||||
| ^^^^^^^^^^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | println!("world {foo}", foo = "hello");
|
||||
| ^^^^^ --
|
||||
|
||||
error: aborting due to 11 previous errors
|
||||
|
||||
|
|
|
|||
|
|
@ -5,66 +5,120 @@ LL | write!(&mut v, "Hello {}", "world");
|
|||
| ^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::write-literal` implied by `-D warnings`
|
||||
help: try this
|
||||
|
|
||||
LL | write!(&mut v, "Hello world");
|
||||
| ^^^^^--
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/write_literal.rs:31:44
|
||||
|
|
||||
LL | writeln!(&mut v, "Hello {} {}", world, "world");
|
||||
| ^^^^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | writeln!(&mut v, "Hello {} world", world);
|
||||
| ^^^^^ --
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/write_literal.rs:32:34
|
||||
|
|
||||
LL | writeln!(&mut v, "Hello {}", "world");
|
||||
| ^^^^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | writeln!(&mut v, "Hello world");
|
||||
| ^^^^^--
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/write_literal.rs:37:33
|
||||
|
|
||||
LL | writeln!(&mut v, "{0} {1}", "hello", "world");
|
||||
| ^^^^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | writeln!(&mut v, "hello {1}", "world");
|
||||
| ^^^^^ --
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/write_literal.rs:37:42
|
||||
|
|
||||
LL | writeln!(&mut v, "{0} {1}", "hello", "world");
|
||||
| ^^^^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | writeln!(&mut v, "{0} world", "hello");
|
||||
| ^^^^^ --
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/write_literal.rs:38:33
|
||||
|
|
||||
LL | writeln!(&mut v, "{1} {0}", "hello", "world");
|
||||
| ^^^^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | writeln!(&mut v, "{1} hello", "world");
|
||||
| ^^^^^--
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/write_literal.rs:38:42
|
||||
|
|
||||
LL | writeln!(&mut v, "{1} {0}", "hello", "world");
|
||||
| ^^^^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | writeln!(&mut v, "world {0}", "hello");
|
||||
| ^^^^^ --
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/write_literal.rs:41:43
|
||||
--> $DIR/write_literal.rs:41:37
|
||||
|
|
||||
LL | writeln!(&mut v, "{foo} {bar}", foo = "hello", bar = "world");
|
||||
| ^^^^^^^
|
||||
| ^^^^^^^^^^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | writeln!(&mut v, "hello {bar}", bar = "world");
|
||||
| ^^^^^ --
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/write_literal.rs:41:58
|
||||
--> $DIR/write_literal.rs:41:52
|
||||
|
|
||||
LL | writeln!(&mut v, "{foo} {bar}", foo = "hello", bar = "world");
|
||||
| ^^^^^^^
|
||||
| ^^^^^^^^^^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | writeln!(&mut v, "{foo} world", foo = "hello");
|
||||
| ^^^^^ --
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/write_literal.rs:42:43
|
||||
--> $DIR/write_literal.rs:42:37
|
||||
|
|
||||
LL | writeln!(&mut v, "{bar} {foo}", foo = "hello", bar = "world");
|
||||
| ^^^^^^^
|
||||
| ^^^^^^^^^^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | writeln!(&mut v, "{bar} hello", bar = "world");
|
||||
| ^^^^^--
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/write_literal.rs:42:58
|
||||
--> $DIR/write_literal.rs:42:52
|
||||
|
|
||||
LL | writeln!(&mut v, "{bar} {foo}", foo = "hello", bar = "world");
|
||||
| ^^^^^^^
|
||||
| ^^^^^^^^^^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | writeln!(&mut v, "world {foo}", foo = "hello");
|
||||
| ^^^^^ --
|
||||
|
||||
error: aborting due to 11 previous errors
|
||||
|
||||
|
|
|
|||
27
tests/ui/write_literal_2.rs
Normal file
27
tests/ui/write_literal_2.rs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#![allow(unused_must_use)]
|
||||
#![warn(clippy::write_literal)]
|
||||
|
||||
use std::io::Write;
|
||||
|
||||
fn main() {
|
||||
let mut v = Vec::new();
|
||||
|
||||
writeln!(&mut v, "{}", "{hello}");
|
||||
writeln!(&mut v, r"{}", r"{hello}");
|
||||
writeln!(&mut v, "{}", '\'');
|
||||
writeln!(&mut v, "{}", '"');
|
||||
writeln!(&mut v, r"{}", '"'); // don't lint
|
||||
writeln!(&mut v, r"{}", '\'');
|
||||
writeln!(
|
||||
&mut v,
|
||||
"some {}",
|
||||
"hello \
|
||||
world!"
|
||||
);
|
||||
writeln!(
|
||||
&mut v,
|
||||
"some {}\
|
||||
{} \\ {}",
|
||||
"1", "2", "3",
|
||||
);
|
||||
}
|
||||
106
tests/ui/write_literal_2.stderr
Normal file
106
tests/ui/write_literal_2.stderr
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
error: literal with an empty format string
|
||||
--> $DIR/write_literal_2.rs:9:28
|
||||
|
|
||||
LL | writeln!(&mut v, "{}", "{hello}");
|
||||
| ^^^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::write-literal` implied by `-D warnings`
|
||||
help: try this
|
||||
|
|
||||
LL | writeln!(&mut v, "{{hello}}");
|
||||
| ^^^^^^^^^--
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/write_literal_2.rs:10:29
|
||||
|
|
||||
LL | writeln!(&mut v, r"{}", r"{hello}");
|
||||
| ^^^^^^^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | writeln!(&mut v, r"{{hello}}");
|
||||
| ^^^^^^^^^--
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/write_literal_2.rs:11:28
|
||||
|
|
||||
LL | writeln!(&mut v, "{}", '/'');
|
||||
| ^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | writeln!(&mut v, "'");
|
||||
| ^--
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/write_literal_2.rs:12:28
|
||||
|
|
||||
LL | writeln!(&mut v, "{}", '"');
|
||||
| ^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | writeln!(&mut v, "/"");
|
||||
| ^^--
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/write_literal_2.rs:14:29
|
||||
|
|
||||
LL | writeln!(&mut v, r"{}", '/'');
|
||||
| ^^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | writeln!(&mut v, r"'");
|
||||
| ^--
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/write_literal_2.rs:18:9
|
||||
|
|
||||
LL | / "hello /
|
||||
LL | | world!"
|
||||
| |_______________^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | "some hello /
|
||||
LL | world!"
|
||||
|
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/write_literal_2.rs:25:9
|
||||
|
|
||||
LL | "1", "2", "3",
|
||||
| ^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | "some 1{} / {}", "2", "3",
|
||||
| ^ --
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/write_literal_2.rs:25:14
|
||||
|
|
||||
LL | "1", "2", "3",
|
||||
| ^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | 2 / {}",
|
||||
LL | "1", "3",
|
||||
|
|
||||
|
||||
error: literal with an empty format string
|
||||
--> $DIR/write_literal_2.rs:25:19
|
||||
|
|
||||
LL | "1", "2", "3",
|
||||
| ^^^
|
||||
|
|
||||
help: try this
|
||||
|
|
||||
LL | {} / 3",
|
||||
LL | "1", "2",
|
||||
|
|
||||
|
||||
error: aborting due to 9 previous errors
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue