Handle irrufutable or unreachable let-else · rust-lang/rust@df9a2e0 (original) (raw)
`@@ -17,7 +17,7 @@ use rustc_middle::ty::{self, Ty, TyCtxt};
`
17
17
`use rustc_session::lint::builtin::BINDINGS_WITH_VARIANT_NAME;
`
18
18
`use rustc_session::lint::builtin::{IRREFUTABLE_LET_PATTERNS, UNREACHABLE_PATTERNS};
`
19
19
`use rustc_session::Session;
`
20
``
`-
use rustc_span::Span;
`
``
20
`+
use rustc_span::{DesugaringKind, ExpnKind, Span};
`
21
21
`use std::slice;
`
22
22
``
23
23
`crate fn check_match(tcx: TyCtxt<'_>, def_id: DefId) {
`
`@@ -381,6 +381,10 @@ fn irrefutable_let_pattern(tcx: TyCtxt<'_>, id: HirId, span: Span) {
`
381
381
`}
`
382
382
``
383
383
`let source = let_source(tcx, id);
`
``
384
`+
let span = match source {
`
``
385
`+
LetSource::LetElse(span) => span,
`
``
386
`+
_ => span,
`
``
387
`+
};
`
384
388
` tcx.struct_span_lint_hir(IRREFUTABLE_LET_PATTERNS, id, span, |lint| match source {
`
385
389
`LetSource::GenericLet => {
`
386
390
`` emit_diag!(lint, "let
", "let
is useless", "removing let
");
``
`@@ -401,6 +405,14 @@ fn irrefutable_let_pattern(tcx: TyCtxt<'_>, id: HirId, span: Span) {
`
401
405
`` "removing the guard and adding a let
inside the match arm"
``
402
406
`);
`
403
407
`}
`
``
408
`+
LetSource::LetElse(..) => {
`
``
409
`+
emit_diag!(
`
``
410
`+
lint,
`
``
411
`` +
"let...else
",
``
``
412
`` +
"else
clause is useless",
``
``
413
`` +
"removing the else
clause"
``
``
414
`+
);
`
``
415
`+
}
`
404
416
`LetSource::WhileLet => {
`
405
417
`emit_diag!(
`
406
418
` lint,
`
`@@ -755,6 +767,7 @@ pub enum LetSource {
`
755
767
`GenericLet,
`
756
768
`IfLet,
`
757
769
`IfLetGuard,
`
``
770
`+
LetElse(Span),
`
758
771
`WhileLet,
`
759
772
`}
`
760
773
``
`@@ -768,6 +781,12 @@ fn let_source(tcx: TyCtxt<'_>, pat_id: HirId) -> LetSource {
`
768
781
`}) if hir_id == pat_id => {
`
769
782
`return LetSource::IfLetGuard;
`
770
783
`}
`
``
784
`+
hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Let(..), span, .. }) => {
`
``
785
`+
let expn_data = span.ctxt().outer_expn_data();
`
``
786
`+
if let ExpnKind::Desugaring(DesugaringKind::LetElse) = expn_data.kind {
`
``
787
`+
return LetSource::LetElse(expn_data.call_site);
`
``
788
`+
}
`
``
789
`+
}
`
771
790
` _ => {}
`
772
791
`}
`
773
792
`let parent_parent = hir.get_parent_node(parent);
`