Skip to content

Commit 3cc2a6f

Browse files
committed
untranslatable_diagnostic lint: point at the untranslated thing
and not the function/method call
1 parent 10ef666 commit 3cc2a6f

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

compiler/rustc_lint/src/internal.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -415,14 +415,17 @@ declare_lint_pass!(Diagnostics => [UNTRANSLATABLE_DIAGNOSTIC, DIAGNOSTIC_OUTSIDE
415415

416416
impl LateLintPass<'_> for Diagnostics {
417417
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
418+
let collect_args_tys_and_spans = |args: &[Expr<'_>], reserve_one_extra: bool| {
419+
let mut result = Vec::with_capacity(args.len() + usize::from(reserve_one_extra));
420+
result.extend(args.iter().map(|arg| (cx.typeck_results().expr_ty(arg), arg.span)));
421+
result
422+
};
418423
// Only check function calls and method calls.
419-
let (span, def_id, fn_gen_args, call_tys) = match expr.kind {
424+
let (span, def_id, fn_gen_args, arg_tys_and_spans) = match expr.kind {
420425
ExprKind::Call(callee, args) => {
421426
match cx.typeck_results().node_type(callee.hir_id).kind() {
422427
&ty::FnDef(def_id, fn_gen_args) => {
423-
let call_tys: Vec<_> =
424-
args.iter().map(|arg| cx.typeck_results().expr_ty(arg)).collect();
425-
(callee.span, def_id, fn_gen_args, call_tys)
428+
(callee.span, def_id, fn_gen_args, collect_args_tys_and_spans(args, false))
426429
}
427430
_ => return, // occurs for fns passed as args
428431
}
@@ -432,10 +435,9 @@ impl LateLintPass<'_> for Diagnostics {
432435
else {
433436
return;
434437
};
435-
let mut call_tys: Vec<_> =
436-
args.iter().map(|arg| cx.typeck_results().expr_ty(arg)).collect();
437-
call_tys.insert(0, cx.tcx.types.self_param); // dummy inserted for `self`
438-
(span, def_id, fn_gen_args, call_tys)
438+
let mut args = collect_args_tys_and_spans(args, true);
439+
args.insert(0, (cx.tcx.types.self_param, _recv.span)); // dummy inserted for `self`
440+
(span, def_id, fn_gen_args, args)
439441
}
440442
_ => return,
441443
};
@@ -525,11 +527,11 @@ impl LateLintPass<'_> for Diagnostics {
525527
// `UNTRANSLATABLE_DIAGNOSTIC` lint.
526528
for (param_i, param_i_p_name) in impl_into_diagnostic_message_params {
527529
// Is the arg type `{Sub,D}iagMessage`or `impl Into<{Sub,D}iagMessage>`?
528-
let arg_ty = call_tys[param_i];
530+
let (arg_ty, arg_span) = arg_tys_and_spans[param_i];
529531
let is_translatable = is_diag_message(arg_ty)
530532
|| matches!(arg_ty.kind(), ty::Param(p) if p.name == param_i_p_name);
531533
if !is_translatable {
532-
cx.emit_span_lint(UNTRANSLATABLE_DIAGNOSTIC, span, UntranslatableDiag);
534+
cx.emit_span_lint(UNTRANSLATABLE_DIAGNOSTIC, arg_span, UntranslatableDiag);
533535
}
534536
}
535537
}
Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: diagnostics should be created using translatable messages
2-
--> $DIR/diagnostics.rs:43:9
2+
--> $DIR/diagnostics.rs:43:31
33
|
44
LL | Diag::new(dcx, level, "untranslatable diagnostic")
5-
| ^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
note: the lint level is defined here
88
--> $DIR/diagnostics.rs:7:9
@@ -11,16 +11,16 @@ LL | #![deny(rustc::untranslatable_diagnostic)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

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

1919
error: diagnostics should be created using translatable messages
20-
--> $DIR/diagnostics.rs:85:14
20+
--> $DIR/diagnostics.rs:85:19
2121
|
2222
LL | diag.note("untranslatable diagnostic");
23-
| ^^^^
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2424

2525
error: diagnostics should only be created in `Diagnostic`/`Subdiagnostic`/`LintDiagnostic` impls
2626
--> $DIR/diagnostics.rs:99:21
@@ -41,36 +41,34 @@ LL | let _diag = dcx.struct_err("untranslatable diagnostic");
4141
| ^^^^^^^^^^
4242

4343
error: diagnostics should be created using translatable messages
44-
--> $DIR/diagnostics.rs:102:21
44+
--> $DIR/diagnostics.rs:102:32
4545
|
4646
LL | let _diag = dcx.struct_err("untranslatable diagnostic");
47-
| ^^^^^^^^^^
47+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
4848

4949
error: diagnostics should be created using translatable messages
50-
--> $DIR/diagnostics.rs:120:5
50+
--> $DIR/diagnostics.rs:120:7
5151
|
5252
LL | f("untranslatable diagnostic", crate::fluent_generated::no_crate_example);
53-
| ^
53+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
5454

5555
error: diagnostics should be created using translatable messages
56-
--> $DIR/diagnostics.rs:122:5
56+
--> $DIR/diagnostics.rs:122:50
5757
|
5858
LL | f(crate::fluent_generated::no_crate_example, "untranslatable diagnostic");
59-
| ^
59+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
6060

6161
error: diagnostics should be created using translatable messages
62-
--> $DIR/diagnostics.rs:124:5
62+
--> $DIR/diagnostics.rs:124:7
6363
|
6464
LL | f("untranslatable diagnostic", "untranslatable diagnostic");
65-
| ^
65+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
6666

6767
error: diagnostics should be created using translatable messages
68-
--> $DIR/diagnostics.rs:124:5
68+
--> $DIR/diagnostics.rs:124:36
6969
|
7070
LL | f("untranslatable diagnostic", "untranslatable diagnostic");
71-
| ^
72-
|
73-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
71+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
7472

7573
error: aborting due to 10 previous errors
7674

0 commit comments

Comments
 (0)