Skip to content

Commit

Permalink
Auto merge of #13805 - Muscraft:im-a-teapot-lint-unstable, r=epage
Browse files Browse the repository at this point in the history
Error when unstable lints are specified but not enabled

In [#13797, it was noted that](#13797 (comment)) the `im-a-teapot` lint should always be unstable. This PR makes it so that `im-a-teapot` is unstable, and it is an error to specify it without the `test-dummy-unstable` cargo feature.

It does this by adding a `feature-gate` field to `Lint` and `LintGroup` that optionally
puts the lint/lint group behind a feature. The `feature-gate` is then checked during the new `analyze_cargo_lints_table` step that runs across all lints (and groups) specified in `[lints.cargo]` or `[workspace.lints]` if the package is inheriting its lints from a workspace.

The error looks like the following:
No inherit
![No inherit](https://github.com/rust-lang/cargo/assets/23045215/c245af87-8623-42dc-9652-be461809bb30)
Inherited
![Inhrtited](https://github.com/rust-lang/cargo/assets/23045215/5a90b7c9-0e9e-4a07-ab0e-e2e43cca8991)
  • Loading branch information
bors committed May 1, 2024
2 parents 1c92c1e + 712946c commit 6fc9e4b
Show file tree
Hide file tree
Showing 4 changed files with 373 additions and 15 deletions.
11 changes: 8 additions & 3 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ impl FromStr for Edition {
}
}

#[derive(PartialEq)]
#[derive(Debug, PartialEq)]
enum Status {
Stable,
Unstable,
Expand Down Expand Up @@ -387,11 +387,11 @@ macro_rules! features {
$(
$(#[$attr])*
#[doc = concat!("\n\n\nSee <https://doc.rust-lang.org/nightly/cargo/", $docs, ">.")]
pub fn $feature() -> &'static Feature {
pub const fn $feature() -> &'static Feature {
fn get(features: &Features) -> bool {
stab!($stab) == Status::Stable || features.$feature
}
static FEAT: Feature = Feature {
const FEAT: Feature = Feature {
name: stringify!($feature),
stability: stab!($stab),
version: $version,
Expand All @@ -406,6 +406,10 @@ macro_rules! features {
fn is_enabled(&self, features: &Features) -> bool {
(self.get)(features)
}

pub(crate) fn name(&self) -> &str {
self.name
}
}

impl Features {
Expand Down Expand Up @@ -512,6 +516,7 @@ features! {
}

/// Status and metadata for a single unstable feature.
#[derive(Debug)]
pub struct Feature {
/// Feature name. This is valid Rust identifier so no dash only underscore.
name: &'static str,
Expand Down
24 changes: 23 additions & 1 deletion src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ use crate::sources::{PathSource, CRATES_IO_INDEX, CRATES_IO_REGISTRY};
use crate::util::edit_distance;
use crate::util::errors::{CargoResult, ManifestError};
use crate::util::interning::InternedString;
use crate::util::lints::{check_im_a_teapot, check_implicit_features, unused_dependencies};
use crate::util::lints::{
analyze_cargo_lints_table, check_im_a_teapot, check_implicit_features, unused_dependencies,
};
use crate::util::toml::{read_manifest, InheritableFields};
use crate::util::{
context::CargoResolverConfig, context::CargoResolverPrecedence, context::ConfigRelativePath,
Expand Down Expand Up @@ -1227,6 +1229,26 @@ impl<'gctx> Workspace<'gctx> {
.is_some_and(|l| l.workspace)
.then(|| ws_cargo_lints);

let ws_contents = match self.root_maybe() {
MaybePackage::Package(pkg) => pkg.manifest().contents(),
MaybePackage::Virtual(v) => v.contents(),
};

let ws_document = match self.root_maybe() {
MaybePackage::Package(pkg) => pkg.manifest().document(),
MaybePackage::Virtual(v) => v.document(),
};

analyze_cargo_lints_table(
pkg,
&path,
&normalized_lints,
ws_cargo_lints,
ws_contents,
ws_document,
self.root_manifest(),
self.gctx,
)?;
check_im_a_teapot(
pkg,
&path,
Expand Down
Loading

0 comments on commit 6fc9e4b

Please sign in to comment.