cargo/diagnostics/rules/
redundant_homepage.rs1use std::path::Path;
2
3use cargo_util_schemas::manifest::InheritableField;
4use cargo_util_terminal::report::AnnotationKind;
5use cargo_util_terminal::report::Group;
6use cargo_util_terminal::report::Level;
7use cargo_util_terminal::report::Origin;
8use cargo_util_terminal::report::Snippet;
9use tracing::instrument;
10
11use super::STYLE;
12use crate::CargoResult;
13use crate::GlobalContext;
14use crate::core::Package;
15use crate::core::Workspace;
16use crate::diagnostics::Lint;
17use crate::diagnostics::LintLevel;
18use crate::diagnostics::LintLevelProduct;
19use crate::diagnostics::LintLevelSource;
20use crate::diagnostics::ScopedDiagnosticStats;
21use crate::diagnostics::get_key_value_span;
22use crate::diagnostics::workspace_rel_path;
23
24pub static LINT: &Lint = &Lint {
25 name: "redundant_homepage",
26 desc: "`package.homepage` is redundant with another manifest field",
27 primary_group: &STYLE,
28 msrv: Some(super::CARGO_LINTS_MSRV),
29 feature_gate: None,
30 docs: Some(
31 r#"
32### What it does
33
34Checks if the value of `package.homepage` is already covered by another field.
35
36See also [`package.homepage` reference documentation](manifest.md#the-homepage-field).
37
38### Why it is bad
39
40When package browsers render each link, a redundant link adds visual noise.
41
42### Drawbacks
43
44### Example
45
46```toml
47[package]
48name = "foo"
49homepage = "https://github.com/rust-lang/cargo/"
50repository = "https://github.com/rust-lang/cargo/"
51```
52
53Should be written as:
54
55```toml
56[package]
57name = "foo"
58repository = "https://github.com/rust-lang/cargo/"
59```
60"#,
61 ),
62};
63
64#[instrument(skip_all)]
65pub(crate) fn lint_package(
66 ws: &Workspace<'_>,
67 pkg: &Package,
68 manifest_path: &Path,
69 level: LintLevelProduct,
70 pkg_stats: &mut ScopedDiagnosticStats<'_>,
71 gctx: &GlobalContext,
72) -> CargoResult<()> {
73 let LintLevelProduct {
74 level: lint_level,
75 source,
76 } = level;
77
78 let manifest_path = workspace_rel_path(ws, manifest_path);
79
80 lint_package_inner(pkg, &manifest_path, lint_level, source, pkg_stats, gctx)
81}
82
83fn lint_package_inner(
84 pkg: &Package,
85 manifest_path: &str,
86 lint_level: LintLevel,
87 source: LintLevelSource,
88 pkg_stats: &mut ScopedDiagnosticStats<'_>,
89 gctx: &GlobalContext,
90) -> CargoResult<()> {
91 let manifest = pkg.manifest();
92
93 let Some(normalized_pkg) = &manifest.normalized_toml().package else {
94 return Ok(());
95 };
96 let Some(InheritableField::Value(homepage)) = &normalized_pkg.homepage else {
97 return Ok(());
98 };
99
100 let other_field = if let Some(InheritableField::Value(repository)) = &normalized_pkg.repository
101 && repository == homepage
102 {
103 "repository"
104 } else if let Some(InheritableField::Value(documentation)) = &normalized_pkg.documentation
105 && documentation == homepage
106 {
107 "documentation"
108 } else {
109 return Ok(());
110 };
111
112 let document = manifest.document();
113 let contents = manifest.contents();
114 let level = lint_level.to_diagnostic_level();
115 let emitted_source = LINT.emitted_source(lint_level, source);
116
117 let mut primary = Group::with_title(level.primary_title(LINT.desc));
118 if let Some(document) = document
119 && let Some(contents) = contents
120 && let Some(span) = get_key_value_span(document, &["package", "homepage"])
121 {
122 let mut snippet = Snippet::source(contents)
123 .path(manifest_path)
124 .annotation(AnnotationKind::Primary.span(span.value));
125 if let Some(span) = get_key_value_span(document, &["package", other_field]) {
126 snippet = snippet.annotation(AnnotationKind::Context.span(span.value));
127 }
128 primary = primary.element(snippet);
129 } else {
130 primary = primary.element(Origin::path(manifest_path));
131 }
132 primary = primary.element(Level::NOTE.message(emitted_source));
133 let mut report = vec![primary];
134 let help =
135 Group::with_title(Level::HELP.secondary_title("consider removing `package.homepage`"));
136 report.push(help);
137
138 pkg_stats.record_lint(lint_level);
139 gctx.shell().print_report(&report, lint_level.force())?;
140
141 Ok(())
142}