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