Skip to content

Commit a2fb591

Browse files
committed
Improve docs with examples
1 parent c38e9d7 commit a2fb591

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

clippy_lints/src/format_args.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,26 @@ declare_clippy_lint! {
8585
/// # let foo = 42;
8686
/// format!("{foo}");
8787
/// ```
88+
///
89+
/// ### Supported cases
90+
///
91+
/// code | suggestion | comment
92+
/// ---|---|---
93+
/// `print!("{}", var)` | `print!("{var}")` | simple variables
94+
/// `print!("{0}", var)` | `print!("{var}")` | positional variables
95+
/// `print!("{v}", v=var)` | `print!("{var}")` | named variables
96+
/// `print!("{0} {0}", var)` | `print!("{var} {var}")` | aliased variables
97+
/// `print!("{0:1$}", var, width)` | `print!("{var:width$}")` | width support
98+
/// `print!("{0:.1$}", var, prec)` | `print!("{var:.prec$}")` | precision support
99+
/// `print!("{:.*}", prec, var)` | `print!("{var:.prec$}")` | asterisk support
100+
///
101+
/// ### Unsupported cases
102+
///
103+
/// code | suggestion | comment
104+
/// ---|---|---
105+
/// `print!("{0}={1}", var, 1+2)` | `print!("{var}={0}", 1+2)` | Format string uses an indexed argument that cannot be inlined. Supporting this case requires re-indexing of the format string.
106+
///
107+
/// changelog: [`needless-format-args`]: A new lint to inline format arguments, i.e. `print!("{}", var)` into `print!("{var}")`
88108
#[clippy::version = "1.64.0"]
89109
pub NEEDLESS_FORMAT_ARGS,
90110
nursery,

src/docs/needless_format_args.txt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,24 @@ format!("{}", foo);
1414
Use instead:
1515
```
1616
format!("{foo}");
17-
```
17+
```
18+
19+
### Supported cases
20+
21+
code | suggestion | comment
22+
---|---|---
23+
`print!("{}", var)` | `print!("{var}")` | simple variables
24+
`print!("{0}", var)` | `print!("{var}")` | positional variables
25+
`print!("{v}", v=var)` | `print!("{var}")` | named variables
26+
`print!("{0} {0}", var)` | `print!("{var} {var}")` | aliased variables
27+
`print!("{0:1$}", var, width)` | `print!("{var:width$}")` | width support
28+
`print!("{0:.1$}", var, prec)` | `print!("{var:.prec$}")` | precision support
29+
`print!("{:.*}", prec, var)` | `print!("{var:.prec$}")` | asterisk support
30+
31+
### Unsupported cases
32+
33+
code | suggestion | comment
34+
---|---|---
35+
`print!("{0}={1}", var, 1+2)` | `print!("{var}={0}", 1+2)` | Format string uses an indexed argument that cannot be inlined. Supporting this case requires re-indexing of the format string.
36+
37+
changelog: [`needless-format-args`]: A new lint to inline format arguments, i.e. `print!("{}", var)` into `print!("{var}")`

0 commit comments

Comments
 (0)