-
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
Clippy breaks when we change anything about the std macros #7843
Comments
cc @rust-lang/clippy Yeah I've wanted a way to for clippy to be able to query the original expressions for a while; and ideally be able to retrieve typed info on the expressions. The really tricky thing is that some clippy lints need typed expressions from the macro invocation. The @estebank might have ideas |
@m-ou-se suggested |
I think this can largely be fixed in Clippy by using a Visitor to scan the expanded HIR of a macro and collect all expressions that are not from expansion - these are the inputs. The format string details are more tricky, but we could also use a Visitor to skip to the interesting parts and not rely on the exact expansion. Also we have access to the DefId of the macro that was called so we should be using diagnostic items to detect when entering a given macro. |
Hmmm, that sounds interesting. Still might depend on some internals, but definitely not as many |
Don't destructure args tuple in format_args! This allows Clippy to parse the HIR more simply since `arg0` is changed to `_args.0`. (cc rust-lang/rust-clippy#7843). From rustc's perspective, I think this is something between a lateral move and a tiny improvement since there are fewer bindings. r? `@m-ou-se`
Don't destructure args tuple in format_args! This allows Clippy to parse the HIR more simply since `arg0` is changed to `_args.0`. (cc rust-lang#7843). From rustc's perspective, I think this is something between a lateral move and a tiny improvement since there are fewer bindings. r? `@m-ou-se`
…illot Fix a format_args span to be expansion I found this while exploring solutions for rust-lang/rust-clippy#7843. r? `@m-ou-se`
New macro utils changelog: none Sorry, this is a big one. A lot of interrelated changes and I wanted to put the new utils to use to make sure they are somewhat battle-tested. We may want to divide some of the lint-specific refactoring commits into batches for smaller reviewing tasks. I could also split into more PRs. Introduces a bunch of new utils at `clippy_utils::macros::...`. Please read through the docs and give any feedback! I'm happy to introduce `MacroCall` and various functions to retrieve an instance. It feels like the missing puzzle piece. I'm also introducing `ExpnId` from rustc as "useful for Clippy too". `@rust-lang/clippy` Fixes #7843 by not parsing every node of macro implementations, at least the major offenders. I probably want to get rid of `is_expn_of` at some point.
Clippy recognizes macros like
panic!()
andformat_args!()
by its exact expansion, such as here:rust-clippy/clippy_utils/src/higher.rs
Lines 719 to 736 in 389a74b
And here:
rust-clippy/clippy_utils/src/higher.rs
Lines 505 to 675 in 389a74b
This is very fragile, as it needs to be changed whenever we improve/change their implementations in std/core. For example: #7723 removes a
&
, after which clippy doesn't Recognize the arguments to panic anymore.Worse, since Clippy is now not a submodule but a subtree in
rust-lang/rust
, it means that we can no longer make any changes at all to those macros without it getting blocked in CI on clippy. Clippy's dependence on all these implementations details effectively blocks development onpanic
,assert*
, andformat_args
/fmt::Arguments
inrust-lang/rust
.So as a maintainer of
std
, I'd like to ask again: please do not depend on these implementation details. We change this stuff. I'm expecting some big changes to fmt::Arguments and the assert macros in the future, and the panic macros we've also repeatedly changed tiny details of over the past year, and and they will change again.See also:
if_then_panic
doesn't fire when edition is 2021 #7723panic!
ascore::panic!
in non-built-incore
macros rust#78343 (comment)I don't know what the best solution is, but making Clippy depend heavily on every detail of our macros is just going to cause a big headache down the road for someone. We should probably work on some way in std or rustc to make it easy to for clippy to have the information without having to do these hacks to revert a macro expansion.
Maybe we can have an empty and hidden
#[inline(always)] fn macro_hint<T>(hint: &str, _: T) {}
somewhere that we can call in our macros (e.g.macro_hint("hey clippy, this is an assert_eq", (left, right))
). Then Clippy could recognize that instead.The text was updated successfully, but these errors were encountered: