@@ -120,8 +120,8 @@ impl LintPass for UnusedResults {
120
120
}
121
121
122
122
impl LateLintPass for UnusedResults {
123
- fn check_stmt ( & mut self , cx : & LateContext , s : & hir:: Stmt ) {
124
- let expr = match s . node {
123
+ fn check_stmt ( & mut self , cx : & LateContext , stmt : & hir:: Stmt ) {
124
+ let expr = match stmt . node {
125
125
hir:: StmtSemi ( ref expr, _) => & * * expr,
126
126
_ => return
127
127
} ;
@@ -131,37 +131,40 @@ impl LateLintPass for UnusedResults {
131
131
}
132
132
133
133
let t = cx. tcx . expr_ty ( & expr) ;
134
- let warned = match t. sty {
134
+ match t. sty {
135
135
ty:: TyTuple ( ref tys) if tys. is_empty ( ) => return ,
136
136
ty:: TyBool => return ,
137
- ty:: TyStruct ( def, _) |
138
- ty:: TyEnum ( def, _) => {
139
- let attrs = cx. tcx . get_attrs ( def. did ) ;
140
- check_must_use ( cx, & attrs[ ..] , s. span )
137
+ _ => ( )
138
+ }
139
+ let must_use = check_must_use ( cx, t) ;
140
+ if let Some ( s) = must_use {
141
+ let mut msg = "unused result which must be used" . to_string ( ) ;
142
+ if !s. is_empty ( ) {
143
+ msg. push_str ( ": " ) ;
144
+ msg. push_str ( & s) ;
141
145
}
142
- _ => false ,
143
- } ;
144
- if !warned {
145
- cx. span_lint ( UNUSED_RESULTS , s. span , "unused result" ) ;
146
+ cx. span_lint ( UNUSED_MUST_USE , stmt. span , & msg) ;
147
+ } else {
148
+ cx. span_lint ( UNUSED_RESULTS , stmt. span , "unused result" ) ;
146
149
}
147
150
148
- fn check_must_use ( cx : & LateContext , attrs : & [ ast:: Attribute ] , sp : Span ) -> bool {
149
- for attr in attrs {
150
- if attr. check_name ( "must_use" ) {
151
- let mut msg = "unused result which must be used" . to_string ( ) ;
152
- // check for #[must_use="..."]
153
- match attr. value_str ( ) {
154
- None => { }
155
- Some ( s) => {
156
- msg. push_str ( ": " ) ;
157
- msg. push_str ( & s) ;
151
+ fn check_must_use ( cx : & LateContext , ty : ty:: Ty ) -> Option < String > {
152
+ match ty. sty {
153
+ ty:: TyStruct ( def, _) |
154
+ ty:: TyEnum ( def, _) => {
155
+ for attr in cx. tcx . get_attrs ( def. did ) . iter ( ) {
156
+ if attr. check_name ( "must_use" ) {
157
+ let s = match attr. value_str ( ) {
158
+ None => "" . to_string ( ) ,
159
+ Some ( s) => s. to_string ( )
160
+ } ;
161
+ return Some ( s) ;
158
162
}
159
163
}
160
- cx. span_lint ( UNUSED_MUST_USE , sp, & msg) ;
161
- return true ;
162
164
}
165
+ _ => ( )
163
166
}
164
- false
167
+ None
165
168
}
166
169
}
167
170
}
0 commit comments