Rewrite the untranslatable_diagnostic lint.

Currently it only checks calls to functions marked with
`#[rustc_lint_diagnostics]`. This commit changes it to check calls to
any function with an `impl Into<{D,Subd}iagMessage>` parameter. This
greatly improves its coverage and doesn't rely on people remembering to
add `#[rustc_lint_diagnostics]`.

The commit also adds `#[allow(rustc::untranslatable_diagnostic)`]
attributes to places that need it that are caught by the improved lint.
These places that might be easy to convert to translatable diagnostics.

Finally, it also:
- Expands and corrects some comments.
- Does some minor formatting improvements.
- Adds missing `DecorateLint` cases to
  `tests/ui-fulldeps/internal-lints/diagnostics.rs`.
This commit is contained in:
Nicholas Nethercote
2024-02-20 14:12:50 +11:00
parent d602394827
commit b7d58eef4b
35 changed files with 224 additions and 63 deletions

View File

@@ -14,8 +14,8 @@ extern crate rustc_session;
extern crate rustc_span;
use rustc_errors::{
AddToDiagnostic, Diag, EmissionGuarantee, DiagCtxt, IntoDiagnostic, Level,
SubdiagMessageOp,
AddToDiagnostic, DecorateLint, Diag, DiagCtxt, DiagInner, DiagMessage, EmissionGuarantee,
IntoDiagnostic, Level, SubdiagMessageOp,
};
use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_span::Span;
@@ -78,6 +78,31 @@ impl AddToDiagnostic for TranslatableInAddToDiagnostic {
}
}
pub struct UntranslatableInDecorateLint;
impl<'a> DecorateLint<'a, ()> for UntranslatableInDecorateLint {
fn decorate_lint<'b, >(self, diag: &'b mut Diag<'a, ()>) {
diag.note("untranslatable diagnostic");
//~^ ERROR diagnostics should be created using translatable messages
}
fn msg(&self) -> DiagMessage {
unreachable!();
}
}
pub struct TranslatableInDecorateLint;
impl<'a> DecorateLint<'a, ()> for TranslatableInDecorateLint {
fn decorate_lint<'b>(self, diag: &'b mut Diag<'a, ()>) {
diag.note(crate::fluent_generated::no_crate_note);
}
fn msg(&self) -> DiagMessage {
unreachable!();
}
}
pub fn make_diagnostics<'a>(dcx: &'a DiagCtxt) {
let _diag = dcx.struct_err(crate::fluent_generated::no_crate_example);
//~^ ERROR diagnostics should only be created in `IntoDiagnostic`/`AddToDiagnostic` impls
@@ -87,9 +112,11 @@ pub fn make_diagnostics<'a>(dcx: &'a DiagCtxt) {
//~^^ ERROR diagnostics should be created using translatable messages
}
// Check that `rustc_lint_diagnostics`-annotated functions aren't themselves linted.
// Check that `rustc_lint_diagnostics`-annotated functions aren't themselves linted for
// `diagnostic_outside_of_impl`.
#[rustc_lint_diagnostics]
pub fn skipped_because_of_annotation<'a>(dcx: &'a DiagCtxt) {
#[allow(rustc::untranslatable_diagnostic)]
let _diag = dcx.struct_err("untranslatable diagnostic"); // okay!
}

View File

@@ -16,8 +16,14 @@ error: diagnostics should be created using translatable messages
LL | diag.note("untranslatable diagnostic");
| ^^^^
error: diagnostics should be created using translatable messages
--> $DIR/diagnostics.rs:85:14
|
LL | diag.note("untranslatable diagnostic");
| ^^^^
error: diagnostics should only be created in `IntoDiagnostic`/`AddToDiagnostic` impls
--> $DIR/diagnostics.rs:82:21
--> $DIR/diagnostics.rs:107:21
|
LL | let _diag = dcx.struct_err(crate::fluent_generated::no_crate_example);
| ^^^^^^^^^^
@@ -29,16 +35,16 @@ LL | #![deny(rustc::diagnostic_outside_of_impl)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: diagnostics should only be created in `IntoDiagnostic`/`AddToDiagnostic` impls
--> $DIR/diagnostics.rs:85:21
--> $DIR/diagnostics.rs:110:21
|
LL | let _diag = dcx.struct_err("untranslatable diagnostic");
| ^^^^^^^^^^
error: diagnostics should be created using translatable messages
--> $DIR/diagnostics.rs:85:21
--> $DIR/diagnostics.rs:110:21
|
LL | let _diag = dcx.struct_err("untranslatable diagnostic");
| ^^^^^^^^^^
error: aborting due to 5 previous errors
error: aborting due to 6 previous errors