-
Notifications
You must be signed in to change notification settings - Fork 13.4k
non-local-definitions
lint probably shouldn't fire in doc comments
#124534
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Are you able to provide us with a sample code demonstrating the issue? Ideally one that is self-contained. @rustbot label +E-needs-mcve |
Oops, sorry, needs This fails on the playground on beta:
@rustbot label -E-needs-mcve |
Firstly I like to note that using Secondly I think the lint is right to say that the
So if I transform your example into a regular #[test]
fn doctest() {
#[macro_export]
macro_rules! a_macro { () => { todo!() } }
} but let's imaging that there is another doctest, it could call the macro: #[test]
fn doctest2() {
a_macro!();
} meaning that the Note that currently @rustbot label -needs-triage -C-bug +C-discussion +T-compiler +T-rustdoc Footnotes
|
I'm aware, we do it CI-only to avoid new warnings cropping up in new releases or PRs.
As I noted in the OP, I'm aware these things compile into functions for test running, but I'd argue that doesn't make the lint correct - it's totally reasonable and expected to sometimes write doctests that have public functions and arguably public macros in them. If we demonstrate how the crate works with a macro_export we shouldn't get a lint warning (let alone one that "may become a hard error in the future"!). The "doctests are secretly functions" bit is an implementation detail of the fact that they're tests, rather than something inherent in typechecked docs. |
You can go around that by adding a |
Sorry didn't pick up the "doctest function"; but whenever they are in doctests or not doesn't make the lint incorrect.
I agree this is useful, but it has nothing to do with the lint it-self, it's IMO tooling issue. Thankfully, as pointed out by @GuillaumeGomez (thks), //! To use this crate you need to export a macro
//! ```
//! #[macro_export]
//! macro_rules! a_macro { () => {} }
+ //! # fn main() {}
//! ```
I would argue that because it's an implementation detail, users can't rely on it, and thus can't assume that making a |
Ah, thanks, maybe the lint in doctest contexts should print this as a suggestion as I assume some others (like me) aren't aware of this?
ISTM with the current pre-#123974 state of things the result is generally what people writing doc tests like the above are expecting, so the lint doesn't really make sense. The post-#123974 version described by @Urgau above is indeed not what people are expecting but that seems like an issue with #123974 rather than a more general one? |
If we want users to not rely on it the lint should probably be updated to indicate this to users as the behavior is incredibly surprising :). |
Indeed, this would be a good suggestion. I will implement it. @rustbot claim |
I think it should be the case, or at least before the doctest code. |
…chaelwoerister,GuillaumeGomez Adjust `#[macro_export]`/doctest help suggestion for non_local_defs lint This PR adjust the help suggestion of the `non_local_definitions` lint when encountering a `#[macro_export]` at top-level doctest. So instead of a non-sentential help suggestion to move the `macro_rules!` up above the `rustdoc`-generated function. We now suggest users to declare their own function. Fixes *(partially, needs backport)* rust-lang#124534
Rollup merge of rust-lang#124568 - Urgau:non-local-defs-doctest, r=michaelwoerister,GuillaumeGomez Adjust `#[macro_export]`/doctest help suggestion for non_local_defs lint This PR adjust the help suggestion of the `non_local_definitions` lint when encountering a `#[macro_export]` at top-level doctest. So instead of a non-sentential help suggestion to move the `macro_rules!` up above the `rustdoc`-generated function. We now suggest users to declare their own function. Fixes *(partially, needs backport)* rust-lang#124534
Uh oh!
There was an error while loading. Please reload this page.
Latest beta is generating
non-local-definitions
warnings in our doctests because we have some docs that describe#[macro_export]
s, which of course end up inside a doctest function generating the lint.cc tracking issue #120363
The text was updated successfully, but these errors were encountered: