Skip to content

Commit 9e7d228

Browse files
committed
use subdiagnostic for sugesting add let
1 parent 667b15b commit 9e7d228

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

compiler/rustc_error_messages/locales/en-US/infer.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,4 @@ infer_msl_introduces_static = introduces a `'static` lifetime requirement
171171
infer_msl_unmet_req = because this has an unmet lifetime requirement
172172
infer_msl_trait_note = this has an implicit `'static` lifetime requirement
173173
infer_msl_trait_sugg = consider relaxing the implicit `'static` requirement
174+
infer_suggest_add_let_for_letchains = consider adding `let`

compiler/rustc_infer/src/errors/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,18 @@ pub enum SourceKindMultiSuggestion<'a> {
180180
},
181181
}
182182

183+
#[derive(Subdiagnostic)]
184+
#[suggestion(
185+
infer_suggest_add_let_for_letchains,
186+
style = "verbose",
187+
applicability = "machine-applicable",
188+
code = "let "
189+
)]
190+
pub(crate) struct SuggAddLetForLetChains {
191+
#[primary_span]
192+
pub span: Span,
193+
}
194+
183195
impl<'a> SourceKindMultiSuggestion<'a> {
184196
pub fn new_fully_qualified(
185197
span: Span,

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ use crate::traits::{
5959
StatementAsExpression,
6060
};
6161

62+
use crate::errors::SuggAddLetForLetChains;
6263
use hir::intravisit::{walk_expr, walk_stmt};
6364
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
6465
use rustc_errors::{pluralize, struct_span_err, Diagnostic, ErrorGuaranteed, IntoDiagnosticArg};
@@ -2421,12 +2422,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
24212422
let mut visitor = IfVisitor { err_span: span, found_if: false, result: false };
24222423
visitor.visit_body(&body);
24232424
if visitor.result {
2424-
err.span_suggestion_verbose(
2425-
span.shrink_to_lo(),
2426-
"consider adding `let`",
2427-
"let ".to_string(),
2428-
Applicability::MachineApplicable,
2429-
);
2425+
err.subdiagnostic(SuggAddLetForLetChains{span: span.shrink_to_lo()});
24302426
}
24312427
}
24322428
}

compiler/rustc_parse/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ pub(crate) struct ExpectedExpressionFoundLet {
425425
pub(crate) struct ExpectedEqForLetExpr {
426426
#[primary_span]
427427
pub span: Span,
428-
#[suggestion_verbose(applicability = "maybe-incorrect", code = "=")]
428+
#[suggestion(applicability = "maybe-incorrect", code = "=", style = "verbose")]
429429
pub sugg_span: Span,
430430
}
431431

src/test/ui/did_you_mean/issue-103909.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ error[E0308]: mismatched types
1414
|
1515
LL | if Err(err) = File::open("hello.txt") {
1616
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
17+
|
18+
help: consider adding `let`
19+
|
20+
LL | if let Err(err) = File::open("hello.txt") {
21+
| +++
1722

1823
error: aborting due to 2 previous errors
1924

0 commit comments

Comments
 (0)