lint: port while true diagnostics

Signed-off-by: David Wood <david.wood@huawei.com>
This commit is contained in:
David Wood 2022-06-28 10:55:42 +01:00
parent 4f7b10f484
commit 588977b350
2 changed files with 6 additions and 4 deletions

View File

@ -286,3 +286,6 @@ lint-unused-import-braces = braces around {$node} is unnecessary
lint-unused-allocation = unnecessary allocation, use `&` instead lint-unused-allocation = unnecessary allocation, use `&` instead
lint-unused-allocation-mut = unnecessary allocation, use `&mut` instead lint-unused-allocation-mut = unnecessary allocation, use `&mut` instead
lint-builtin-while-true = denote infinite loops with `loop {"{"} ... {"}"}`
.suggestion = use `loop`

View File

@ -31,7 +31,7 @@ use rustc_ast::{self as ast, *};
use rustc_ast_pretty::pprust::{self, expr_to_string}; use rustc_ast_pretty::pprust::{self, expr_to_string};
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_errors::{Applicability, Diagnostic, DiagnosticStyledString, MultiSpan}; use rustc_errors::{fluent, Applicability, Diagnostic, DiagnosticStyledString, MultiSpan};
use rustc_feature::{deprecated_attributes, AttributeGate, BuiltinAttribute, GateIssue, Stability}; use rustc_feature::{deprecated_attributes, AttributeGate, BuiltinAttribute, GateIssue, Stability};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res}; use rustc_hir::def::{DefKind, Res};
@ -99,13 +99,12 @@ impl EarlyLintPass for WhileTrue {
if let ast::ExprKind::Lit(ref lit) = pierce_parens(cond).kind { if let ast::ExprKind::Lit(ref lit) = pierce_parens(cond).kind {
if let ast::LitKind::Bool(true) = lit.kind { if let ast::LitKind::Bool(true) = lit.kind {
if !lit.span.from_expansion() { if !lit.span.from_expansion() {
let msg = "denote infinite loops with `loop { ... }`";
let condition_span = e.span.with_hi(cond.span.hi()); let condition_span = e.span.with_hi(cond.span.hi());
cx.struct_span_lint(WHILE_TRUE, condition_span, |lint| { cx.struct_span_lint(WHILE_TRUE, condition_span, |lint| {
lint.build(msg) lint.build(fluent::lint::builtin_while_true)
.span_suggestion_short( .span_suggestion_short(
condition_span, condition_span,
"use `loop`", fluent::lint::suggestion,
format!( format!(
"{}loop", "{}loop",
label.map_or_else(String::new, |label| format!( label.map_or_else(String::new, |label| format!(