Skip to content

Commit ed935de

Browse files
committed
Run-rustfix
1 parent 68df61e commit ed935de

File tree

4 files changed

+385
-3
lines changed

4 files changed

+385
-3
lines changed

tests/ui/single_match.fixed

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
//@run-rustfix
2+
#![warn(clippy::single_match)]
3+
#![allow(unused, clippy::uninlined_format_args, clippy::redundant_pattern_matching)]
4+
fn dummy() {}
5+
6+
fn single_match() {
7+
let x = Some(1u8);
8+
9+
if let Some(y) = x {
10+
println!("{:?}", y);
11+
};
12+
13+
let x = Some(1u8);
14+
if let Some(y) = x { println!("{:?}", y) }
15+
16+
let z = (1u8, 1u8);
17+
if let (2..=3, 7..=9) = z { dummy() };
18+
19+
// Not linted (pattern guards used)
20+
match x {
21+
Some(y) if y == 0 => println!("{:?}", y),
22+
_ => (),
23+
}
24+
25+
// Not linted (no block with statements in the single arm)
26+
match z {
27+
(2..=3, 7..=9) => println!("{:?}", z),
28+
_ => println!("nope"),
29+
}
30+
}
31+
32+
enum Foo {
33+
Bar,
34+
Baz(u8),
35+
}
36+
use std::borrow::Cow;
37+
use Foo::*;
38+
39+
fn single_match_know_enum() {
40+
let x = Some(1u8);
41+
let y: Result<_, i8> = Ok(1i8);
42+
43+
if let Some(y) = x { dummy() };
44+
45+
if let Ok(y) = y { dummy() };
46+
47+
let c = Cow::Borrowed("");
48+
49+
if let Cow::Borrowed(..) = c { dummy() };
50+
51+
let z = Foo::Bar;
52+
// no warning
53+
match z {
54+
Bar => println!("42"),
55+
Baz(_) => (),
56+
}
57+
58+
match z {
59+
Baz(_) => println!("42"),
60+
Bar => (),
61+
}
62+
}
63+
64+
// issue #173
65+
fn if_suggestion() {
66+
let x = "test";
67+
if x == "test" { println!() }
68+
69+
#[derive(PartialEq, Eq)]
70+
enum Foo {
71+
A,
72+
B,
73+
C(u32),
74+
}
75+
76+
let x = Foo::A;
77+
if x == Foo::A { println!() }
78+
79+
const FOO_C: Foo = Foo::C(0);
80+
if x == FOO_C { println!() }
81+
82+
if x == Foo::A { println!() }
83+
84+
let x = &x;
85+
if x == &Foo::A { println!() }
86+
87+
enum Bar {
88+
A,
89+
B,
90+
}
91+
impl PartialEq for Bar {
92+
fn eq(&self, rhs: &Self) -> bool {
93+
matches!((self, rhs), (Self::A, Self::A) | (Self::B, Self::B))
94+
}
95+
}
96+
impl Eq for Bar {}
97+
98+
let x = Bar::A;
99+
if let Bar::A = x { println!() }
100+
101+
// issue #7038
102+
struct X;
103+
let x = Some(X);
104+
if let None = x { println!() };
105+
}
106+
107+
// See: issue #8282
108+
fn ranges() {
109+
enum E {
110+
V,
111+
}
112+
let x = (Some(E::V), Some(42));
113+
114+
// Don't lint, because the `E` enum can be extended with additional fields later. Thus, the
115+
// proposed replacement to `if let Some(E::V)` may hide non-exhaustive warnings that appeared
116+
// because of `match` construction.
117+
match x {
118+
(Some(E::V), _) => {},
119+
(None, _) => {},
120+
}
121+
122+
// lint
123+
if let (Some(_), _) = x {}
124+
125+
// lint
126+
if let (Some(E::V), _) = x { todo!() }
127+
128+
// lint
129+
if let (.., Some(E::V), _) = (Some(42), Some(E::V), Some(42)) {}
130+
131+
// Don't lint, see above.
132+
match (Some(E::V), Some(E::V), Some(E::V)) {
133+
(.., Some(E::V), _) => {},
134+
(.., None, _) => {},
135+
}
136+
137+
// Don't lint, see above.
138+
match (Some(E::V), Some(E::V), Some(E::V)) {
139+
(Some(E::V), ..) => {},
140+
(None, ..) => {},
141+
}
142+
143+
// Don't lint, see above.
144+
match (Some(E::V), Some(E::V), Some(E::V)) {
145+
(_, Some(E::V), ..) => {},
146+
(_, None, ..) => {},
147+
}
148+
}
149+
150+
fn skip_type_aliases() {
151+
enum OptionEx {
152+
Some(i32),
153+
None,
154+
}
155+
enum ResultEx {
156+
Err(i32),
157+
Ok(i32),
158+
}
159+
160+
use OptionEx::{None, Some};
161+
use ResultEx::{Err, Ok};
162+
163+
// don't lint
164+
match Err(42) {
165+
Ok(_) => dummy(),
166+
Err(_) => (),
167+
};
168+
169+
// don't lint
170+
match Some(1i32) {
171+
Some(_) => dummy(),
172+
None => (),
173+
};
174+
}
175+
176+
macro_rules! single_match {
177+
($num:literal) => {
178+
match $num {
179+
15 => println!("15"),
180+
_ => (),
181+
}
182+
};
183+
}
184+
185+
fn main() {
186+
single_match!(5);
187+
188+
// Don't lint
189+
let _ = match Some(0) {
190+
#[cfg(feature = "foo")]
191+
Some(10) => 11,
192+
Some(x) => x,
193+
_ => 0,
194+
};
195+
}
196+
197+
fn issue_10808(bar: Option<i32>) {
198+
if let Some(v) = bar { unsafe {
199+
let r = &v as *const i32;
200+
println!("{}", *r);
201+
} }
202+
203+
if let Some(v) = bar {
204+
unsafe {
205+
let r = &v as *const i32;
206+
println!("{}", *r);
207+
}
208+
}
209+
}

tests/ui/single_match.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
//@run-rustfix
12
#![warn(clippy::single_match)]
2-
#![allow(unused, clippy::uninlined_format_args)]
3-
3+
#![allow(unused, clippy::uninlined_format_args, clippy::redundant_pattern_matching)]
44
fn dummy() {}
55

66
fn single_match() {

tests/ui/single_match_else.fixed

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
//@run-rustfix
2+
//@aux-build: proc_macros.rs
3+
#![warn(clippy::single_match_else)]
4+
#![allow(unused, clippy::needless_return, clippy::no_effect, clippy::uninlined_format_args)]
5+
extern crate proc_macros;
6+
use proc_macros::with_span;
7+
8+
enum ExprNode {
9+
ExprAddrOf,
10+
Butterflies,
11+
Unicorns,
12+
}
13+
14+
static NODE: ExprNode = ExprNode::Unicorns;
15+
16+
fn unwrap_addr() -> Option<&'static ExprNode> {
17+
let _ = if let ExprNode::ExprAddrOf = ExprNode::Butterflies { Some(&NODE) } else {
18+
let x = 5;
19+
None
20+
};
21+
22+
// Don't lint
23+
with_span!(span match ExprNode::Butterflies {
24+
ExprNode::ExprAddrOf => Some(&NODE),
25+
_ => {
26+
let x = 5;
27+
None
28+
},
29+
})
30+
}
31+
32+
macro_rules! unwrap_addr {
33+
($expression:expr) => {
34+
match $expression {
35+
ExprNode::ExprAddrOf => Some(&NODE),
36+
_ => {
37+
let x = 5;
38+
None
39+
},
40+
}
41+
};
42+
}
43+
44+
#[rustfmt::skip]
45+
fn main() {
46+
unwrap_addr!(ExprNode::Unicorns);
47+
48+
//
49+
// don't lint single exprs/statements
50+
//
51+
52+
// don't lint here
53+
match Some(1) {
54+
Some(a) => println!("${:?}", a),
55+
None => return,
56+
}
57+
58+
// don't lint here
59+
match Some(1) {
60+
Some(a) => println!("${:?}", a),
61+
None => {
62+
return
63+
},
64+
}
65+
66+
// don't lint here
67+
match Some(1) {
68+
Some(a) => println!("${:?}", a),
69+
None => {
70+
return;
71+
},
72+
}
73+
74+
//
75+
// lint multiple exprs/statements "else" blocks
76+
//
77+
78+
// lint here
79+
if let Some(a) = Some(1) { println!("${:?}", a) } else {
80+
println!("else block");
81+
return
82+
}
83+
84+
// lint here
85+
if let Some(a) = Some(1) { println!("${:?}", a) } else {
86+
println!("else block");
87+
return;
88+
}
89+
90+
// lint here
91+
use std::convert::Infallible;
92+
if let Ok(a) = Result::<i32, Infallible>::Ok(1) { println!("${:?}", a) } else {
93+
println!("else block");
94+
return;
95+
}
96+
97+
use std::borrow::Cow;
98+
if let Cow::Owned(a) = Cow::from("moo") { println!("${:?}", a) } else {
99+
println!("else block");
100+
return;
101+
}
102+
}
103+
104+
fn issue_10808(bar: Option<i32>) {
105+
if let Some(v) = bar { unsafe {
106+
let r = &v as *const i32;
107+
println!("{}", *r);
108+
} } else {
109+
println!("None1");
110+
println!("None2");
111+
}
112+
113+
if let Some(v) = bar {
114+
println!("Some");
115+
println!("{v}");
116+
} else { unsafe {
117+
let v = 0;
118+
let r = &v as *const i32;
119+
println!("{}", *r);
120+
} }
121+
122+
if let Some(v) = bar { unsafe {
123+
let r = &v as *const i32;
124+
println!("{}", *r);
125+
} } else { unsafe {
126+
let v = 0;
127+
let r = &v as *const i32;
128+
println!("{}", *r);
129+
} }
130+
131+
if let Some(v) = bar {
132+
unsafe {
133+
let r = &v as *const i32;
134+
println!("{}", *r);
135+
}
136+
} else {
137+
println!("None");
138+
println!("None");
139+
}
140+
141+
match bar {
142+
Some(v) => {
143+
println!("Some");
144+
println!("{v}");
145+
},
146+
#[rustfmt::skip]
147+
None => {
148+
unsafe {
149+
let v = 0;
150+
let r = &v as *const i32;
151+
println!("{}", *r);
152+
}
153+
},
154+
}
155+
156+
match bar {
157+
#[rustfmt::skip]
158+
Some(v) => {
159+
unsafe {
160+
let r = &v as *const i32;
161+
println!("{}", *r);
162+
}
163+
},
164+
#[rustfmt::skip]
165+
None => {
166+
unsafe {
167+
let v = 0;
168+
let r = &v as *const i32;
169+
println!("{}", *r);
170+
}
171+
},
172+
}
173+
}

0 commit comments

Comments
 (0)