use subdiagnostic for sugesting add let

This commit is contained in:
yukang 2022-11-08 16:04:14 +08:00
parent 667b15bb0e
commit 9e7d2287cd
5 changed files with 21 additions and 7 deletions

View file

@ -171,3 +171,4 @@ infer_msl_introduces_static = introduces a `'static` lifetime requirement
infer_msl_unmet_req = because this has an unmet lifetime requirement infer_msl_unmet_req = because this has an unmet lifetime requirement
infer_msl_trait_note = this has an implicit `'static` lifetime requirement infer_msl_trait_note = this has an implicit `'static` lifetime requirement
infer_msl_trait_sugg = consider relaxing the implicit `'static` requirement infer_msl_trait_sugg = consider relaxing the implicit `'static` requirement
infer_suggest_add_let_for_letchains = consider adding `let`

View file

@ -180,6 +180,18 @@ pub enum SourceKindMultiSuggestion<'a> {
}, },
} }
#[derive(Subdiagnostic)]
#[suggestion(
infer_suggest_add_let_for_letchains,
style = "verbose",
applicability = "machine-applicable",
code = "let "
)]
pub(crate) struct SuggAddLetForLetChains {
#[primary_span]
pub span: Span,
}
impl<'a> SourceKindMultiSuggestion<'a> { impl<'a> SourceKindMultiSuggestion<'a> {
pub fn new_fully_qualified( pub fn new_fully_qualified(
span: Span, span: Span,

View file

@ -59,6 +59,7 @@ use crate::traits::{
StatementAsExpression, StatementAsExpression,
}; };
use crate::errors::SuggAddLetForLetChains;
use hir::intravisit::{walk_expr, walk_stmt}; use hir::intravisit::{walk_expr, walk_stmt};
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet}; use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_errors::{pluralize, struct_span_err, Diagnostic, ErrorGuaranteed, IntoDiagnosticArg}; use rustc_errors::{pluralize, struct_span_err, Diagnostic, ErrorGuaranteed, IntoDiagnosticArg};
@ -2421,12 +2422,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
let mut visitor = IfVisitor { err_span: span, found_if: false, result: false }; let mut visitor = IfVisitor { err_span: span, found_if: false, result: false };
visitor.visit_body(&body); visitor.visit_body(&body);
if visitor.result { if visitor.result {
err.span_suggestion_verbose( err.subdiagnostic(SuggAddLetForLetChains{span: span.shrink_to_lo()});
span.shrink_to_lo(),
"consider adding `let`",
"let ".to_string(),
Applicability::MachineApplicable,
);
} }
} }
} }

View file

@ -425,7 +425,7 @@ pub(crate) struct ExpectedExpressionFoundLet {
pub(crate) struct ExpectedEqForLetExpr { pub(crate) struct ExpectedEqForLetExpr {
#[primary_span] #[primary_span]
pub span: Span, pub span: Span,
#[suggestion_verbose(applicability = "maybe-incorrect", code = "=")] #[suggestion(applicability = "maybe-incorrect", code = "=", style = "verbose")]
pub sugg_span: Span, pub sugg_span: Span,
} }

View file

@ -14,6 +14,11 @@ error[E0308]: mismatched types
| |
LL | if Err(err) = File::open("hello.txt") { LL | if Err(err) = File::open("hello.txt") {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
|
help: consider adding `let`
|
LL | if let Err(err) = File::open("hello.txt") {
| +++
error: aborting due to 2 previous errors error: aborting due to 2 previous errors