taiki-e / cargo-llvm-cov

Cargo subcommand to easily use LLVM source-based code coverage (-C instrument-coverage).
Apache License 2.0
920 stars 57 forks source link

Rust 1.80 upcoming warning: unexpected `cfg` condition name: `coverage_nightly` #370

Closed azriel91 closed 2 months ago

azriel91 commented 4 months ago

Heya, in the upcoming Rust 1.80, the following attribute will cause some cargo commands to fail (at least it causes clippy on rustc 1.80.0-nightly (7d83a4c13 2024-05-06) to fail):

#![cfg_attr(coverage_nightly, feature(coverage_attribute))]

Details are in this Rust blog post: Automatic checking of cfgs at compile-time

Error Message

The following error message appears when running clippy:

cargo clippy --workspace --fix -- -D warnings
error: unexpected `cfg` condition name: `coverage_nightly`
 --> src/lib.rs:1:13
  |
1 | #![cfg_attr(coverage_nightly, feature(coverage_attribute))]
  |             ^^^^^^^^^^^^^^^^
  |
  = help: expected names are: `clippy`, `debug_assertions`, `doc`, `docsrs`, `doctest`, `feature`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `ub_checks`, `unix`, `windows`
  = help: consider using a Cargo feature instead or adding `println!("cargo::rustc-check-cfg=cfg(coverage_nightly)");` to the top of the `build.rs`
  = note: see <https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg> for more information about checking conditional configuration
  = note: `-D unexpected-cfgs` implied by `-D warnings`
  = help: to override `-D warnings` add `#[allow(unexpected_cfgs)]`

The Fix

Add a build.rs with the following:

fn main() {
    println!("cargo::rustc-check-cfg=cfg(coverage_nightly)");
}

I think that's the right fix, as it got my build to pass.

If that's the right fix, then in cargo-llvm-cov it would be handy add it to the README.md as part of set up.

taiki-e commented 4 months ago

See https://github.com/rust-lang/rust/issues/124800 (coverage_nightly is listed in https://github.com/rust-lang/rust/issues/124800#issuecomment-2097123618). This lint will probably be allowed by default: https://github.com/rust-lang/rust/pull/124841

Urgau commented 4 months ago

Heads up, with the release of rust-lang/cargo#13913 (in nightly-2024-05-19), Cargo has now gain the ability to declare --check-cfg args directly inside the [lints] table with [lints.rust.unexpected_cfgs.check-cfg][^1]:

Cargo.toml:

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(coverage_nightly)'] }

[^1]: take effect on Rust 1.80 (current nightly), is ignored on Rust 1.79 (current beta), and produce an unused warning below

matan-starkware commented 2 months ago

This linter update seems to work for me at the package level, but not at the workspace level. Meaning if I put the line you had in every package's Cargo.toml it takes effect, but if I put it in the workspace's Cargo.toml it seems to make no difference:

[workspace.lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(coverage_nightly)'] }

Is there any way to have this configured at the workspace level?

taiki-e commented 2 months ago

See cargo docs: https://doc.rust-lang.org/cargo/reference/workspaces.html#the-lints-table

# [PROJECT_DIR]/Cargo.toml
[workspace]
members = ["crates/*"]

[workspace.lints.rust]
unsafe_code = "forbid"
# [PROJECT_DIR]/crates/bar/Cargo.toml
[package]
name = "bar"
version = "0.1.0"

[lints]
workspace = true

You have to add the last one (lints.workspace = true).