@@ -180,12 +180,12 @@ fn collect_unwrap_info<'tcx>(
180
180
Vec :: new ( )
181
181
}
182
182
183
- /// A HIR visitor delegate that checks if a local variable of type `Option<_> ` is mutated,
184
- /// *except* for if `Option:: as_mut` is called.
183
+ /// A HIR visitor delegate that checks if a local variable of type `Option` or `Result ` is mutated,
184
+ /// *except* for if `. as_mut() ` is called.
185
185
/// The reason for why we allow that one specifically is that `.as_mut()` cannot change
186
- /// the option to `None` , and that is important because this lint relies on the fact that
186
+ /// the variant , and that is important because this lint relies on the fact that
187
187
/// `is_some` + `unwrap` is equivalent to `if let Some(..) = ..`, which it would not be if
188
- /// the option is changed to None between `is_some` and `unwrap`.
188
+ /// the option is changed to None between `is_some` and `unwrap`, ditto for `Result` .
189
189
/// (And also `.as_mut()` is a somewhat common method that is still worth linting on.)
190
190
struct MutationVisitor < ' tcx > {
191
191
is_mutated : bool ,
@@ -194,13 +194,13 @@ struct MutationVisitor<'tcx> {
194
194
}
195
195
196
196
/// Checks if the parent of the expression pointed at by the given `HirId` is a call to
197
- /// `Option:: as_mut`.
197
+ /// `. as_mut() `.
198
198
///
199
199
/// Used by the mutation visitor to specifically allow `.as_mut()` calls.
200
200
/// In particular, the `HirId` that the visitor receives is the id of the local expression
201
201
/// (i.e. the `x` in `x.as_mut()`), and that is the reason for why we care about its parent
202
202
/// expression: that will be where the actual method call is.
203
- fn is_option_as_mut_use ( tcx : TyCtxt < ' _ > , expr_id : HirId ) -> bool {
203
+ fn is_as_mut_use ( tcx : TyCtxt < ' _ > , expr_id : HirId ) -> bool {
204
204
if let Node :: Expr ( mutating_expr) = tcx. parent_hir_node ( expr_id)
205
205
&& let ExprKind :: MethodCall ( path, _, [ ] , _) = mutating_expr. kind
206
206
{
@@ -214,7 +214,7 @@ impl<'tcx> Delegate<'tcx> for MutationVisitor<'tcx> {
214
214
fn borrow ( & mut self , cat : & PlaceWithHirId < ' tcx > , diag_expr_id : HirId , bk : ty:: BorrowKind ) {
215
215
if let ty:: BorrowKind :: Mutable = bk
216
216
&& is_potentially_local_place ( self . local_id , & cat. place )
217
- && !is_option_as_mut_use ( self . tcx , diag_expr_id)
217
+ && !is_as_mut_use ( self . tcx , diag_expr_id)
218
218
{
219
219
self . is_mutated = true ;
220
220
}
@@ -272,12 +272,10 @@ enum AsRefKind {
272
272
/// If it isn't, the expression itself is returned.
273
273
fn consume_option_as_ref < ' tcx > ( expr : & ' tcx Expr < ' tcx > ) -> ( & ' tcx Expr < ' tcx > , Option < AsRefKind > ) {
274
274
if let ExprKind :: MethodCall ( path, recv, [ ] , _) = expr. kind {
275
- if path. ident . name == sym:: as_ref {
276
- ( recv, Some ( AsRefKind :: AsRef ) )
277
- } else if path. ident . name == sym:: as_mut {
278
- ( recv, Some ( AsRefKind :: AsMut ) )
279
- } else {
280
- ( expr, None )
275
+ match path. ident . name {
276
+ sym:: as_ref => ( recv, Some ( AsRefKind :: AsRef ) ) ,
277
+ sym:: as_mut => ( recv, Some ( AsRefKind :: AsMut ) ) ,
278
+ _ => ( expr, None ) ,
281
279
}
282
280
} else {
283
281
( expr, None )
0 commit comments