@@ -30,8 +30,8 @@ LL | more code
3030 (See [ diagnostic levels] ( #diagnostic-levels ) )
3131- Code (for example, for "mismatched types", it is ` E0308 ` ). It helps
3232 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 .
3535- Message. It is the main description of the problem. It should be general and
3636 able to stand on its own, so that it can make sense even in isolation.
3737- Diagnostic window. This contains several things:
@@ -116,7 +116,7 @@ Here are a few examples:
116116 so warnings are instead emitted,
117117 and will eventually be turned into fixed (hard) errors.
118118
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
120120for normal code, preferring to use lints instead. Some cases, such as warnings
121121with CLI flags, will require the use of hard-coded warnings.
122122
@@ -139,7 +139,7 @@ use an error-level lint instead of a fixed error.
139139- The word "illegal" is illegal. Prefer "invalid" or a more specific word
140140 instead.
141141- 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
143143 ` span_* ` methods or a diagnostic struct's ` #[primary_span] ` to easily do
144144 this). Also ` note ` other spans that have contributed to the error if the span
145145 isn't too large.
@@ -324,14 +324,12 @@ described below can be used as normal.
324324
325325[ diagnostic-structs ] : ./diagnostics/diagnostic-structs.md
326326
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
329328usually have names like ` span_err ` or ` struct_span_err ` or ` span_warn ` , etc...
330329There are lots of them; they emit different types of "errors", such as
331330warnings, errors, fatal errors, suggestions, etc.
332331
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
335333
336334In general, there are two classes of such methods: ones that emit an error
337335directly 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
350348emit or [ cancel] [ cancel ] a ` DiagnosticBuilder ` will result in an ICE.) See the
351349[ docs] [ diagbuild ] for more info on what you can do.
352350
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
355353[ diagbuild ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/diagnostic_builder/struct.DiagnosticBuilder.html
356354[ 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
358356
359357``` rust,ignore
360358// 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);
362360
363361// In some cases, you might need to check if `sp` is generated by a macro to
364362// avoid printing weird errors about macro-generated code.
@@ -421,7 +419,7 @@ apply them)
421419For example, to make our ` qux ` suggestion machine-applicable, we would do:
422420
423421``` 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);
425423
426424if let Ok(snippet) = sess.source_map().span_to_snippet(sp) {
427425 err.span_suggestion(
@@ -644,24 +642,22 @@ fn pierce_parens(mut expr: &ast::Expr) -> &ast::Expr {
644642// list of methods.
645643impl EarlyLintPass for WhileTrue {
646644 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+ })
665661 }
666662 }
667663}
0 commit comments