Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 23 additions & 55 deletions src/cargo/lints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@ pub enum ManifestFor<'a> {

impl ManifestFor<'_> {
fn lint_level(&self, pkg_lints: &TomlToolLints, lint: &Lint) -> (LintLevel, LintLevelReason) {
lint.level(
pkg_lints,
self.rust_version(),
self.edition(),
self.unstable_features(),
)
lint.level(pkg_lints, self.rust_version(), self.unstable_features())
}

pub fn rust_version(&self) -> Option<&RustVersion> {
Expand Down Expand Up @@ -111,20 +106,12 @@ pub fn analyze_cargo_lints_table(
let manifest_path = rel_cwd_manifest_path(manifest_path, gctx);
let mut unknown_lints = Vec::new();
for lint_name in cargo_lints.keys().map(|name| name) {
let Some((name, default_level, edition_lint_opts, feature_gate)) =
find_lint_or_group(lint_name)
else {
let Some((name, default_level, feature_gate)) = find_lint_or_group(lint_name) else {
unknown_lints.push(lint_name);
continue;
};

let (_, reason, _) = level_priority(
name,
*default_level,
*edition_lint_opts,
cargo_lints,
manifest.edition(),
);
let (_, reason, _) = level_priority(name, *default_level, cargo_lints);

// Only run analysis on user-specified lints
if !reason.is_user_specified() {
Expand Down Expand Up @@ -160,21 +147,15 @@ pub fn analyze_cargo_lints_table(

fn find_lint_or_group<'a>(
name: &str,
) -> Option<(
&'static str,
&LintLevel,
&Option<(Edition, LintLevel)>,
&Option<&'static Feature>,
)> {
) -> Option<(&'static str, &LintLevel, &Option<&'static Feature>)> {
if let Some(lint) = LINTS.iter().find(|l| l.name == name) {
Some((
lint.name,
&lint.primary_group.default_level,
&lint.edition_lint_opts,
&lint.feature_gate,
))
} else if let Some(group) = LINT_GROUPS.iter().find(|g| g.name == name) {
Some((group.name, &group.default_level, &None, &group.feature_gate))
Some((group.name, &group.default_level, &group.feature_gate))
} else {
None
}
Expand Down Expand Up @@ -423,7 +404,6 @@ pub struct Lint {
/// linting system, then at earliest an MSRV of 1.78 is required as `[lints.cargo]` was a hard
/// error before then.
pub msrv: Option<RustVersion>,
pub edition_lint_opts: Option<(Edition, LintLevel)>,
pub feature_gate: Option<&'static Feature>,
/// This is a markdown formatted string that will be used when generating
/// the lint documentation. If docs is `None`, the lint will not be
Expand All @@ -436,7 +416,6 @@ impl Lint {
&self,
pkg_lints: &TomlToolLints,
pkg_rust_version: Option<&RustVersion>,
edition: Edition,
unstable_features: &Features,
) -> (LintLevel, LintLevelReason) {
// We should return `Allow` if a lint is behind a feature, but it is
Expand All @@ -455,20 +434,13 @@ impl Lint {
}
}

let lint_level_priority = level_priority(
self.name,
self.primary_group.default_level,
self.edition_lint_opts,
pkg_lints,
edition,
);
let lint_level_priority =
level_priority(self.name, self.primary_group.default_level, pkg_lints);

let group_level_priority = level_priority(
self.primary_group.name,
self.primary_group.default_level,
None,
pkg_lints,
edition,
);

let (_, (l, r, _)) = max_by_key(
Expand Down Expand Up @@ -541,15 +513,13 @@ impl From<TomlLintLevel> for LintLevel {
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum LintLevelReason {
Default,
Edition(Edition),
Package,
}

impl Display for LintLevelReason {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LintLevelReason::Default => write!(f, "by default"),
LintLevelReason::Edition(edition) => write!(f, "in edition {}", edition),
LintLevelReason::Package => write!(f, "in `[lints]`"),
}
}
Expand All @@ -559,7 +529,6 @@ impl LintLevelReason {
fn is_user_specified(&self) -> bool {
match self {
LintLevelReason::Default => false,
LintLevelReason::Edition(_) => false,
LintLevelReason::Package => true,
}
}
Expand All @@ -568,32 +537,16 @@ impl LintLevelReason {
fn level_priority(
name: &str,
default_level: LintLevel,
edition_lint_opts: Option<(Edition, LintLevel)>,
pkg_lints: &TomlToolLints,
edition: Edition,
) -> (LintLevel, LintLevelReason, i8) {
let (unspecified_level, reason) = if let Some(level) = edition_lint_opts
.filter(|(e, _)| edition >= *e)
.map(|(_, l)| l)
{
(level, LintLevelReason::Edition(edition))
} else {
(default_level, LintLevelReason::Default)
};

// Don't allow the group to be overridden if the level is `Forbid`
if unspecified_level == LintLevel::Forbid {
return (unspecified_level, reason, 0);
}

if let Some(defined_level) = pkg_lints.get(name) {
(
defined_level.level().into(),
LintLevelReason::Package,
defined_level.priority(),
)
} else {
(unspecified_level, reason, 0)
(default_level, LintLevelReason::Default, 0)
}
}

Expand All @@ -603,6 +556,21 @@ mod tests {
use snapbox::ToDebug;
use std::collections::HashSet;

#[test]
fn ensure_lint_groups_do_not_default_to_forbid() {
let forbid_groups = super::LINT_GROUPS
.iter()
.filter(|g| matches!(g.default_level, super::LintLevel::Forbid))
.collect::<Vec<_>>();

assert!(
forbid_groups.is_empty(),
"\n`LintGroup`s should never default to `forbid`, but the following do:\n\
{}\n",
forbid_groups.iter().map(|g| g.name).join("\n")
);
}

#[test]
fn ensure_sorted_lints() {
// This will be printed out if the fields are not sorted.
Expand Down
2 changes: 0 additions & 2 deletions src/cargo/lints/rules/blanket_hint_mostly_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub static LINT: &Lint = &Lint {
desc: "blanket_hint_mostly_unused lint",
primary_group: &SUSPICIOUS,
msrv: Some(super::CARGO_LINTS_MSRV),
edition_lint_opts: None,
feature_gate: None,
docs: Some(
r#"
Expand Down Expand Up @@ -66,7 +65,6 @@ pub fn blanket_hint_mostly_unused(
let (lint_level, reason) = LINT.level(
pkg_lints,
ws.lowest_rust_version(),
maybe_pkg.edition(),
maybe_pkg.unstable_features(),
);

Expand Down
9 changes: 2 additions & 7 deletions src/cargo/lints/rules/im_a_teapot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub static LINT: &Lint = &Lint {
desc: "`im_a_teapot` is specified",
primary_group: &TEST_DUMMY_UNSTABLE,
msrv: None,
edition_lint_opts: None,
feature_gate: Some(Feature::test_dummy_unstable()),
docs: None,
};
Expand All @@ -36,12 +35,8 @@ pub fn check_im_a_teapot(
gctx: &GlobalContext,
) -> CargoResult<()> {
let manifest = pkg.manifest();
let (lint_level, reason) = LINT.level(
pkg_lints,
pkg.rust_version(),
manifest.edition(),
manifest.unstable_features(),
);
let (lint_level, reason) =
LINT.level(pkg_lints, pkg.rust_version(), manifest.unstable_features());

if lint_level == LintLevel::Allow {
return Ok(());
Expand Down
3 changes: 0 additions & 3 deletions src/cargo/lints/rules/implicit_minimum_version_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub static LINT: &Lint = &Lint {
desc: "dependency version requirement without an explicit minimum version",
primary_group: &PEDANTIC,
msrv: None,
edition_lint_opts: None,
feature_gate: None,
docs: Some(
r#"
Expand Down Expand Up @@ -93,7 +92,6 @@ pub fn implicit_minimum_version_req_pkg(
let (lint_level, reason) = LINT.level(
cargo_lints,
pkg.rust_version(),
pkg.manifest().edition(),
pkg.manifest().unstable_features(),
);

Expand Down Expand Up @@ -161,7 +159,6 @@ pub fn implicit_minimum_version_req_ws(
let (lint_level, reason) = LINT.level(
cargo_lints,
ws.lowest_rust_version(),
maybe_pkg.edition(),
maybe_pkg.unstable_features(),
);

Expand Down
2 changes: 0 additions & 2 deletions src/cargo/lints/rules/missing_lints_inheritance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub static LINT: &Lint = &Lint {
desc: "missing `[lints]` to inherit `[workspace.lints]`",
primary_group: &SUSPICIOUS,
msrv: Some(super::CARGO_LINTS_MSRV),
edition_lint_opts: None,
feature_gate: None,
docs: Some(
r#"
Expand Down Expand Up @@ -64,7 +63,6 @@ pub fn missing_lints_inheritance(
let (lint_level, reason) = LINT.level(
cargo_lints,
pkg.rust_version(),
pkg.manifest().edition(),
pkg.manifest().unstable_features(),
);

Expand Down
2 changes: 0 additions & 2 deletions src/cargo/lints/rules/non_kebab_case_bins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub static LINT: &Lint = &Lint {
desc: "binaries should have a kebab-case name",
primary_group: &STYLE,
msrv: Some(super::CARGO_LINTS_MSRV),
edition_lint_opts: None,
feature_gate: None,
docs: Some(
r#"
Expand Down Expand Up @@ -73,7 +72,6 @@ pub fn non_kebab_case_bins(
let (lint_level, reason) = LINT.level(
cargo_lints,
pkg.rust_version(),
pkg.manifest().edition(),
pkg.manifest().unstable_features(),
);

Expand Down
2 changes: 0 additions & 2 deletions src/cargo/lints/rules/non_kebab_case_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub static LINT: &Lint = &Lint {
desc: "features should have a kebab-case name",
primary_group: &RESTRICTION,
msrv: None,
edition_lint_opts: None,
feature_gate: None,
docs: Some(
r#"
Expand Down Expand Up @@ -66,7 +65,6 @@ pub fn non_kebab_case_features(
let (lint_level, reason) = LINT.level(
cargo_lints,
pkg.rust_version(),
pkg.manifest().edition(),
pkg.manifest().unstable_features(),
);

Expand Down
2 changes: 0 additions & 2 deletions src/cargo/lints/rules/non_kebab_case_packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub static LINT: &Lint = &Lint {
desc: "packages should have a kebab-case name",
primary_group: &RESTRICTION,
msrv: None,
edition_lint_opts: None,
feature_gate: None,
docs: Some(
r#"
Expand Down Expand Up @@ -66,7 +65,6 @@ pub fn non_kebab_case_packages(
let (lint_level, reason) = LINT.level(
cargo_lints,
pkg.rust_version(),
pkg.manifest().edition(),
pkg.manifest().unstable_features(),
);

Expand Down
2 changes: 0 additions & 2 deletions src/cargo/lints/rules/non_snake_case_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub static LINT: &Lint = &Lint {
desc: "features should have a snake-case name",
primary_group: &RESTRICTION,
msrv: None,
edition_lint_opts: None,
feature_gate: None,
docs: Some(
r#"
Expand Down Expand Up @@ -66,7 +65,6 @@ pub fn non_snake_case_features(
let (lint_level, reason) = LINT.level(
cargo_lints,
pkg.rust_version(),
pkg.manifest().edition(),
pkg.manifest().unstable_features(),
);

Expand Down
2 changes: 0 additions & 2 deletions src/cargo/lints/rules/non_snake_case_packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub static LINT: &Lint = &Lint {
desc: "packages should have a snake-case name",
primary_group: &RESTRICTION,
msrv: None,
edition_lint_opts: None,
feature_gate: None,
docs: Some(
r#"
Expand Down Expand Up @@ -66,7 +65,6 @@ pub fn non_snake_case_packages(
let (lint_level, reason) = LINT.level(
cargo_lints,
pkg.rust_version(),
pkg.manifest().edition(),
pkg.manifest().unstable_features(),
);

Expand Down
2 changes: 0 additions & 2 deletions src/cargo/lints/rules/redundant_homepage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub static LINT: &Lint = &Lint {
desc: "`package.homepage` is redundant with another manifest field",
primary_group: &STYLE,
msrv: Some(super::CARGO_LINTS_MSRV),
edition_lint_opts: None,
feature_gate: None,
docs: Some(
r#"
Expand Down Expand Up @@ -70,7 +69,6 @@ pub fn redundant_homepage(
let (lint_level, reason) = LINT.level(
cargo_lints,
pkg.rust_version(),
pkg.manifest().edition(),
pkg.manifest().unstable_features(),
);

Expand Down
2 changes: 0 additions & 2 deletions src/cargo/lints/rules/redundant_readme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ pub static LINT: &Lint = &Lint {
desc: "explicit `package.readme` can be inferred",
primary_group: &STYLE,
msrv: Some(super::CARGO_LINTS_MSRV),
edition_lint_opts: None,
feature_gate: None,
docs: Some(
r#"
Expand Down Expand Up @@ -72,7 +71,6 @@ pub fn redundant_readme(
let (lint_level, reason) = LINT.level(
cargo_lints,
pkg.rust_version(),
pkg.manifest().edition(),
pkg.manifest().unstable_features(),
);

Expand Down
1 change: 0 additions & 1 deletion src/cargo/lints/rules/unknown_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub static LINT: &Lint = &Lint {
desc: "unknown lint",
primary_group: &SUSPICIOUS,
msrv: Some(super::CARGO_LINTS_MSRV),
edition_lint_opts: None,
feature_gate: None,
docs: Some(
r#"
Expand Down
2 changes: 0 additions & 2 deletions src/cargo/lints/rules/unused_workspace_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub static LINT: &Lint = &Lint {
desc: "unused workspace dependency",
primary_group: &SUSPICIOUS,
msrv: Some(super::CARGO_LINTS_MSRV),
edition_lint_opts: None,
feature_gate: None,
docs: Some(
r#"
Expand Down Expand Up @@ -57,7 +56,6 @@ pub fn unused_workspace_dependencies(
let (lint_level, reason) = LINT.level(
cargo_lints,
ws.lowest_rust_version(),
maybe_pkg.edition(),
maybe_pkg.unstable_features(),
);
if lint_level == LintLevel::Allow {
Expand Down
Loading