@@ -30,8 +30,8 @@ LL | more code
30
30
(See [ diagnostic levels] ( #diagnostic-levels ) )
31
31
- Code (for example, for "mismatched types", it is ` E0308 ` ). It helps
32
32
users get more information about the current error through an extended
33
- description of the problem in the error code index. Diagnostics created
34
- by lints don't have a code in the emitted message .
33
+ description of the problem in the error code index. Not all diagnostic have a
34
+ code. For example, diagnostics created by lints don't have one .
35
35
- Message. It is the main description of the problem. It should be general and
36
36
able to stand on its own, so that it can make sense even in isolation.
37
37
- Diagnostic window. This contains several things:
@@ -116,7 +116,7 @@ Here are a few examples:
116
116
so warnings are instead emitted,
117
117
and will eventually be turned into fixed (hard) errors.
118
118
119
- Hard-coded warnings (those using the ` span_warn ` methods ) should be avoided
119
+ Hard-coded warnings (those using methods like ` span_warn ` ) should be avoided
120
120
for normal code, preferring to use lints instead. Some cases, such as warnings
121
121
with CLI flags, will require the use of hard-coded warnings.
122
122
@@ -139,7 +139,7 @@ use an error-level lint instead of a fixed error.
139
139
- The word "illegal" is illegal. Prefer "invalid" or a more specific word
140
140
instead.
141
141
- Errors should document the span of code where they occur (use
142
- [ ` rustc_errors::diagnostic_builder::DiagnosticBuilder ` ] [ diagbuild ] 's
142
+ [ ` rustc_errors::DiagCtxt ` ] [ DiagCtxt ] 's
143
143
` span_* ` methods or a diagnostic struct's ` #[primary_span] ` to easily do
144
144
this). Also ` note ` other spans that have contributed to the error if the span
145
145
isn't too large.
@@ -324,14 +324,12 @@ described below can be used as normal.
324
324
325
325
[ diagnostic-structs ] : ./diagnostics/diagnostic-structs.md
326
326
327
- [ ` Session ` ] [ session ] and [ ` ParseSess ` ] [ parsesses ] have
328
- methods (or fields with methods) that allow reporting errors. These methods
327
+ [ ` DiagCtxt ` ] [ DiagCtxt ] has methods that create and emit errors. These methods
329
328
usually have names like ` span_err ` or ` struct_span_err ` or ` span_warn ` , etc...
330
329
There are lots of them; they emit different types of "errors", such as
331
330
warnings, errors, fatal errors, suggestions, etc.
332
331
333
- [ parsesses ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_session/parse/struct.ParseSess.html
334
- [ session ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_session/struct.Session.html
332
+ [ DiagCtxt ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.DiagCtxt.html
335
333
336
334
In general, there are two classes of such methods: ones that emit an error
337
335
directly and ones that allow finer control over what to emit. For example,
@@ -350,15 +348,15 @@ before emitting it by calling the [`emit`][emit] method. (Failing to either
350
348
emit or [ cancel] [ cancel ] a ` DiagnosticBuilder ` will result in an ICE.) See the
351
349
[ docs] [ diagbuild ] for more info on what you can do.
352
350
353
- [ spanerr ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_session /struct.Session .html#method.span_err
354
- [ strspanerr ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_session /struct.Session .html#method.struct_span_err
351
+ [ spanerr ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors /struct.DiagCtxt .html#method.span_err
352
+ [ strspanerr ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors /struct.DiagCtxt .html#method.struct_span_err
355
353
[ diagbuild ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/diagnostic_builder/struct.DiagnosticBuilder.html
356
354
[ emit ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/diagnostic_builder/struct.DiagnosticBuilder.html#method.emit
357
- [ cancel ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.Diagnostic .html#method.cancel
355
+ [ cancel ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/diagnostic_builder/ struct.DiagnosticBuilder .html#method.cancel
358
356
359
357
``` rust,ignore
360
358
// Get a DiagnosticBuilder. This does _not_ emit an error yet.
361
- let mut err = sess.struct_span_err(sp, fluent::example::example_error);
359
+ let mut err = sess.dcx. struct_span_err(sp, fluent::example::example_error);
362
360
363
361
// In some cases, you might need to check if `sp` is generated by a macro to
364
362
// avoid printing weird errors about macro-generated code.
@@ -421,7 +419,7 @@ apply them)
421
419
For example, to make our ` qux ` suggestion machine-applicable, we would do:
422
420
423
421
``` rust,ignore
424
- let mut err = sess.struct_span_err(sp, fluent::example::message);
422
+ let mut err = sess.dcx. struct_span_err(sp, fluent::example::message);
425
423
426
424
if let Ok(snippet) = sess.source_map().span_to_snippet(sp) {
427
425
err.span_suggestion(
@@ -644,24 +642,22 @@ fn pierce_parens(mut expr: &ast::Expr) -> &ast::Expr {
644
642
// list of methods.
645
643
impl EarlyLintPass for WhileTrue {
646
644
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
647
- if let ast::ExprKind::While(cond, ..) = &e.kind {
648
- if let ast::ExprKind::Lit(ref lit) = pierce_parens(cond).kind {
649
- if let ast::LitKind::Bool(true) = lit.kind {
650
- if !lit.span.from_expansion() {
651
- let condition_span = cx.sess.source_map().guess_head_span(e.span);
652
- cx.struct_span_lint(WHILE_TRUE, condition_span, |lint| {
653
- lint.build(fluent::example::use_loop)
654
- .span_suggestion_short(
655
- condition_span,
656
- fluent::example::suggestion,
657
- "loop".to_owned(),
658
- Applicability::MachineApplicable,
659
- )
660
- .emit();
661
- })
662
- }
663
- }
664
- }
645
+ if let ast::ExprKind::While(cond, ..) = &e.kind
646
+ && let ast::ExprKind::Lit(ref lit) = pierce_parens(cond).kind
647
+ && let ast::LitKind::Bool(true) = lit.kind
648
+ && !lit.span.from_expansion()
649
+ {
650
+ let condition_span = cx.sess.source_map().guess_head_span(e.span);
651
+ cx.struct_span_lint(WHILE_TRUE, condition_span, |lint| {
652
+ lint.build(fluent::example::use_loop)
653
+ .span_suggestion_short(
654
+ condition_span,
655
+ fluent::example::suggestion,
656
+ "loop".to_owned(),
657
+ Applicability::MachineApplicable,
658
+ )
659
+ .emit();
660
+ })
665
661
}
666
662
}
667
663
}
0 commit comments