Skip to content

Commit ee0856c

Browse files
committed
Recomend .as_ref()? in certain situations
1 parent 8b0ea22 commit ee0856c

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
lines changed

clippy_lints/src/question_mark.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ impl Pass {
8484
replacement_str = format!("Some({}?)", receiver_str);
8585
}
8686
}
87+
} else if Self::moves_by_default(cx, subject) {
88+
replacement_str = format!("{}.as_ref()?;", receiver_str);
8789
} else {
8890
replacement_str = format!("{}?;", receiver_str);
8991
}
@@ -105,6 +107,12 @@ impl Pass {
105107
}
106108
}
107109

110+
fn moves_by_default(cx: &LateContext<'_, '_>, expression: &Expr) -> bool {
111+
let expr_ty = cx.tables.expr_ty(expression);
112+
113+
expr_ty.moves_by_default(cx.tcx, cx.param_env, expression.span)
114+
}
115+
108116
fn is_option(cx: &LateContext<'_, '_>, expression: &Expr) -> bool {
109117
let expr_ty = cx.tables.expr_ty(expression);
110118

tests/ui/question_mark.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ fn returns_something_similar_to_option(a: SeemsOption<u32>) -> SeemsOption<u32>
3737
a
3838
}
3939

40-
pub struct SomeStruct {
40+
pub struct CopyStruct {
4141
pub opt: Option<u32>,
4242
}
4343

44-
impl SomeStruct {
44+
impl CopyStruct {
4545
#[rustfmt::skip]
4646
pub fn func(&self) -> Option<u32> {
4747
if (self.opt).is_none() {
@@ -62,12 +62,49 @@ impl SomeStruct {
6262
}
6363
}
6464

65+
#[derive(Clone)]
66+
pub struct MoveStruct {
67+
pub opt: Option<Vec<u32>>,
68+
}
69+
70+
impl MoveStruct {
71+
pub fn ref_func(&self) -> Option<Vec<u32>> {
72+
if self.opt.is_none() {
73+
return None;
74+
}
75+
76+
self.opt.clone()
77+
}
78+
79+
pub fn mov_func_reuse(self) -> Option<Vec<u32>> {
80+
if self.opt.is_none() {
81+
return None;
82+
}
83+
84+
self.opt
85+
}
86+
87+
pub fn mov_func_no_use(self) -> Option<Vec<u32>> {
88+
if self.opt.is_none() {
89+
return None;
90+
}
91+
Some(Vec::new())
92+
}
93+
}
94+
6595
fn main() {
6696
some_func(Some(42));
6797
some_func(None);
6898

69-
let some_struct = SomeStruct { opt: Some(54) };
70-
some_struct.func();
99+
let copy_struct = CopyStruct { opt: Some(54) };
100+
copy_struct.func();
101+
102+
let move_struct = MoveStruct {
103+
opt: Some(vec![42, 1337]),
104+
};
105+
move_struct.ref_func();
106+
move_struct.clone().mov_func_reuse();
107+
move_struct.clone().mov_func_no_use();
71108

72109
let so = SeemsOption::Some(45);
73110
returns_something_similar_to_option(so);

tests/ui/question_mark.stderr

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,29 @@ error: this block may be rewritten with the `?` operator
3535
59 | | };
3636
| |_________^ help: replace_it_with: `Some(self.opt?)`
3737

38-
error: aborting due to 4 previous errors
38+
error: this block may be rewritten with the `?` operator
39+
--> $DIR/question_mark.rs:72:9
40+
|
41+
72 | / if self.opt.is_none() {
42+
73 | | return None;
43+
74 | | }
44+
| |_________^ help: replace_it_with: `self.opt.as_ref()?;`
45+
46+
error: this block may be rewritten with the `?` operator
47+
--> $DIR/question_mark.rs:80:9
48+
|
49+
80 | / if self.opt.is_none() {
50+
81 | | return None;
51+
82 | | }
52+
| |_________^ help: replace_it_with: `self.opt.as_ref()?;`
53+
54+
error: this block may be rewritten with the `?` operator
55+
--> $DIR/question_mark.rs:88:9
56+
|
57+
88 | / if self.opt.is_none() {
58+
89 | | return None;
59+
90 | | }
60+
| |_________^ help: replace_it_with: `self.opt.as_ref()?;`
61+
62+
error: aborting due to 7 previous errors
3963

0 commit comments

Comments
 (0)