Skip to content

Commit 230355f

Browse files
committed
comments and factor to own method
1 parent 1d93048 commit 230355f

File tree

2 files changed

+38
-36
lines changed

2 files changed

+38
-36
lines changed

compiler/rustc_typeck/src/check/pat.rs

+36-34
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::check::FnCtxt;
22
use rustc_ast as ast;
3+
34
use rustc_ast::util::lev_distance::find_best_match_for_name;
45
use rustc_data_structures::fx::FxHashMap;
56
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
@@ -740,6 +741,40 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
740741
pat_ty
741742
}
742743

744+
fn maybe_suggest_range_literal(
745+
&self,
746+
e: &mut DiagnosticBuilder<'_>,
747+
opt_def_id: Option<hir::def_id::DefId>,
748+
ident: Ident,
749+
) -> bool {
750+
match opt_def_id {
751+
Some(def_id) => match self.tcx.hir().get_if_local(def_id) {
752+
Some(hir::Node::Item(hir::Item {
753+
kind: hir::ItemKind::Const(_, body_id), ..
754+
})) => match self.tcx.hir().get(body_id.hir_id) {
755+
hir::Node::Expr(expr) => {
756+
if hir::is_range_literal(expr) {
757+
let span = self.tcx.hir().span(body_id.hir_id);
758+
if let Ok(snip) = self.tcx.sess.source_map().span_to_snippet(span) {
759+
e.span_suggestion_verbose(
760+
ident.span,
761+
"you may want to move the range into the match block",
762+
snip,
763+
Applicability::MachineApplicable,
764+
);
765+
return true;
766+
}
767+
}
768+
}
769+
_ => (),
770+
},
771+
_ => (),
772+
},
773+
_ => (),
774+
}
775+
false
776+
}
777+
743778
fn emit_bad_pat_path(
744779
&self,
745780
mut e: DiagnosticBuilder<'_>,
@@ -789,40 +824,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
789824
self.tcx.lang_items().range_to_inclusive_struct(),
790825
];
791826
if type_def_id != None && ranges.contains(&type_def_id) {
792-
let generic_message = match item_def_id {
793-
Some(def_id) => match self.tcx.hir().get_if_local(def_id) {
794-
Some(hir::Node::Item(hir::Item {
795-
kind: hir::ItemKind::Const(_, body_id),
796-
..
797-
})) => match self.tcx.hir().get(body_id.hir_id) {
798-
hir::Node::Expr(expr) => {
799-
if hir::is_range_literal(expr) {
800-
let span = self.tcx.hir().span(body_id.hir_id);
801-
if let Ok(snip) =
802-
self.tcx.sess.source_map().span_to_snippet(span)
803-
{
804-
e.span_suggestion_verbose(
805-
span,
806-
"you may want to move the range into the match block",
807-
snip,
808-
Applicability::MachineApplicable
809-
);
810-
false
811-
} else {
812-
true
813-
}
814-
} else {
815-
true
816-
}
817-
}
818-
_ => true,
819-
},
820-
_ => true,
821-
},
822-
_ => true,
823-
};
824-
825-
if generic_message {
827+
if !self.maybe_suggest_range_literal(&mut e, item_def_id, *ident) {
826828
let msg = "constants only support matching by type, \
827829
if you meant to match against a range of values, \
828830
consider using a range pattern like `min ..= max` in the match block";

src/test/ui/issues/issue-76191.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ LL | RANGE => {}
1616
found struct `RangeInclusive<i32>`
1717
help: you may want to move the range into the match block
1818
|
19-
LL | const RANGE: RangeInclusive<i32> = 0..=255;
20-
| ^^^^^^^
19+
LL | 0..=255 => {}
20+
| ^^^^^^^
2121

2222
error[E0308]: mismatched types
2323
--> $DIR/issue-76191.rs:15:9

0 commit comments

Comments
 (0)