-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Doc links should count as a "use" with respect to the unused_imports lint #79542
Comments
This is tricky because the |
I think this is likely unfixable in the general case without pulling all of the doc-comment parsing and evaluation machinery into rustc and running it. That said I think you can work around this with e.g. cfg(doc) or whatever the cfg is? |
Yeah, it would require inlining the doc-comment parsing, but that'd be nice as well to get the broken_intra_doc_links lint into normal builds rather than just doc builds. Definitely appreciate that there's a ton of work in the way of doing that though! At one point IIRC there was an idea of having rustdoc just talk to rust-analyzer which seems like it could potentially unblock this as well? |
This breaks cross-crate re-exports: #75855. |
I would be very nervous of regressions doing this, especially for associated items. IIRC rust-analyzer has some trouble with associated items still. |
I would want to improve the performance a lot more before doing this by default. #78761 |
Not a solution, but note you can work around:
With the following instead: /// [`HashMap`]
///
/// [`HashMap`]: std::collections::HashMap
pub fn foo() {} Which is cleaner (rather than |
Please, no. Rustdoc's link resolution is an ad-hoc mess very remotely related to path resolution rules in the language. |
I agree with that actually - there are still some edge cases I'm hoping to change around primitives (on the theory that no one is using |
I think I would want to see at least an MCP for this, it would be quite a lot of work (and like petrochenkov mentions, affect stability guarentees) for IMO not much benefit. |
A shame this is rejected (for now), but understandable. Regarding @Lonami's suggestion: /// [`HashMap`]
///
/// [`HashMap`]: std::collections::HashMap
pub fn foo() {} Actually, this becomes a mess as soon as you start to link to the same item from multiple doc-comments, or to methods/sub-paths such as The cleaner solution often ends up being this (at risk of forgetting whether the import is actually needed): #[allow(unused)]
use std::collections::HashMap;
/// [`HashMap`]
pub fn foo() {} |
You can also use: /// This is a link to [`bar`](crate::some::module::bar).
fn foo() {} |
Compiling this will warn about the
HashMap
import being unused, but it is required for the doc link to work. It would be nice if rustc handled this like Java's compiler does, where a doc link acts as a use of an import even when not building documentation. You can work around this with[HashMap](std::collections::HashMap)
but long imports in the doc block can get a bit gross.The text was updated successfully, but these errors were encountered: