rust/tests/ui/single_match.rs

94 lines
1.9 KiB
Rust
Raw Normal View History

2018-10-06 18:18:06 +02:00
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
2018-07-28 17:34:52 +02:00
#![warn(clippy::single_match)]
2018-12-09 23:26:16 +01:00
fn dummy() {}
2018-12-09 23:26:16 +01:00
fn single_match() {
let x = Some(1u8);
match x {
2018-12-09 23:26:16 +01:00
Some(y) => {
println!("{:?}", y);
},
_ => (),
};
let x = Some(1u8);
match x {
// Note the missing block braces.
// We suggest `if let Some(y) = x { .. }` because the macro
// is expanded before we can do anything.
Some(y) => println!("{:?}", y),
2018-12-09 23:26:16 +01:00
_ => (),
}
2018-12-09 23:26:16 +01:00
let z = (1u8, 1u8);
match z {
(2...3, 7...9) => dummy(),
2018-12-09 23:26:16 +01:00
_ => {},
};
// Not linted (pattern guards used)
match x {
Some(y) if y == 0 => println!("{:?}", y),
2018-12-09 23:26:16 +01:00
_ => (),
}
// Not linted (no block with statements in the single arm)
match z {
(2...3, 7...9) => println!("{:?}", z),
_ => println!("nope"),
}
}
2018-12-09 23:26:16 +01:00
enum Foo {
Bar,
Baz(u8),
}
use std::borrow::Cow;
2018-12-09 23:26:16 +01:00
use Foo::*;
fn single_match_know_enum() {
let x = Some(1u8);
2018-12-09 23:26:16 +01:00
let y: Result<_, i8> = Ok(1i8);
match x {
Some(y) => dummy(),
2018-12-09 23:26:16 +01:00
None => (),
};
match y {
Ok(y) => dummy(),
2018-12-09 23:26:16 +01:00
Err(..) => (),
};
let c = Cow::Borrowed("");
match c {
Cow::Borrowed(..) => dummy(),
Cow::Owned(..) => (),
};
let z = Foo::Bar;
// no warning
match z {
Bar => println!("42"),
Baz(_) => (),
}
match z {
Baz(_) => println!("42"),
Bar => (),
}
}
2018-12-09 23:26:16 +01:00
fn main() {}