Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Incorrect import warning when only used in documentation
If a use
statement is only used during the documentation, I get an warning during cargo run
. For example, the following shell script
#! /usr/bin/env bash
set -e -u
if [ -e cargo_test ]
then
rm -r cargo_test
fi
cargo new cargo_test --bin
cd cargo_test
#
cat << EOF > src/main.rs
pub mod one;
pub mod two;
fn main() {
one::fun_1();
two::fun_2();
}
EOF
cat << EOF > src/one.rs
use crate::two;
/// See [fun_2](two::fun_2)
pub fn fun_1() {
println!( "fun_1" );
}
EOF
cat << EOF > src/two.rs
use crate::one;
/// See [fun_1](one::fun_1)
pub fn fun_2() {
println!( "fun_2" );
}
EOF
# The documentation builds fine without warning
echo 'cargo doc'
cargo doc
# The program warns that the imports are not used
echo 'cargo run'
cargo run
Generates the output
Creating binary (application) `cargo_test` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
cargo doc
Documenting cargo_test v0.1.0 (/Users/bradbell/trash/rust/cargo_test)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.23s
Generated /Users/bradbell/trash/rust/cargo_test/target/doc/cargo_test/index.html
cargo run
Compiling cargo_test v0.1.0 (/Users/bradbell/trash/rust/cargo_test)
warning: unused import: `crate::two`
--> src/one.rs:1:5
|
1 | use crate::two;
| ^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
warning: unused import: `crate::one`
--> src/two.rs:1:5
|
1 | use crate::one;
| ^^^^^^^^^^
warning: `cargo_test` (bin "cargo_test") generated 2 warnings (run `cargo fix --bin "cargo_test"` to apply 2 suggestions)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.13s
Running `target/debug/cargo_test`
fun_1
fun_2
3 posts - 2 participants
🏷️ rust_feed