Skip to content

Commit 246709f

Browse files
committed
run-rustfix
1 parent 45f61ea commit 246709f

File tree

4 files changed

+136
-18
lines changed

4 files changed

+136
-18
lines changed

clippy_lints/src/question_mark.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ impl QuestionMark {
5757
if Self::is_option(cx, subject);
5858

5959
then {
60-
let receiver_str = &Sugg::hir(cx, subject, "..");
60+
let mut applicability = Applicability::MachineApplicable;
61+
let receiver_str = snippet_with_applicability(cx, subject.span, "..", &mut applicability);
6162
let mut replacement: Option<String> = None;
6263
if let Some(else_) = else_ {
6364
if_chain! {
@@ -86,7 +87,7 @@ impl QuestionMark {
8687
expr.span,
8788
"replace it with",
8889
replacement_str,
89-
Applicability::MaybeIncorrect, // snippet
90+
applicability,
9091
);
9192
}
9293
)

tests/ui/question_mark.fixed

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// run-rustfix
2+
#![allow(unreachable_code)]
3+
4+
fn some_func(a: Option<u32>) -> Option<u32> {
5+
a?;
6+
7+
a
8+
}
9+
10+
fn some_other_func(a: Option<u32>) -> Option<u32> {
11+
if a.is_none() {
12+
return None;
13+
} else {
14+
return Some(0);
15+
}
16+
unreachable!()
17+
}
18+
19+
pub enum SeemsOption<T> {
20+
Some(T),
21+
None,
22+
}
23+
24+
impl<T> SeemsOption<T> {
25+
pub fn is_none(&self) -> bool {
26+
match *self {
27+
SeemsOption::None => true,
28+
SeemsOption::Some(_) => false,
29+
}
30+
}
31+
}
32+
33+
fn returns_something_similar_to_option(a: SeemsOption<u32>) -> SeemsOption<u32> {
34+
if a.is_none() {
35+
return SeemsOption::None;
36+
}
37+
38+
a
39+
}
40+
41+
pub struct CopyStruct {
42+
pub opt: Option<u32>,
43+
}
44+
45+
impl CopyStruct {
46+
#[rustfmt::skip]
47+
pub fn func(&self) -> Option<u32> {
48+
(self.opt)?;
49+
50+
self.opt?;
51+
52+
let _ = Some(self.opt?);
53+
54+
let _ = self.opt?;
55+
56+
self.opt
57+
}
58+
}
59+
60+
#[derive(Clone)]
61+
pub struct MoveStruct {
62+
pub opt: Option<Vec<u32>>,
63+
}
64+
65+
impl MoveStruct {
66+
pub fn ref_func(&self) -> Option<Vec<u32>> {
67+
self.opt.as_ref()?;
68+
69+
self.opt.clone()
70+
}
71+
72+
pub fn mov_func_reuse(self) -> Option<Vec<u32>> {
73+
self.opt.as_ref()?;
74+
75+
self.opt
76+
}
77+
78+
pub fn mov_func_no_use(self) -> Option<Vec<u32>> {
79+
self.opt.as_ref()?;
80+
Some(Vec::new())
81+
}
82+
83+
pub fn if_let_ref_func(self) -> Option<Vec<u32>> {
84+
let v: &Vec<_> = self.opt.as_ref()?;
85+
86+
Some(v.clone())
87+
}
88+
89+
pub fn if_let_mov_func(self) -> Option<Vec<u32>> {
90+
let v = self.opt?;
91+
92+
Some(v)
93+
}
94+
}
95+
96+
fn main() {
97+
some_func(Some(42));
98+
some_func(None);
99+
some_other_func(Some(42));
100+
101+
let copy_struct = CopyStruct { opt: Some(54) };
102+
copy_struct.func();
103+
104+
let move_struct = MoveStruct {
105+
opt: Some(vec![42, 1337]),
106+
};
107+
move_struct.ref_func();
108+
move_struct.clone().mov_func_reuse();
109+
move_struct.mov_func_no_use();
110+
111+
let so = SeemsOption::Some(45);
112+
returns_something_similar_to_option(so);
113+
}

tests/ui/question_mark.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// run-rustfix
2+
#![allow(unreachable_code)]
3+
14
fn some_func(a: Option<u32>) -> Option<u32> {
25
if a.is_none() {
36
return None;
@@ -98,7 +101,7 @@ impl MoveStruct {
98101
}
99102

100103
pub fn if_let_ref_func(self) -> Option<Vec<u32>> {
101-
let mut v: &Vec<_> = if let Some(ref v) = self.opt {
104+
let v: &Vec<_> = if let Some(ref v) = self.opt {
102105
v
103106
} else {
104107
return None;
@@ -108,7 +111,7 @@ impl MoveStruct {
108111
}
109112

110113
pub fn if_let_mov_func(self) -> Option<Vec<u32>> {
111-
let mut v = if let Some(v) = self.opt {
114+
let v = if let Some(v) = self.opt {
112115
v
113116
} else {
114117
return None;
@@ -121,6 +124,7 @@ impl MoveStruct {
121124
fn main() {
122125
some_func(Some(42));
123126
some_func(None);
127+
some_other_func(Some(42));
124128

125129
let copy_struct = CopyStruct { opt: Some(54) };
126130
copy_struct.func();

tests/ui/question_mark.stderr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this block may be rewritten with the `?` operator
2-
--> $DIR/question_mark.rs:2:5
2+
--> $DIR/question_mark.rs:5:5
33
|
44
LL | / if a.is_none() {
55
LL | | return None;
@@ -9,23 +9,23 @@ LL | | }
99
= note: `-D clippy::question-mark` implied by `-D warnings`
1010

1111
error: this block may be rewritten with the `?` operator
12-
--> $DIR/question_mark.rs:47:9
12+
--> $DIR/question_mark.rs:50:9
1313
|
1414
LL | / if (self.opt).is_none() {
1515
LL | | return None;
1616
LL | | }
1717
| |_________^ help: replace it with: `(self.opt)?;`
1818

1919
error: this block may be rewritten with the `?` operator
20-
--> $DIR/question_mark.rs:51:9
20+
--> $DIR/question_mark.rs:54:9
2121
|
2222
LL | / if self.opt.is_none() {
2323
LL | | return None
2424
LL | | }
2525
| |_________^ help: replace it with: `self.opt?;`
2626

2727
error: this block may be rewritten with the `?` operator
28-
--> $DIR/question_mark.rs:55:17
28+
--> $DIR/question_mark.rs:58:17
2929
|
3030
LL | let _ = if self.opt.is_none() {
3131
| _________________^
@@ -36,7 +36,7 @@ LL | | };
3636
| |_________^ help: replace it with: `Some(self.opt?)`
3737

3838
error: this if-let-else may be rewritten with the `?` operator
39-
--> $DIR/question_mark.rs:61:17
39+
--> $DIR/question_mark.rs:64:17
4040
|
4141
LL | let _ = if let Some(x) = self.opt {
4242
| _________________^
@@ -47,45 +47,45 @@ LL | | };
4747
| |_________^ help: replace it with: `self.opt?`
4848

4949
error: this block may be rewritten with the `?` operator
50-
--> $DIR/question_mark.rs:78:9
50+
--> $DIR/question_mark.rs:81:9
5151
|
5252
LL | / if self.opt.is_none() {
5353
LL | | return None;
5454
LL | | }
5555
| |_________^ help: replace it with: `self.opt.as_ref()?;`
5656

5757
error: this block may be rewritten with the `?` operator
58-
--> $DIR/question_mark.rs:86:9
58+
--> $DIR/question_mark.rs:89:9
5959
|
6060
LL | / if self.opt.is_none() {
6161
LL | | return None;
6262
LL | | }
6363
| |_________^ help: replace it with: `self.opt.as_ref()?;`
6464

6565
error: this block may be rewritten with the `?` operator
66-
--> $DIR/question_mark.rs:94:9
66+
--> $DIR/question_mark.rs:97:9
6767
|
6868
LL | / if self.opt.is_none() {
6969
LL | | return None;
7070
LL | | }
7171
| |_________^ help: replace it with: `self.opt.as_ref()?;`
7272

7373
error: this if-let-else may be rewritten with the `?` operator
74-
--> $DIR/question_mark.rs:101:30
74+
--> $DIR/question_mark.rs:104:26
7575
|
76-
LL | let mut v: &Vec<_> = if let Some(ref v) = self.opt {
77-
| ______________________________^
76+
LL | let v: &Vec<_> = if let Some(ref v) = self.opt {
77+
| __________________________^
7878
LL | | v
7979
LL | | } else {
8080
LL | | return None;
8181
LL | | };
8282
| |_________^ help: replace it with: `self.opt.as_ref()?`
8383

8484
error: this if-let-else may be rewritten with the `?` operator
85-
--> $DIR/question_mark.rs:111:21
85+
--> $DIR/question_mark.rs:114:17
8686
|
87-
LL | let mut v = if let Some(v) = self.opt {
88-
| _____________________^
87+
LL | let v = if let Some(v) = self.opt {
88+
| _________________^
8989
LL | | v
9090
LL | | } else {
9191
LL | | return None;

0 commit comments

Comments
 (0)