As cargo check
shows, it's often useful to check if your program is well-formed without actually generating code (an often pretty time-consuming task). I want to check a single (library) Rust file with rustc
directly (I cannot use Cargo!).
cargo check
apparently works by calling this:
rustc --emit=metadata -Z no-codegen
This only emits metadata, a .rmeta
file. Cargo actually needs that to check crates dependent on the checked crate. In my case I really don't need the metadata file.
I tried the following:
rustc --crate-type=lib --emit=
rustc --crate-type=lib --emit=nothing
But both didn't work. I use --crate-type=lib
because my file doesn't have a main
function. I need a platform-independent solution (I don't just want to use it on my machine, but use it in a public script).
How do I make rustc
not write a single file?
You can just skip the --emit
flag.
The final command would then be: rustc -Z no-codegen rust.rs
To quote my own GitHub comment about this very question, there are a few options for stable Rust:
rustc --emit=mir -o /dev/null
seems to work in 1.18 and newer, writing nothing. (--emit=mir
is the only helpful --emit
option—the others try to create silly files like /dev/null.foo0.rcgu.o
, except --emit=dep-info
, which does no checking.)rustc -C extra-filename=-tmp -C linker=true
(i.e. use /bin/true
as a “linker”) seems to work in all versions, writing some intermediate files but cleaning them up.rustc --out-dir=<new empty temporary directory>
is less clever and therefore perhaps less likely to break?Note that linker errors, if any, will not be found by the first two options (nor by the nightly-only -Zno-codegen
option).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With