Skip to content

Commit b799d4c

Browse files
committed
Add attribute documentation
1 parent c0512ba commit b799d4c

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- [Installation](installation.md)
66
- [Usage](usage.md)
7+
- [Extending Clippy coverage](lib_usage.md)
78
- [Configuration](configuration.md)
89
- [Lint Configuration](lint_configuration.md)
910
- [Clippy's Lints](lints.md)

book/src/lib_usage.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Modifying Clippy behavior with attributes
2+
3+
In some cases it is possible extend Clippy coverage to include 3rd party libraries.
4+
At this moment, only one such modification is possible: adding a
5+
`#[clippy::format_args]` attribute to a macro that supports `format!`-like syntax.
6+
7+
## `#[clippy::format_args]`
8+
9+
This attribute can be added to a macro that supports `format!`-like syntax.
10+
It tells Clippy that the macro is a formatting macro, and that the arguments to the macro
11+
should be linted as if they were arguments to `format!`. Any lint that would apply to a
12+
`format!` call will also apply to the macro call. The macro may have additional arguments
13+
before the format string, and these will be ignored.
14+
15+
### Example
16+
17+
Note that the `#[clippy::format_args]` is only available in v1.84, and will
18+
cause an `error: usage of unknown attribute` when running older `clippy`.
19+
To avoid this, you can use the [rustversion](https://github.com/dtolnay/rustversion)
20+
crate to apply the attribute conditionally.
21+
22+
```rust
23+
/// A macro that prints a message if a condition is true
24+
#[macro_export]
25+
#[rustversion::attr(since(1.84), clippy::format_args)]
26+
macro_rules! print_if {
27+
($condition:expr, $($args:tt)+) => {{
28+
if $condition {
29+
println!($($args)+)
30+
}
31+
}};
32+
}
33+
```

0 commit comments

Comments
 (0)