-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support user format-like macros #9948
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Attributes for Crate Authors | ||
|
||
In some cases it is possible to extend Clippy coverage to 3rd party libraries. | ||
To do this, Clippy provides attributes that can be applied to items in the 3rd party crate. | ||
|
||
## `#[clippy::format_args]` | ||
|
||
_Available since Clippy v1.84_ | ||
|
||
This attribute can be added to a macro that supports `format!`, `println!`, or similar 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)+) | ||
} | ||
}}; | ||
} | ||
``` | ||
|
||
## `#[clippy::has_significant_drop]` | ||
|
||
_Available since Clippy v1.60_ | ||
|
||
The `clippy::has_significant_drop` attribute can be added to types whose Drop impls have an important side effect, | ||
such as unlocking a mutex, making it important for users to be able to accurately understand their lifetimes. | ||
When a temporary is returned in a function call in a match scrutinee, its lifetime lasts until the end of the match | ||
block, which may be surprising. | ||
|
||
### Example | ||
|
||
```rust | ||
#[clippy::has_significant_drop] | ||
struct CounterWrapper<'a> { | ||
counter: &'a Counter, | ||
} | ||
|
||
impl<'a> Drop for CounterWrapper<'a> { | ||
fn drop(&mut self) { | ||
self.counter.i.fetch_sub(1, Ordering::Relaxed); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
#![allow(clippy::similar_names)] // `expr` and `expn` | ||
|
||
use crate::get_unique_attr; | ||
use crate::visitors::{Descend, for_each_expr_without_closures}; | ||
|
||
use arrayvec::ArrayVec; | ||
use rustc_ast::{FormatArgs, FormatArgument, FormatPlaceholder}; | ||
use rustc_data_structures::fx::FxHashMap; | ||
use rustc_data_structures::sync::{Lrc, OnceLock}; | ||
use rustc_hir::{self as hir, Expr, ExprKind, HirId, Node, QPath}; | ||
use rustc_lint::LateContext; | ||
use rustc_lint::{LateContext, LintContext}; | ||
use rustc_span::def_id::DefId; | ||
use rustc_span::hygiene::{self, MacroKind, SyntaxContext}; | ||
use rustc_span::{BytePos, ExpnData, ExpnId, ExpnKind, Span, SpanData, Symbol, sym}; | ||
|
@@ -36,7 +37,9 @@ pub fn is_format_macro(cx: &LateContext<'_>, macro_def_id: DefId) -> bool { | |
if let Some(name) = cx.tcx.get_diagnostic_name(macro_def_id) { | ||
FORMAT_MACRO_DIAG_ITEMS.contains(&name) | ||
} else { | ||
false | ||
// Allow users to tag any macro as being format!-like | ||
// TODO: consider deleting FORMAT_MACRO_DIAG_ITEMS and using just this method | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a follow up PR to the rust repo, after this PR is merged and synced |
||
get_unique_attr(cx.sess(), cx.tcx.get_attrs_unchecked(macro_def_id), "format_args").is_some() | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are lints that apply to
format!
that wouldn't apply to macros marked with the attr, might want to list it asformat!
/println!
/etc or similarThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
e.g.
format_in_format_args
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Alexendoo thx, updated