Skip to content

Commit

Permalink
Add attribute documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Oct 21, 2024
1 parent c0512ba commit b799d4c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- [Installation](installation.md)
- [Usage](usage.md)
- [Extending Clippy coverage](lib_usage.md)
- [Configuration](configuration.md)
- [Lint Configuration](lint_configuration.md)
- [Clippy's Lints](lints.md)
Expand Down
33 changes: 33 additions & 0 deletions book/src/lib_usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Modifying Clippy behavior with attributes

In some cases it is possible extend Clippy coverage to include 3rd party libraries.
At this moment, only one such modification is possible: adding a
`#[clippy::format_args]` attribute to a macro that supports `format!`-like syntax.

## `#[clippy::format_args]`

This attribute can be added to a macro that supports `format!`-like syntax.
It tells Clippy that the macro is a formatting macro, and that the arguments to the macro
should be linted as if they were arguments to `format!`. Any lint that would apply to a
`format!` call will also apply to the macro call. The macro may have additional arguments
before the format string, and these will be ignored.

### Example

Note that the `#[clippy::format_args]` is only available in v1.84, and will
cause an `error: usage of unknown attribute` when running older `clippy`.
To avoid this, you can use the [rustversion](https://github.com/dtolnay/rustversion)
crate to apply the attribute conditionally.

```rust
/// A macro that prints a message if a condition is true
#[macro_export]
#[rustversion::attr(since(1.84), clippy::format_args)]
macro_rules! print_if {
($condition:expr, $($args:tt)+) => {{
if $condition {
println!($($args)+)
}
}};
}
```

0 comments on commit b799d4c

Please sign in to comment.