#
cargo/lints/rules/
im_a_teapot.rs

    1use std::path::Path;
    2
    3use annotate_snippets::AnnotationKind;
    4use annotate_snippets::Group;
    5use annotate_snippets::Level;
    6use annotate_snippets::Origin;
    7use annotate_snippets::Snippet;
    8use cargo_util_schemas::manifest::TomlToolLints;
    9
    10use crate::CargoResult;
    11use crate::GlobalContext;
    12use crate::core::Feature;
    13use crate::core::Package;
    14use crate::lints::Lint;
    15use crate::lints::LintLevel;
    16use crate::lints::TEST_DUMMY_UNSTABLE;
    17use crate::lints::get_key_value_span;
    18use crate::lints::rel_cwd_manifest_path;
    19
    20/// This lint is only to be used for testing purposes
    21pub const LINT: Lint = Lint {
    22    name: "im_a_teapot",
    23    desc: "`im_a_teapot` is specified",
    24    primary_group: &TEST_DUMMY_UNSTABLE,
    25    edition_lint_opts: None,
    26    feature_gate: Some(Feature::test_dummy_unstable()),
    27    docs: None,
    28};
    29
    30pub fn check_im_a_teapot(
    31    pkg: &Package,
    32    path: &Path,
    33    pkg_lints: &TomlToolLints,
    34    error_count: &mut usize,
    35    gctx: &GlobalContext,
    36) -> CargoResult<()> {
    37    let manifest = pkg.manifest();
    38    let (lint_level, reason) =
    39        LINT.level(pkg_lints, manifest.edition(), manifest.unstable_features());
    40
    41    if lint_level == LintLevel::Allow {
    42        return Ok(());
    43    }
    44
    45    if manifest
    46        .normalized_toml()
    47        .package()
    48        .is_some_and(|p| p.im_a_teapot.is_some())
    49    {
    50        if lint_level.is_error() {
    51            *error_count += 1;
    52        }
    53        let level = lint_level.to_diagnostic_level();
    54        let manifest_path = rel_cwd_manifest_path(path, gctx);
    55        let emitted_reason = LINT.emitted_source(lint_level, reason);
    56
    57        let mut desc = Group::with_title(level.primary_title(LINT.desc));
    58
    59        if let Some(document) = manifest.document()
    60            && let Some(contents) = manifest.contents()
    61        {
    62            let span = get_key_value_span(document, &["package", "im-a-teapot"]).unwrap();
    63
    64            desc = desc.element(
    65                Snippet::source(contents)
    66                    .path(&manifest_path)
    67                    .annotation(AnnotationKind::Primary.span(span.key.start..span.value.end)),
    68            );
    69        } else {
    70            desc = desc.element(Origin::path(&manifest_path));
    71        }
    72
    73        let report = &[desc.element(Level::NOTE.message(&emitted_reason))];
    74
    75        gctx.shell().print_report(report, lint_level.force())?;
    76    }
    77    Ok(())
    78}