File tree Expand file tree Collapse file tree 3 files changed +74
-5
lines changed Expand file tree Collapse file tree 3 files changed +74
-5
lines changed Original file line number Diff line number Diff line change @@ -84,6 +84,8 @@ impl Pass {
84
84
replacement_str = format!( "Some({}?)" , receiver_str) ;
85
85
}
86
86
}
87
+ } else if Self :: moves_by_default( cx, subject) {
88
+ replacement_str = format!( "{}.as_ref()?;" , receiver_str) ;
87
89
} else {
88
90
replacement_str = format!( "{}?;" , receiver_str) ;
89
91
}
@@ -105,6 +107,12 @@ impl Pass {
105
107
}
106
108
}
107
109
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
+
108
116
fn is_option ( cx : & LateContext < ' _ , ' _ > , expression : & Expr ) -> bool {
109
117
let expr_ty = cx. tables . expr_ty ( expression) ;
110
118
Original file line number Diff line number Diff line change @@ -37,11 +37,11 @@ fn returns_something_similar_to_option(a: SeemsOption<u32>) -> SeemsOption<u32>
37
37
a
38
38
}
39
39
40
- pub struct SomeStruct {
40
+ pub struct CopyStruct {
41
41
pub opt : Option < u32 > ,
42
42
}
43
43
44
- impl SomeStruct {
44
+ impl CopyStruct {
45
45
#[ rustfmt:: skip]
46
46
pub fn func ( & self ) -> Option < u32 > {
47
47
if ( self . opt ) . is_none ( ) {
@@ -62,12 +62,49 @@ impl SomeStruct {
62
62
}
63
63
}
64
64
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
+
65
95
fn main ( ) {
66
96
some_func ( Some ( 42 ) ) ;
67
97
some_func ( None ) ;
68
98
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 ( ) ;
71
108
72
109
let so = SeemsOption :: Some ( 45 ) ;
73
110
returns_something_similar_to_option ( so) ;
Original file line number Diff line number Diff line change @@ -35,5 +35,29 @@ error: this block may be rewritten with the `?` operator
35
35
59 | | };
36
36
| |_________^ help: replace_it_with: `Some(self.opt?)`
37
37
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
39
63
You can’t perform that action at this time.
0 commit comments