links show as unused imports

795 views Asked by At

Is there a way to include something into documentation scope without including it into the code scope? My Rustdoc links to a Trait that's not in the code scope. If I add use std::ops::Add at the top, I get unused import warning from cargo build. If I don't add it, I get unresolved link warning from cargo doc.

In theory, cargo build should not warn me because rustdoc is in the same scope as the main code, but it does.

/// [Add]
pub fn foo() {}
2

There are 2 answers

0
Silvio Mayolo On

You can fully qualify the link.

/// [Add](std::ops::Add)

Note that the syntax you've been using is shorthand for

/// [Add](Add)

and the thing in parentheses at the end can be any valid qualified name, including fully qualified ones starting at std or the name of some other crate.

0
Chayim Friedman On

You can use #[cfg(doc)]:

#[cfg(doc)]
use std::ops::Add;

/// [Add]
pub fn foo() {}

Or just use the full name, as suggested by @SilvioMayolo.