Skip to content

Commit 41d1340

Browse files
committed
errors: implement fallback diagnostic translation
This commit updates the signatures of all diagnostic functions to accept types that can be converted into a `DiagnosticMessage`. This enables existing diagnostic calls to continue to work as before and Fluent identifiers to be provided. The `SessionDiagnostic` derive just generates normal diagnostic calls, so these APIs had to be modified to accept Fluent identifiers. In addition, loading of the "fallback" Fluent bundle, which contains the built-in English messages, has been implemented. Each diagnostic now has "arguments" which correspond to variables in the Fluent messages (necessary to render a Fluent message) but no API for adding arguments has been added yet. Therefore, diagnostics (that do not require interpolation) can be converted to use Fluent identifiers and will be output as before.
1 parent c8b9e85 commit 41d1340

File tree

6 files changed

+24
-10
lines changed

6 files changed

+24
-10
lines changed

clippy_lints/src/collapsible_match.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ fn check_arm<'tcx>(
130130
&msg,
131131
|diag| {
132132
let mut help_span = MultiSpan::from_spans(vec![binding_span, inner_then_pat.span]);
133-
help_span.push_span_label(binding_span, "replace this binding".into());
134-
help_span.push_span_label(inner_then_pat.span, "with this pattern".into());
133+
help_span.push_span_label(binding_span, "replace this binding");
134+
help_span.push_span_label(inner_then_pat.span, "with this pattern");
135135
diag.span_help(help_span, "the outer pattern can be modified to include the inner pattern");
136136
},
137137
);

clippy_lints/src/doc.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,17 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
621621
let filename = FileName::anon_source_code(&code);
622622

623623
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
624-
let emitter = EmitterWriter::new(Box::new(io::sink()), None, false, false, false, None, false);
624+
let fallback_bundle = rustc_errors::fallback_fluent_bundle();
625+
let emitter = EmitterWriter::new(
626+
Box::new(io::sink()),
627+
None,
628+
fallback_bundle,
629+
false,
630+
false,
631+
false,
632+
None,
633+
false,
634+
);
625635
let handler = Handler::with_emitter(false, None, Box::new(emitter));
626636
let sess = ParseSess::with_span_handler(handler, sm);
627637

clippy_lints/src/loops/needless_collect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ fn check_needless_collect_indirect_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCo
102102

103103
// Suggest replacing iter_call with iter_replacement, and removing stmt
104104
let mut span = MultiSpan::from_span(method_name.ident.span);
105-
span.push_span_label(iter_call.span, "the iterator could be used here instead".into());
105+
span.push_span_label(iter_call.span, "the iterator could be used here instead");
106106
span_lint_hir_and_then(
107107
cx,
108108
super::NEEDLESS_COLLECT,

clippy_lints/src/missing_const_for_fn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
148148

149149
if let Err((span, err)) = is_min_const_fn(cx.tcx, mir, self.msrv.as_ref()) {
150150
if cx.tcx.is_const_fn_raw(def_id.to_def_id()) {
151-
cx.tcx.sess.span_err(span, &err);
151+
cx.tcx.sess.span_err(span, err.as_ref());
152152
}
153153
} else {
154154
span_lint(cx, MISSING_CONST_FOR_FN, span, "this could be a `const fn`");

clippy_lints/src/needless_pass_by_value.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,12 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
235235
for (span, suggestion) in clone_spans {
236236
diag.span_suggestion(
237237
span,
238-
&snippet_opt(cx, span)
238+
snippet_opt(cx, span)
239239
.map_or(
240240
"change the call to".into(),
241241
|x| Cow::from(format!("change `{}` to", x)),
242-
),
242+
)
243+
.as_ref(),
243244
suggestion.into(),
244245
Applicability::Unspecified,
245246
);
@@ -264,11 +265,12 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
264265
for (span, suggestion) in clone_spans {
265266
diag.span_suggestion(
266267
span,
267-
&snippet_opt(cx, span)
268+
snippet_opt(cx, span)
268269
.map_or(
269270
"change the call to".into(),
270271
|x| Cow::from(format!("change `{}` to", x))
271-
),
272+
)
273+
.as_ref(),
272274
suggestion.into(),
273275
Applicability::Unspecified,
274276
);

src/driver.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,11 @@ fn report_clippy_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
165165
// Separate the output with an empty line
166166
eprintln!();
167167

168+
let fallback_bundle = rustc_errors::fallback_fluent_bundle();
168169
let emitter = Box::new(rustc_errors::emitter::EmitterWriter::stderr(
169170
rustc_errors::ColorConfig::Auto,
170171
None,
172+
fallback_bundle,
171173
false,
172174
false,
173175
None,
@@ -191,7 +193,7 @@ fn report_clippy_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
191193
];
192194

193195
for note in &xs {
194-
handler.note_without_error(note);
196+
handler.note_without_error(note.as_ref());
195197
}
196198

197199
// If backtraces are enabled, also print the query stack

0 commit comments

Comments
 (0)