-
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
Migrate write.rs
to rustc_ast::FormatArgs
#10275
Conversation
This is awesome, thanks for the implementation! The code looks reasonable, and I learnt a few things. If my understanding is correct, you essentially store every format_args invocation in a hashmap as |
Yeah, I suggested on zulip to have one internal lint pass that stores the mapping in a shared hash map. |
It's being populated by the Putting the populating part in its own internal lint would make that clearer so I'll look at doing that |
Looks good! Somewhat related -- does clippy optimize lint running if a lint is disabled? Or does it run all lints all the time, but hides output of the disabled lints? If first, all arg formatting lints would need some way to indicate they are dependent on this internal lint. |
01271d9
to
d1407e5
Compare
rust-lang/rust#106983 Currently the latter. In the future maybe also the former. Also in the future lint passes might run in parallel. This might lead to problems with the I still have to do a proper review. |
It shouldn't be too much of an issue if it does optimise that in the future because you'd have to go pretty far out of your way to disable the collector lint, it's not part of any group like Is clippy tested anywhere with |
let format_args_expr = for_each_expr(start, |expr| { | ||
let ctxt = expr.span.ctxt(); | ||
if ctxt == start.span.ctxt() { | ||
ControlFlow::Continue(Descend::Yes) | ||
} else if ctxt.outer_expn().is_descendant_of(expn_id) | ||
&& macro_backtrace(expr.span) | ||
.map(|macro_call| cx.tcx.item_name(macro_call.def_id)) | ||
.any(|name| matches!(name, sym::const_format_args | sym::format_args | sym::format_args_nl)) | ||
{ | ||
ControlFlow::Break(expr) | ||
} else { | ||
ControlFlow::Continue(Descend::No) | ||
} | ||
}); |
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.
Worth mentioning that I pulled this from the existing FormatArgsExpn::find_nested
and the very start of FormatArgsExpn::parse
:
rust-clippy/clippy_utils/src/macros.rs
Lines 989 to 1004 in 8a98609
pub fn find_nested(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, expn_id: ExpnId) -> Option<Self> { | |
for_each_expr(expr, |e| { | |
let e_ctxt = e.span.ctxt(); | |
if e_ctxt == expr.span.ctxt() { | |
ControlFlow::Continue(Descend::Yes) | |
} else if e_ctxt.outer_expn().is_descendant_of(expn_id) { | |
if let Some(args) = FormatArgsExpn::parse(cx, e) { | |
ControlFlow::Break(args) | |
} else { | |
ControlFlow::Continue(Descend::No) | |
} | |
} else { | |
ControlFlow::Continue(Descend::No) | |
} | |
}) | |
} |
rust-clippy/clippy_utils/src/macros.rs
Lines 893 to 896 in 8a98609
pub fn parse(cx: &LateContext<'_>, expr: &'tcx Expr<'tcx>) -> Option<Self> { | |
let macro_name = macro_backtrace(expr.span) | |
.map(|macro_call| cx.tcx.item_name(macro_call.def_id)) | |
.find(|&name| matches!(name, sym::const_format_args | sym::format_args | sym::format_args_nl))?; |
Friendly ping @flip1995 :) |
👋 |
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.
I don't really like the thread_local
variable to store the state, but I can't come up with a better implementation. So let's merge this to move things forward.
@bors r+ Thanks! |
Migrate `write.rs` to `rustc_ast::FormatArgs` changelog: none Part 1 of #10233 The additions to `clippy_utils` are the main novelty of this PR, there's no removals yet since other parts still rely on `FormatArgsExpn` The changes to `write.rs` itself are relatively straightforward this time around, as there's no lints in it that rely on type checking format params r? `@flip1995`
💔 Test failed - checks-action_test |
Damn proc macros |
d1407e5
to
fa0c3cc
Compare
Moved that test to |
Why is this linted now and wasn't before this refactor? |
Previously it parsed the string from the source text in order to hand it off to rust-clippy/clippy_utils/src/macros.rs Lines 416 to 424 in 56fbfe5
After the refactor it doesn't need to use println!(with_span!("" "")); currently produces
and is unchanged after this PR |
Thanks for the explanation! I'm fine with this "regression" since it is only for invalid code anyway IIUC. Let's get this merged to get it in with this sync. @bors + |
@bors r+ (forgot the |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Update Clippy r? `@Manishearth` cc `@m-ou-se` This sync also includes rust-lang/rust-clippy#10275
Update Clippy r? `@Manishearth` cc `@m-ou-se` This sync also includes rust-lang#10275
changelog: none
Part 1 of #10233
The additions to
clippy_utils
are the main novelty of this PR, there's no removals yet since other parts still rely onFormatArgsExpn
The changes to
write.rs
itself are relatively straightforward this time around, as there's no lints in it that rely on type checking format paramsr? @flip1995