Skip to content

Commit 9b511f0

Browse files
committed
feat: Add lint for global use of hint-mostly-unused
1 parent f99f1cc commit 9b511f0

File tree

4 files changed

+181
-9
lines changed

4 files changed

+181
-9
lines changed

src/cargo/core/workspace.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ use crate::util::context::FeatureUnification;
2525
use crate::util::edit_distance;
2626
use crate::util::errors::{CargoResult, ManifestError};
2727
use crate::util::interning::InternedString;
28-
use crate::util::lints::{analyze_cargo_lints_table, check_im_a_teapot};
28+
use crate::util::lints::{
29+
analyze_cargo_lints_table, blanket_hint_mostly_unused, check_im_a_teapot,
30+
};
2931
use crate::util::toml::{InheritableFields, read_manifest};
3032
use crate::util::{
3133
Filesystem, GlobalContext, IntoUrl, context::CargoResolverConfig, context::ConfigRelativePath,
@@ -1292,9 +1294,9 @@ impl<'gctx> Workspace<'gctx> {
12921294
}
12931295

12941296
pub fn emit_ws_lints(&self) -> CargoResult<()> {
1295-
let error_count = 0;
1297+
let mut error_count = 0;
12961298

1297-
let _cargo_lints = match self.root_maybe() {
1299+
let cargo_lints = match self.root_maybe() {
12981300
MaybePackage::Package(pkg) => {
12991301
let toml = pkg.manifest().normalized_toml();
13001302
if let Some(ws) = &toml.workspace {
@@ -1315,6 +1317,14 @@ impl<'gctx> Workspace<'gctx> {
13151317
.cloned()
13161318
.unwrap_or(manifest::TomlToolLints::default());
13171319

1320+
blanket_hint_mostly_unused(
1321+
self.root_maybe(),
1322+
self.root_manifest(),
1323+
&cargo_lints,
1324+
&mut error_count,
1325+
self.gctx,
1326+
)?;
1327+
13181328
if error_count > 0 {
13191329
Err(crate::util::errors::AlreadyPrintedError::new(anyhow!(
13201330
"encountered {error_count} errors(s) while running lints"

src/cargo/util/lints.rs

Lines changed: 112 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
use crate::core::{Edition, Feature, Features, Manifest, Package};
1+
use crate::core::{Edition, Feature, Features, Manifest, MaybePackage, Package};
22
use crate::{CargoResult, GlobalContext};
33
use annotate_snippets::{AnnotationKind, Group, Level, Snippet};
4-
use cargo_util_schemas::manifest::{TomlLintLevel, TomlToolLints};
4+
use cargo_util_schemas::manifest::{ProfilePackageSpec, TomlLintLevel, TomlToolLints};
55
use pathdiff::diff_paths;
66
use std::fmt::Display;
77
use std::ops::Range;
88
use std::path::Path;
99

1010
const LINT_GROUPS: &[LintGroup] = &[TEST_DUMMY_UNSTABLE];
11-
pub const LINTS: &[Lint] = &[IM_A_TEAPOT, UNKNOWN_LINTS];
11+
pub const LINTS: &[Lint] = &[BLANKET_HINT_MOSTLY_UNUSED, IM_A_TEAPOT, UNKNOWN_LINTS];
1212

1313
pub fn analyze_cargo_lints_table(
1414
pkg: &Package,
@@ -473,6 +473,115 @@ pub fn check_im_a_teapot(
473473
Ok(())
474474
}
475475

476+
const BLANKET_HINT_MOSTLY_UNUSED: Lint = Lint {
477+
name: "blanket_hint_mostly_unused",
478+
desc: "blanket_hint_mostly_unused lint",
479+
groups: &[],
480+
default_level: LintLevel::Warn,
481+
edition_lint_opts: None,
482+
feature_gate: None,
483+
docs: Some(
484+
r#"
485+
### What it does
486+
Checks if `hint-mostly-unused` being applied to all dependencies.
487+
488+
### Why it is bad
489+
`hint-mostly-unused` indicates that most of a crate's API surface will go
490+
unused by anything depending on it; this hint can speed up the build by
491+
attempting to minimize compilation time for items that aren't used at all.
492+
Misapplication to crates that don't fit that criteria will slow down the build
493+
rather than speeding it up. It should be selectively applied to dependencies
494+
that meet these criteria. Applying it globally is always a misapplication and
495+
will likely slow down the build.
496+
497+
### Example
498+
```toml
499+
[profile.dev.package."*"]
500+
hint-mostly-unused = true
501+
```
502+
503+
Should instead be:
504+
```toml
505+
[profile.dev.package.huge-mostly-unused-dependency]
506+
hint-mostly-unused = true
507+
```
508+
"#,
509+
),
510+
};
511+
512+
pub fn blanket_hint_mostly_unused(
513+
maybe_pkg: &MaybePackage,
514+
path: &Path,
515+
pkg_lints: &TomlToolLints,
516+
error_count: &mut usize,
517+
gctx: &GlobalContext,
518+
) -> CargoResult<()> {
519+
let (lint_level, reason) = BLANKET_HINT_MOSTLY_UNUSED.level(
520+
pkg_lints,
521+
maybe_pkg.edition(),
522+
maybe_pkg.unstable_features(),
523+
);
524+
525+
if lint_level == LintLevel::Allow {
526+
return Ok(());
527+
}
528+
529+
let level = lint_level.to_diagnostic_level();
530+
let manifest_path = rel_cwd_manifest_path(path, gctx);
531+
let mut paths = Vec::new();
532+
533+
if let Some(profiles) = maybe_pkg.profiles() {
534+
for (profile_name, top_level_profile) in &profiles.0 {
535+
if let Some(true) = top_level_profile.hint_mostly_unused {
536+
paths.push(vec!["profile", profile_name.as_str(), "hint-mostly-unused"]);
537+
}
538+
539+
if let Some(packages) = &top_level_profile.package
540+
&& let Some(profile) = packages.get(&ProfilePackageSpec::All)
541+
&& let Some(true) = profile.hint_mostly_unused
542+
{
543+
paths.push(vec![
544+
"profile",
545+
profile_name.as_str(),
546+
"package",
547+
"*",
548+
"hint-mostly-unused",
549+
]);
550+
}
551+
}
552+
}
553+
554+
for (i, path) in paths.iter().enumerate() {
555+
if lint_level.is_error() {
556+
*error_count += 1;
557+
}
558+
let title = "`hint-mostly-unused` should not be blanket applied";
559+
if let (Some(span), Some(table_span)) = (
560+
get_key_value_span(maybe_pkg.document(), &path),
561+
get_key_value_span(maybe_pkg.document(), &path[..path.len() - 1]),
562+
) {
563+
let mut group = level.clone().primary_title(title).element(
564+
Snippet::source(maybe_pkg.contents())
565+
.path(&manifest_path)
566+
.annotation(AnnotationKind::Primary.span(span.key.start..span.value.end))
567+
.annotation(AnnotationKind::Visible.span(table_span.key)),
568+
);
569+
570+
if i == 0 {
571+
group =
572+
group
573+
.element(Level::NOTE.message(
574+
BLANKET_HINT_MOSTLY_UNUSED.emitted_source(lint_level, reason),
575+
));
576+
}
577+
578+
gctx.shell().print_report(&[group], lint_level.force())?;
579+
}
580+
}
581+
582+
Ok(())
583+
}
584+
476585
const UNKNOWN_LINTS: Lint = Lint {
477586
name: "unknown_lints",
478587
desc: "unknown lint",

src/doc/src/reference/lints.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,37 @@ Note: [Cargo's linting system is unstable](unstable.md#lintscargo) and can only
55
## Warn-by-default
66

77
These lints are all set to the 'warn' level by default.
8+
- [`blanket_hint_mostly_unused`](#blanket_hint_mostly_unused)
89
- [`unknown_lints`](#unknown_lints)
910

11+
## `blanket_hint_mostly_unused`
12+
Set to `warn` by default
13+
14+
### What it does
15+
Checks if `hint-mostly-unused` being applied to all dependencies.
16+
17+
### Why it is bad
18+
`hint-mostly-unused` indicates that most of a crate's API surface will go
19+
unused by anything depending on it; this hint can speed up the build by
20+
attempting to minimize compilation time for items that aren't used at all.
21+
Misapplication to crates that don't fit that criteria will slow down the build
22+
rather than speeding it up. It should be selectively applied to dependencies
23+
that meet these criteria. Applying it globally is always a misapplication and
24+
will likely slow down the build.
25+
26+
### Example
27+
```toml
28+
[profile.dev.package."*"]
29+
hint-mostly-unused = true
30+
```
31+
32+
Should instead be:
33+
```toml
34+
[profile.dev.package.huge-mostly-unused-dependency]
35+
hint-mostly-unused = true
36+
```
37+
38+
1039
## `unknown_lints`
1140
Set to `warn` by default
1241

tests/testsuite/lints/blanket_hint_mostly_unused.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,16 @@ hint-mostly-unused = true
2222
p.cargo("check -Zprofile-hint-mostly-unused -v")
2323
.masquerade_as_nightly_cargo(&["profile-hint-mostly-unused", "cargo-lints"])
2424
.with_stderr_data(str![[r#"
25+
[WARNING] `hint-mostly-unused` should not be blanket applied
26+
--> Cargo.toml:8:1
27+
|
28+
7 | [profile.dev]
29+
8 | hint-mostly-unused = true
30+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
31+
|
32+
= [NOTE] `cargo::blanket_hint_mostly_unused` is set to `warn` by default
2533
[CHECKING] foo v0.0.1 ([ROOT]/foo)
26-
[RUNNING] `rustc --crate-name foo [..]`
34+
[RUNNING] `rustc --crate-name foo [..]
2735
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
2836
2937
"#]])
@@ -50,8 +58,16 @@ hint-mostly-unused = true
5058
p.cargo("check -Zprofile-hint-mostly-unused -v")
5159
.masquerade_as_nightly_cargo(&["profile-hint-mostly-unused", "cargo-lints"])
5260
.with_stderr_data(str![[r#"
61+
[WARNING] `hint-mostly-unused` should not be blanket applied
62+
--> Cargo.toml:8:1
63+
|
64+
7 | [profile.dev.package."*"]
65+
8 | hint-mostly-unused = true
66+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
67+
|
68+
= [NOTE] `cargo::blanket_hint_mostly_unused` is set to `warn` by default
5369
[CHECKING] foo v0.0.1 ([ROOT]/foo)
54-
[RUNNING] `rustc --crate-name foo [..]`
70+
[RUNNING] `rustc --crate-name foo [..]
5571
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
5672
5773
"#]])
@@ -87,8 +103,16 @@ authors = []
87103
p.cargo("check -Zprofile-hint-mostly-unused -v")
88104
.masquerade_as_nightly_cargo(&["profile-hint-mostly-unused", "cargo-lints"])
89105
.with_stderr_data(str![[r#"
106+
[WARNING] `hint-mostly-unused` should not be blanket applied
107+
--> Cargo.toml:6:1
108+
|
109+
5 | [profile.dev.package."*"]
110+
6 | hint-mostly-unused = true
111+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
112+
|
113+
= [NOTE] `cargo::blanket_hint_mostly_unused` is set to `warn` by default
90114
[CHECKING] foo v0.0.1 ([ROOT]/foo/foo)
91-
[RUNNING] `rustc --crate-name foo [..]`
115+
[RUNNING] `rustc --crate-name foo [..]
92116
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
93117
94118
"#]])

0 commit comments

Comments
 (0)