I'm writing a no_std
crate and trying to find a nice way to add unit tests to my code without RA showing a bunch of errors because the tests rely in std
. I searched and found several issues about this on GitHub and with the suggestions I put together this minimal example:
#![cfg_attr(not(test), no_std)]
#![cfg_attr(not(test), no_main)]
fn get_value() -> u32 {
42
}
#[cfg_attr(not(test), panic_handler)]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn correct_value() {
assert_eq!(get_value(), 42);
}
}
In .cargo/config.toml
I have:
[build]
target = "thumbv7em-none-eabihf"
I can compile the code with cargo build
and run tests with cargo test --target x86_64-unknown-linux-gnu
. The problem is I can't figure out how to make RA aware of this setup so it doesn't show errors everywhere.
(apologies for the screenshot)
I found some suggestions online to add a .vscode/settings.json
file with these settings:
{
"rust-analyzer.check.allTargets": false,
"rust-analyzer.check.extraArgs": [
"--target",
"thumbv7em-none-eabihf",
],
}
Unfortunately this doesn't solve the errors. Is there a way to make RA check the test code with a different target than the one specified in .cargo/config.toml
?