Skip to content

Commit 11fc785

Browse files
committedAug 31, 2022
lint: avoid linting diag functions with diag lints
Functions annotated with `#[rustc_lint_diagnostics]` are used by the diagnostic migration lints to know when to lint, but functions that are annotated with this attribute shouldn't themselves be linted. Signed-off-by: David Wood <david.wood@huawei.com>
1 parent 7f442f8 commit 11fc785

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed
 

‎compiler/rustc_lint/src/internal.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,14 @@ impl LateLintPass<'_> for Diagnostics {
393393
return;
394394
}
395395

396+
let mut found_parent_with_attr = false;
396397
let mut found_impl = false;
397-
for (_, parent) in cx.tcx.hir().parent_iter(expr.hir_id) {
398+
for (hir_id, parent) in cx.tcx.hir().parent_iter(expr.hir_id) {
399+
if let Some(owner_did) = hir_id.as_owner() {
400+
found_parent_with_attr = found_parent_with_attr
401+
|| cx.tcx.has_attr(owner_did.to_def_id(), sym::rustc_lint_diagnostics);
402+
}
403+
398404
debug!(?parent);
399405
if let Node::Item(Item { kind: ItemKind::Impl(impl_), .. }) = parent &&
400406
let Impl { of_trait: Some(of_trait), .. } = impl_ &&
@@ -407,7 +413,7 @@ impl LateLintPass<'_> for Diagnostics {
407413
}
408414
}
409415
debug!(?found_impl);
410-
if !found_impl {
416+
if !found_parent_with_attr && !found_impl {
411417
cx.struct_span_lint(DIAGNOSTIC_OUTSIDE_OF_IMPL, span, |lint| {
412418
lint.build(fluent::lint::diag_out_of_impl).emit();
413419
})
@@ -425,7 +431,7 @@ impl LateLintPass<'_> for Diagnostics {
425431
}
426432
}
427433
debug!(?found_diagnostic_message);
428-
if !found_diagnostic_message {
434+
if !found_parent_with_attr && !found_diagnostic_message {
429435
cx.struct_span_lint(UNTRANSLATABLE_DIAGNOSTIC, span, |lint| {
430436
lint.build(fluent::lint::untranslatable_diag).emit();
431437
})

‎src/test/ui-fulldeps/internal-lints/diagnostics.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// compile-flags: -Z unstable-options
22

33
#![crate_type = "lib"]
4+
#![feature(rustc_attrs)]
45
#![feature(rustc_private)]
56
#![deny(rustc::untranslatable_diagnostic)]
67
#![deny(rustc::diagnostic_outside_of_impl)]
@@ -71,3 +72,10 @@ pub fn make_diagnostics<'a>(sess: &'a ParseSess) {
7172
//~^ ERROR diagnostics should only be created in `SessionDiagnostic`/`AddSubdiagnostic` impls
7273
//~^^ ERROR diagnostics should be created using translatable messages
7374
}
75+
76+
// Check that `rustc_lint_diagnostics`-annotated functions aren't themselves linted.
77+
78+
#[rustc_lint_diagnostics]
79+
pub fn skipped_because_of_annotation<'a>(sess: &'a ParseSess) {
80+
let _diag = sess.struct_err("untranslatable diagnostic"); // okay!
81+
}

‎src/test/ui-fulldeps/internal-lints/diagnostics.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
11
error: diagnostics should be created using translatable messages
2-
--> $DIR/diagnostics.rs:36:14
2+
--> $DIR/diagnostics.rs:37:14
33
|
44
LL | sess.struct_err("untranslatable diagnostic")
55
| ^^^^^^^^^^
66
|
77
note: the lint level is defined here
8-
--> $DIR/diagnostics.rs:5:9
8+
--> $DIR/diagnostics.rs:6:9
99
|
1010
LL | #![deny(rustc::untranslatable_diagnostic)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

1313
error: diagnostics should be created using translatable messages
14-
--> $DIR/diagnostics.rs:53:14
14+
--> $DIR/diagnostics.rs:54:14
1515
|
1616
LL | diag.note("untranslatable diagnostic");
1717
| ^^^^
1818

1919
error: diagnostics should only be created in `SessionDiagnostic`/`AddSubdiagnostic` impls
20-
--> $DIR/diagnostics.rs:67:22
20+
--> $DIR/diagnostics.rs:68:22
2121
|
2222
LL | let _diag = sess.struct_err(fluent::parser::expect_path);
2323
| ^^^^^^^^^^
2424
|
2525
note: the lint level is defined here
26-
--> $DIR/diagnostics.rs:6:9
26+
--> $DIR/diagnostics.rs:7:9
2727
|
2828
LL | #![deny(rustc::diagnostic_outside_of_impl)]
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3030

3131
error: diagnostics should only be created in `SessionDiagnostic`/`AddSubdiagnostic` impls
32-
--> $DIR/diagnostics.rs:70:22
32+
--> $DIR/diagnostics.rs:71:22
3333
|
3434
LL | let _diag = sess.struct_err("untranslatable diagnostic");
3535
| ^^^^^^^^^^
3636

3737
error: diagnostics should be created using translatable messages
38-
--> $DIR/diagnostics.rs:70:22
38+
--> $DIR/diagnostics.rs:71:22
3939
|
4040
LL | let _diag = sess.struct_err("untranslatable diagnostic");
4141
| ^^^^^^^^^^

0 commit comments

Comments
 (0)
Please sign in to comment.