cargo/diagnostics/rules/
im_a_teapot.rs1use std::path::Path;
2
3use cargo_util_schemas::manifest::TomlToolLints;
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::TEST_DUMMY_UNSTABLE;
12use crate::CargoResult;
13use crate::GlobalContext;
14use crate::core::Feature;
15use crate::core::Package;
16use crate::diagnostics::DiagnosticStats;
17use crate::diagnostics::Lint;
18use crate::diagnostics::LintLevel;
19use crate::diagnostics::get_key_value_span;
20use crate::diagnostics::rel_cwd_manifest_path;
21
22pub static LINT: &Lint = &Lint {
24 name: "im_a_teapot",
25 desc: "`im_a_teapot` is specified",
26 primary_group: &TEST_DUMMY_UNSTABLE,
27 msrv: None,
28 feature_gate: Some(Feature::test_dummy_unstable()),
29 docs: None,
30};
31
32#[instrument(skip_all)]
33pub fn check_im_a_teapot(
34 pkg: &Package,
35 path: &Path,
36 pkg_lints: &TomlToolLints,
37 stats: &mut DiagnosticStats,
38 gctx: &GlobalContext,
39) -> CargoResult<()> {
40 let manifest = pkg.manifest();
41 let (lint_level, source) =
42 LINT.level(pkg_lints, pkg.rust_version(), manifest.unstable_features());
43
44 if lint_level == LintLevel::Allow {
45 return Ok(());
46 }
47
48 if manifest
49 .normalized_toml()
50 .package()
51 .is_some_and(|p| p.im_a_teapot.is_some())
52 {
53 let level = lint_level.to_diagnostic_level();
54 let manifest_path = rel_cwd_manifest_path(path, gctx);
55 let emitted_source = LINT.emitted_source(lint_level, source);
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_source))];
74
75 stats.record_lint(lint_level);
76 gctx.shell().print_report(report, lint_level.force())?;
77 }
78 Ok(())
79}