@@ -2,6 +2,7 @@ use rustc_ast::LitKind;
22use rustc_hir:: { Expr , ExprKind } ;
33use rustc_lint:: LateContext ;
44use rustc_middle:: ty;
5+ use rustc_span:: { FileName , Span } ;
56
67/// Every `opentelemetry` `Meter` method that opens an instrument builder, keyed
78/// by the instrument name as its first argument. Shared by the metric lints.
@@ -34,6 +35,24 @@ pub(crate) fn string_literal_span(expr: &Expr<'_>) -> Option<rustc_span::Span> {
3435 }
3536}
3637
38+ /// Whether `span` originates in a test file, i.e. one whose stem is `tests` or
39+ /// ends in `_tests` (`tests.rs`, `parse_tests.rs`). Telemetry call sites in
40+ /// tests exercise the instruments directly rather than routing through the
41+ /// generated `trogon_semconv` constants, so the semconv lints exempt them.
42+ pub ( crate ) fn in_test_file ( cx : & LateContext < ' _ > , span : Span ) -> bool {
43+ let file = cx. tcx . sess . source_map ( ) . lookup_char_pos ( span. lo ( ) ) . file ;
44+ let FileName :: Real ( real) = & file. name else {
45+ return false ;
46+ } ;
47+ let Some ( stem) = real
48+ . local_path ( )
49+ . and_then ( |path| path. file_stem ( ) ?. to_str ( ) . map ( str:: to_owned) )
50+ else {
51+ return false ;
52+ } ;
53+ stem == "tests" || stem. ends_with ( "_tests" )
54+ }
55+
3756/// Whether the adjusted type of `receiver`, with references peeled, is the ADT
3857/// `krate::...::ty_name`. Telemetry types are identified by their owning crate
3958/// and name rather than a full def-path, mirroring how the checked crates avoid
0 commit comments