Skip to content

Commit 30a7eb5

Browse files
committed
chore(lints): Add documentation to public lints
1 parent a72ce7d commit 30a7eb5

File tree

1 file changed

+104
-13
lines changed

1 file changed

+104
-13
lines changed

Diff for: src/cargo/util/lints.rs

+104-13
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ pub struct LintGroup {
256256
pub feature_gate: Option<&'static Feature>,
257257
}
258258

259+
/// This lint group is only to be used for testing purposes
259260
const TEST_DUMMY_UNSTABLE: LintGroup = LintGroup {
260261
name: "test_dummy_unstable",
261262
desc: "test_dummy_unstable is meant to only be used in tests",
@@ -272,6 +273,10 @@ pub struct Lint {
272273
pub default_level: LintLevel,
273274
pub edition_lint_opts: Option<(Edition, LintLevel)>,
274275
pub feature_gate: Option<&'static Feature>,
276+
/// This is a markdown formatted string that will be used when generating
277+
/// the lint documentation. If docs is `None`, the lint will not be
278+
/// documented.
279+
pub docs: Option<&'static str>,
275280
}
276281

277282
impl Lint {
@@ -420,13 +425,15 @@ fn level_priority(
420425
}
421426
}
422427

428+
/// This lint is only to be used for testing purposes
423429
const IM_A_TEAPOT: Lint = Lint {
424430
name: "im_a_teapot",
425431
desc: "`im_a_teapot` is specified",
426432
groups: &[TEST_DUMMY_UNSTABLE],
427433
default_level: LintLevel::Allow,
428434
edition_lint_opts: None,
429435
feature_gate: Some(Feature::test_dummy_unstable()),
436+
docs: None,
430437
};
431438

432439
pub fn check_im_a_teapot(
@@ -476,26 +483,55 @@ pub fn check_im_a_teapot(
476483
Ok(())
477484
}
478485

479-
/// By default, cargo will treat any optional dependency as a [feature]. As of
480-
/// cargo 1.60, these can be disabled by declaring a feature that activates the
481-
/// optional dependency as `dep:<name>` (see [RFC #3143]).
482-
///
483-
/// In the 2024 edition, `cargo` will stop exposing optional dependencies as
484-
/// features implicitly, requiring users to add `foo = ["dep:foo"]` if they
485-
/// still want it exposed.
486-
///
487-
/// For more information, see [RFC #3491]
488-
///
489-
/// [feature]: https://doc.rust-lang.org/cargo/reference/features.html
490-
/// [RFC #3143]: https://rust-lang.github.io/rfcs/3143-cargo-weak-namespaced-features.html
491-
/// [RFC #3491]: https://rust-lang.github.io/rfcs/3491-remove-implicit-features.html
492486
const IMPLICIT_FEATURES: Lint = Lint {
493487
name: "implicit_features",
494488
desc: "implicit features for optional dependencies is deprecated and will be unavailable in the 2024 edition",
495489
groups: &[],
496490
default_level: LintLevel::Allow,
497491
edition_lint_opts: None,
498492
feature_gate: None,
493+
docs: Some(r#"
494+
### What it does
495+
Checks for implicit features for optional dependencies
496+
497+
### Why it is bad
498+
By default, cargo will treat any optional dependency as a [feature]. As of
499+
cargo 1.60, these can be disabled by declaring a feature that activates the
500+
optional dependency as `dep:<name>` (see [RFC #3143]).
501+
502+
In the 2024 edition, `cargo` will stop exposing optional dependencies as
503+
features implicitly, requiring users to add `foo = ["dep:foo"]` if they
504+
still want it exposed.
505+
506+
For more information, see [RFC #3491]
507+
508+
### Example
509+
```toml
510+
edition = "2021"
511+
512+
[dependencies]
513+
bar = { version = "0.1.0", optional = true }
514+
515+
[features]
516+
# No explicit feature activation for `bar`
517+
```
518+
519+
Instead, the dependency should have an explicit feature:
520+
```toml
521+
edition = "2021"
522+
523+
[dependencies]
524+
bar = { version = "0.1.0", optional = true }
525+
526+
[features]
527+
bar = ["dep:bar"]
528+
```
529+
530+
[feature]: https://doc.rust-lang.org/cargo/reference/features.html
531+
[RFC #3143]: https://rust-lang.github.io/rfcs/3143-cargo-weak-namespaced-features.html
532+
[RFC #3491]: https://rust-lang.github.io/rfcs/3491-remove-implicit-features.html
533+
"#
534+
),
499535
};
500536

501537
pub fn check_implicit_features(
@@ -575,6 +611,24 @@ const UNKNOWN_LINTS: Lint = Lint {
575611
default_level: LintLevel::Warn,
576612
edition_lint_opts: None,
577613
feature_gate: None,
614+
docs: Some(
615+
r#"
616+
### What it does
617+
Checks for unknown lints in the `[lints.cargo]` table
618+
619+
### Why it is bad
620+
- The lint name could be misspelled, leading to confusion as to why it is
621+
not working as expected
622+
- The unknown lint could end up causing an error if `cargo` decides to make
623+
a lint with the same name in the future
624+
625+
### Example
626+
```toml
627+
[lints.cargo]
628+
this-lint-does-not-exist = "warn"
629+
```
630+
"#,
631+
),
578632
};
579633

580634
fn output_unknown_lints(
@@ -684,6 +738,43 @@ const UNUSED_OPTIONAL_DEPENDENCY: Lint = Lint {
684738
default_level: LintLevel::Warn,
685739
edition_lint_opts: None,
686740
feature_gate: None,
741+
docs: Some(
742+
r#"
743+
### What it does
744+
Checks for optional dependencies that are not activated by any feature
745+
746+
### Why it is bad
747+
Starting in the 2024 edition, `cargo` no longer implicitly creates features
748+
for optional dependencies (see [RFC #3491]). This means that any optional
749+
dependency not specified with `"dep:<name>"` in some feature is now unused.
750+
This change may be surprising to users who have been using the implicit
751+
features `cargo` has been creating for optional dependencies.
752+
753+
### Example
754+
```toml
755+
edition = "2024"
756+
757+
[dependencies]
758+
bar = { version = "0.1.0", optional = true }
759+
760+
[features]
761+
# No explicit feature activation for `bar`
762+
```
763+
764+
Instead, the dependency should be removed or activated in a feature:
765+
```toml
766+
edition = "2024"
767+
768+
[dependencies]
769+
bar = { version = "0.1.0", optional = true }
770+
771+
[features]
772+
bar = ["dep:bar"]
773+
```
774+
775+
[RFC #3491]: https://rust-lang.github.io/rfcs/3491-remove-implicit-features.html
776+
"#,
777+
),
687778
};
688779

689780
pub fn unused_dependencies(

0 commit comments

Comments
 (0)