rust-clippy/tests/ui/matches.rs

335 lines
6.5 KiB
Rust
Raw Normal View History

2017-09-18 10:47:33 +00:00
#![feature(exclusive_range_pattern)]
2015-04-13 17:58:18 +00:00
2017-09-18 10:47:33 +00:00
#![warn(clippy)]
#![allow(unused, if_let_redundant_pattern_matching)]
#![warn(single_match_else)]
2015-04-13 17:58:18 +00:00
2016-01-13 00:19:27 +00:00
use std::borrow::Cow;
enum Foo { Bar, Baz(u8) }
use Foo::*;
enum ExprNode {
ExprAddrOf,
Butterflies,
Unicorns,
}
static NODE: ExprNode = ExprNode::Unicorns;
2016-06-22 00:17:26 +00:00
fn dummy() {
}
fn unwrap_addr() -> Option<&'static ExprNode> {
2016-06-22 00:17:26 +00:00
match ExprNode::Butterflies {
ExprNode::ExprAddrOf => Some(&NODE),
2016-06-22 00:17:26 +00:00
_ => { let x = 5; None },
}
}
fn single_match(){
2015-04-13 17:58:18 +00:00
let x = Some(1u8);
2016-01-13 00:19:27 +00:00
2016-06-22 00:17:26 +00:00
match x {
Some(y) => { println!("{:?}", y); }
2015-04-13 17:58:18 +00:00
_ => ()
2016-06-22 00:17:26 +00:00
};
2016-01-13 00:19:27 +00:00
2015-04-13 17:58:18 +00:00
let z = (1u8,1u8);
2016-06-22 00:17:26 +00:00
match z {
(2...3, 7...9) => dummy(),
2015-04-13 17:58:18 +00:00
_ => {}
2016-06-22 00:17:26 +00:00
};
// Not linted (pattern guards used)
match x {
Some(y) if y == 0 => println!("{:?}", y),
_ => ()
}
// Not linted (no block with statements in the single arm)
match z {
(2...3, 7...9) => println!("{:?}", z),
_ => println!("nope"),
}
2015-04-13 17:58:18 +00:00
}
2016-01-13 00:19:27 +00:00
fn single_match_know_enum() {
let x = Some(1u8);
let y : Result<_, i8> = Ok(1i8);
2016-06-22 00:17:26 +00:00
match x {
Some(y) => dummy(),
2016-01-13 00:19:27 +00:00
None => ()
2016-06-22 00:17:26 +00:00
};
2016-01-13 00:19:27 +00:00
2016-06-22 00:17:26 +00:00
match y {
Ok(y) => dummy(),
2016-01-13 00:19:27 +00:00
Err(..) => ()
2016-06-22 00:17:26 +00:00
};
2016-01-13 00:19:27 +00:00
let c = Cow::Borrowed("");
2016-06-22 00:17:26 +00:00
match c {
Cow::Borrowed(..) => dummy(),
2016-01-13 00:19:27 +00:00
Cow::Owned(..) => (),
2016-06-22 00:17:26 +00:00
};
2016-01-13 00:19:27 +00:00
let z = Foo::Bar;
// no warning
match z {
Bar => println!("42"),
Baz(_) => (),
}
match z {
Baz(_) => println!("42"),
Bar => (),
}
}
fn match_bool() {
let test: bool = true;
match test {
true => 0,
false => 42,
};
let option = 1;
match option == 1 {
2015-10-21 06:24:56 +00:00
true => 1,
false => 0,
};
2015-12-23 00:14:10 +00:00
match test {
true => (),
false => { println!("Noooo!"); }
2015-10-21 06:24:56 +00:00
};
2015-12-23 00:14:10 +00:00
match test {
2016-06-29 19:50:21 +00:00
false => { println!("Noooo!"); }
_ => (),
};
match test && test {
false => { println!("Noooo!"); }
2015-10-21 06:24:56 +00:00
_ => (),
};
2015-12-23 00:14:10 +00:00
match test {
false => { println!("Noooo!"); }
true => { println!("Yes!"); }
};
// Not linted
match option {
1 ... 10 => 1,
11 ... 20 => 2,
_ => 3,
};
}
fn ref_pats() {
2015-09-02 06:19:47 +00:00
{
let v = &Some(0);
2016-03-09 15:22:31 +00:00
match v {
2015-09-02 06:19:47 +00:00
&Some(v) => println!("{:?}", v),
&None => println!("none"),
}
match v { // this doesn't trigger, we have a different pattern
&Some(v) => println!("some"),
other => println!("other"),
}
}
let tup =& (1, 2);
2016-03-09 15:22:31 +00:00
match tup {
&(v, 1) => println!("{}", v),
_ => println!("none"),
}
// special case: using & both in expr and pats
let w = Some(0);
2016-03-09 15:22:31 +00:00
match &w {
&Some(v) => println!("{:?}", v),
&None => println!("none"),
}
// false positive: only wildcard pattern
let w = Some(0);
match w {
_ => println!("none"),
}
let a = &Some(0);
2016-03-09 15:22:31 +00:00
if let &None = a {
println!("none");
}
let b = Some(0);
2016-03-09 15:22:31 +00:00
if let &None = &b {
println!("none");
}
}
2015-12-23 00:14:10 +00:00
fn overlapping() {
const FOO : u64 = 2;
match 42 {
2017-02-08 13:58:07 +00:00
0 ... 10 => println!("0 ... 10"),
0 ... 11 => println!("0 ... 11"),
2015-12-23 00:14:10 +00:00
_ => (),
}
match 42 {
2017-02-08 13:58:07 +00:00
0 ... 5 => println!("0 ... 5"),
2015-12-23 00:14:10 +00:00
6 ... 7 => println!("6 ... 7"),
2017-02-08 13:58:07 +00:00
FOO ... 11 => println!("0 ... 11"),
2015-12-23 00:14:10 +00:00
_ => (),
}
match 42 {
2017-02-08 13:58:07 +00:00
2 => println!("2"),
0 ... 5 => println!("0 ... 5"),
_ => (),
}
match 42 {
2017-02-08 13:58:07 +00:00
2 => println!("2"),
0 ... 2 => println!("0 ... 2"),
_ => (),
}
2015-12-23 00:14:10 +00:00
match 42 {
0 ... 10 => println!("0 ... 10"),
2017-01-31 07:08:54 +00:00
11 ... 50 => println!("11 ... 50"),
2015-12-23 00:14:10 +00:00
_ => (),
}
match 42 {
2 => println!("2"),
0 .. 2 => println!("0 .. 2"),
_ => (),
}
match 42 {
2017-01-31 07:08:54 +00:00
0 .. 10 => println!("0 .. 10"),
10 .. 50 => println!("10 .. 50"),
_ => (),
}
match 42 {
2017-02-08 13:58:07 +00:00
0 .. 11 => println!("0 .. 11"),
0 ... 11 => println!("0 ... 11"),
_ => (),
}
2016-05-31 20:01:56 +00:00
if let None = Some(42) {
// nothing
} else if let None = Some(42) {
// another nothing :-)
}
2015-12-23 00:14:10 +00:00
}
2017-02-11 06:57:50 +00:00
fn match_wild_err_arm() {
let x: Result<i32, &str> = Ok(3);
match x {
Ok(3) => println!("ok"),
Ok(_) => println!("ok"),
2017-02-11 13:42:42 +00:00
Err(_) => panic!("err")
2017-02-11 06:57:50 +00:00
}
match x {
Ok(3) => println!("ok"),
Ok(_) => println!("ok"),
2017-02-11 13:42:42 +00:00
Err(_) => {panic!()}
2017-02-11 06:57:50 +00:00
}
match x {
Ok(3) => println!("ok"),
Ok(_) => println!("ok"),
2017-02-11 13:42:42 +00:00
Err(_) => {panic!();}
2017-02-11 06:57:50 +00:00
}
2017-02-11 13:42:42 +00:00
// allowed when not with `panic!` block
match x {
Ok(3) => println!("ok"),
Ok(_) => println!("ok"),
Err(_) => println!("err")
}
// allowed when used with `unreachable!`
2017-02-11 06:57:50 +00:00
match x {
Ok(3) => println!("ok"),
Ok(_) => println!("ok"),
Err(_) => {unreachable!()}
}
2017-02-11 13:42:42 +00:00
match x {
Ok(3) => println!("ok"),
Ok(_) => println!("ok"),
Err(_) => unreachable!()
}
2017-02-11 06:57:50 +00:00
match x {
Ok(3) => println!("ok"),
Ok(_) => println!("ok"),
Err(_) => {unreachable!();}
}
// no warning because of the guard
match x {
Ok(x) if x*x == 64 => println!("ok"),
Ok(_) => println!("ok"),
Err(_) => println!("err")
}
// this used to be a false positive, see #1996
2017-11-29 20:52:49 +00:00
match x {
Ok(3) => println!("ok"),
Ok(x) if x*x == 64 => println!("ok 64"),
Ok(_) => println!("ok"),
Err(_) => println!("err")
}
match (x, Some(1i32)) {
(Ok(x), Some(_)) => println!("ok {}", x),
(Ok(_), Some(x)) => println!("ok {}", x),
_ => println!("err")
}
// no warning because of the different types for x
match (x, Some(1.0f64)) {
(Ok(x), Some(_)) => println!("ok {}", x),
(Ok(_), Some(x)) => println!("ok {}", x),
_ => println!("err")
}
// because of a bug, no warning was generated for this case before #2251
match x {
Ok(_tmp) => println!("ok"),
Ok(3) => println!("ok"),
Ok(_) => println!("ok"),
Err(_) => {unreachable!();}
}
2017-02-11 06:57:50 +00:00
}
2017-12-19 22:22:16 +00:00
fn match_as_ref() {
2017-12-20 09:39:48 +00:00
let owned: Option<()> = None;
let borrowed: Option<&()> = match owned {
2017-12-19 22:22:16 +00:00
None => None,
Some(ref v) => Some(v),
};
2017-12-20 09:39:48 +00:00
let mut mut_owned: Option<()> = None;
let borrow_mut: Option<&mut ()> = match mut_owned {
2017-12-19 22:22:16 +00:00
None => None,
Some(ref mut v) => Some(v),
};
}
fn main() {
}