Skip to content

Commit 4f77d72

Browse files
committed
gccrs: add overlapping range endpoints lint
Warn when two range patterns in a match overlap on a single endpoint, for example `0..=5` and `5..=10` which both match `5`. gcc/rust/ChangeLog: * checks/lints/unused/rust-unused-checker.cc (UnusedChecker::visit(HIR::MatchExpr)): Warn on overlapping range endpoints. * rust-lang.cc (grs_langhook_init_options_struct): Enable warn_unused. gcc/testsuite/ChangeLog: * rust/compile/overlapping-range-endpoints_0.rs: New test. Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.com>
1 parent 76397e8 commit 4f77d72

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

gcc/rust/checks/lints/unused/rust-unused-checker.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,21 @@ UnusedChecker::visit (HIR::MatchExpr &expr)
331331
"multiple ranges are one apart");
332332
}
333333

334+
// An inclusive range whose upper endpoint equals another range's lower
335+
// endpoint overlaps it on that single point.
336+
for (size_t i = 0; i < ranges.size (); i++)
337+
{
338+
if (!ranges[i].inclusive)
339+
continue;
340+
for (size_t j = 0; j < ranges.size (); j++)
341+
if (i != j && ranges[i].hi == ranges[j].lo)
342+
{
343+
rust_warning_at (ranges[i].locus, OPT_Wunused,
344+
"multiple patterns overlap on their endpoints");
345+
break;
346+
}
347+
}
348+
334349
walk (expr);
335350
}
336351

gcc/rust/rust-lang.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ grs_langhook_init_options_struct (struct gcc_options *opts)
136136

137137
/* We need to warn on unused variables by default */
138138
opts->x_warn_unused_variable = 1;
139+
/* Experimental lints under -frust-unused-check-2.0 warn by default */
140+
opts->x_warn_unused = 1;
139141
/* For const variables too */
140142
opts->x_warn_unused_const_variable = 1;
141143
/* And finally unused result for #[must_use] */
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// { dg-additional-options "-frust-unused-check-2.0" }
2+
#![feature(no_core, lang_items)]
3+
#![no_core]
4+
5+
#[lang = "sized"]
6+
pub trait Sized {}
7+
8+
pub fn f(x: i32) {
9+
match x {
10+
0..=5 => {}
11+
// { dg-warning "multiple patterns overlap on their endpoints" "" { target *-*-* } .-1 }
12+
5..=10 => {}
13+
_ => {}
14+
}
15+
}

0 commit comments

Comments
 (0)