Skip to main content

cargo/diagnostics/rules/
redundant_homepage.rs

1use std::path::Path;
2
3use cargo_util_schemas::manifest::InheritableField;
4use cargo_util_schemas::manifest::TomlToolLints;
5use cargo_util_terminal::report::AnnotationKind;
6use cargo_util_terminal::report::Group;
7use cargo_util_terminal::report::Level;
8use cargo_util_terminal::report::Origin;
9use cargo_util_terminal::report::Patch;
10use cargo_util_terminal::report::Snippet;
11use tracing::instrument;
12
13use super::STYLE;
14use crate::CargoResult;
15use crate::GlobalContext;
16use crate::core::Package;
17use crate::diagnostics::DiagnosticStats;
18use crate::diagnostics::Lint;
19use crate::diagnostics::LintLevel;
20use crate::diagnostics::LintLevelSource;
21use crate::diagnostics::get_key_value_span;
22use crate::diagnostics::rel_cwd_manifest_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 fn redundant_homepage(
66    pkg: &Package,
67    manifest_path: &Path,
68    cargo_lints: &TomlToolLints,
69    stats: &mut DiagnosticStats,
70    gctx: &GlobalContext,
71) -> CargoResult<()> {
72    let (lint_level, source) = LINT.level(
73        cargo_lints,
74        pkg.rust_version(),
75        pkg.manifest().unstable_features(),
76    );
77
78    if lint_level == LintLevel::Allow {
79        return Ok(());
80    }
81
82    let manifest_path = rel_cwd_manifest_path(manifest_path, gctx);
83
84    lint_package(pkg, &manifest_path, lint_level, source, stats, gctx)
85}
86
87fn lint_package(
88    pkg: &Package,
89    manifest_path: &str,
90    lint_level: LintLevel,
91    source: LintLevelSource,
92    stats: &mut DiagnosticStats,
93    gctx: &GlobalContext,
94) -> CargoResult<()> {
95    let manifest = pkg.manifest();
96
97    let Some(normalized_pkg) = &manifest.normalized_toml().package else {
98        return Ok(());
99    };
100    let Some(InheritableField::Value(homepage)) = &normalized_pkg.homepage else {
101        return Ok(());
102    };
103
104    let other_field = if let Some(InheritableField::Value(repository)) = &normalized_pkg.repository
105        && repository == homepage
106    {
107        "repository"
108    } else if let Some(InheritableField::Value(documentation)) = &normalized_pkg.documentation
109        && documentation == homepage
110    {
111        "documentation"
112    } else {
113        return Ok(());
114    };
115
116    let document = manifest.document();
117    let contents = manifest.contents();
118    let level = lint_level.to_diagnostic_level();
119    let emitted_source = LINT.emitted_source(lint_level, source);
120
121    let mut primary = Group::with_title(level.primary_title(LINT.desc));
122    if let Some(document) = document
123        && let Some(contents) = contents
124        && let Some(span) = get_key_value_span(document, &["package", "homepage"])
125    {
126        let mut snippet = Snippet::source(contents)
127            .path(manifest_path)
128            .annotation(AnnotationKind::Primary.span(span.value));
129        if let Some(span) = get_key_value_span(document, &["package", other_field]) {
130            snippet = snippet.annotation(AnnotationKind::Context.span(span.value));
131        }
132        primary = primary.element(snippet);
133    } else {
134        primary = primary.element(Origin::path(manifest_path));
135    }
136    primary = primary.element(Level::NOTE.message(emitted_source));
137    let mut report = vec![primary];
138    if let Some(document) = document
139        && let Some(contents) = contents
140        && let Some(span) = get_key_value_span(document, &["package", "homepage"])
141    {
142        let span = if let Some(workspace_span) =
143            get_key_value_span(document, &["package", "homepage", "workspace"])
144        {
145            span.key.start..workspace_span.value.end
146        } else {
147            span.key.start..span.value.end
148        };
149        let mut help =
150            Group::with_title(Level::HELP.secondary_title("consider removing `package.homepage`"));
151        help = help.element(
152            Snippet::source(contents)
153                .path(manifest_path)
154                .patch(Patch::new(span, "")),
155        );
156        report.push(help);
157    }
158
159    stats.record_lint(lint_level);
160    gctx.shell().print_report(&report, lint_level.force())?;
161
162    Ok(())
163}