diff --git a/book/src/SUMMARY.md b/book/src/SUMMARY.md index be13fcbe260f..5733873442d7 100644 --- a/book/src/SUMMARY.md +++ b/book/src/SUMMARY.md @@ -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) diff --git a/book/src/lib_usage.md b/book/src/lib_usage.md new file mode 100644 index 000000000000..4a6976dbe90d --- /dev/null +++ b/book/src/lib_usage.md @@ -0,0 +1,22 @@ +# 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 + +```rust +/// A macro that prints a message if a condition is true +#[macro_export] +#[clippy::format_args] +macro_rules! print_if { + ($condition:expr, $($args:tt)+) => {{ + if $condition { + println!($($args)+) + } + }}; +} +```