-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add relative_path_in_macro_definition lint (#14472) #14645 #14648
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
base: master
Are you sure you want to change the base?
Conversation
Implement lint to warn about relative `core`/`kernel` paths in macro definitions. Signed-off-by: Kunwu Chan <[email protected]> Co-developed-by: Grace Deng <[email protected]> Signed-off-by: Grace Deng <[email protected]>
Create `tests/ui/relative_path_in_macro_definition.rs` with positive and negative cases to verify lint behavior. Signed-off-by: Kunwu Chan <[email protected]> Co-developed-by: Grace Deng <[email protected]> Signed-off-by: Grace Deng <[email protected]>
Add `#![allow(clippy::relative_path_in_macro_definition)]` to `incompatible_msrv.rs`, `swap_ptr_to_ref_unfixable.rs`, and `arithmetic_side_effects.rs` to avoid lint interference. Update corresponding `.stderr` files. Signed-off-by: Kunwu Chan <[email protected]> Co-developed-by: Grace Deng <[email protected]> Signed-off-by: Grace Deng <[email protected]>
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.
This is a good starting point. I'd like the message to be more succinct and relate to the current situation though.
match tree { | ||
TokenTree::Token(token, _) => { | ||
if let TokenKind::Ident(ident, _) = token.kind | ||
&& (ident == sym::core || ident.as_str() == "kernel") |
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.
We no longer need ident.as_str()
. Look at clippy_utils::sym
.
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.
Thank you for your feedback and suggestion to avoid ident.as_str() and refer to clippy_utils::sym. I've encountered a compilation error with the current implementation:
error[E0425]: cannot find value kernel
in module sym
--> clippy_lints/src/relative_path_in_macro_definition.rs:57:61
|
57 | && (ident == sym::core || ident == sym::kernel)
| ^^^^^^ not found in sym
The issue arises because rustc_span::symbol::sym does not define a kernel symbol, unlike core. Upon reviewing clippy_utils::sym, I found it to be a macro that wraps Symbol::intern, used for creating symbols from strings, rather than providing predefined symbols like core.
To address this, I propose using Symbol::intern("kernel") to create the kernel symbol dynamically, allowing direct comparison with ident (of type rustc_span::Ident). The updated code snippet is:
if ident == sym::core || ident == Symbol::intern("kernel") {
// ... rest of the lint logic ...
}
This approach avoids ident.as_str() as you recommended, using direct symbol comparisons for efficiency. I've tested this change locally, and it resolves the compilation error while maintaining the lint's functionality to check for relative paths to core and kernel in macro definitions.
Could you please confirm if using Symbol::intern("kernel") is acceptable for this lint, or if there's a preferred method within Clippy's codebase (e.g., using clippy_utils::sym! macro or another approach)? Thank you for your guidance!
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.
Yes, you can simply add "kernel" into that generate!
macro call, then you can use clippy_utils::sym::kernel in your code.
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.
Yes, you can simply add "kernel" into that
generate!
macro call, then you can use clippy_utils::sym::kernel in your code.
It seems that the definition of "sym::core" is in
https://github.com/rust-lang/rust/blob/master/compiler/rustc_span/src/symbol.rs.
But according to the discussion below, we no longer need to add the extra check. Anyway, thanks for the reply.
if let TokenTree::Token(prev_token, _) = prev { | ||
prev_token.kind == TokenKind::PathSep | ||
} else { | ||
false | ||
} |
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.
if let TokenTree::Token(prev_token, _) = prev { | |
prev_token.kind == TokenKind::PathSep | |
} else { | |
false | |
} | |
matches!(prev, TokenTree::Token(Token { kind: TokenKind::PathSep, .. }, _)) |
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.
thx, I'll change it in v2.
cx, | ||
RELATIVE_PATH_IN_MACRO_DEFINITION, | ||
token.span, | ||
"relative path to `core` or `kernel` used in macro definition", |
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.
Here we should reuse the first element of the path instead of writing both options.
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.
thx, I'll change it in v2.
☔ The latest upstream changes (possibly 7bac114) made this pull request unmergeable. Please resolve the merge conflicts. |
Sorry to intrude, but can I ask why |
The source is here: |
Thank you, @KunWuChan. It would be nice if eventually the lint could be extended as @ojeda wrote in Rust-for-Linux/linux#1150 (comment):
I think many besides kernel developers would benefit from a lint like that. |
I agree that there is nothing in the issue about the lint that should restrict which paths must be relative. And So let's simply remove that additional check and make the lint more general. |
Yeah, we expected the lint to be general, rather than just Thanks for this! |
Sure, I'll try to do it as the suggestions.
thx, I'll remove the additional check and make the lint more general. |
Okay, I'll remove the additional check and add some testcases. |
Add relative_path_in_macro_definition lint (#14472) #14645
Implements the relative_path_in_macro_definition lint to warn about relative paths to core or kernel in macro definitions, as requested in #14472.
Includes tests in tests/ui/relative_path_in_macro_definition.rs and updates existing tests (incompatible_msrv.rs, swap_ptr_to_ref_unfixable.rs, arithmetic_side_effects.rs) with #![allow] to avoid interference.
Fixes New lint: relative_path_in_macro_definition #14472
Tests pass: cargo test, cargo test --test compile-test, cargo test --test integration
cargo fmt clean
changelog: add 'relative_path_in_macro_definition' lint.
Signed-off-by: Kunwu Chan [email protected]
Co-developed-by: Grace Deng [email protected]
Signed-off-by: Grace Deng [email protected]