@@ -4,15 +4,15 @@ use clippy_utils::usage::is_potentially_local_place;
44use clippy_utils:: { higher, path_to_local, sym} ;
55use rustc_errors:: Applicability ;
66use rustc_hir:: intravisit:: { FnKind , Visitor , walk_expr, walk_fn} ;
7- use rustc_hir:: { BinOpKind , Body , Expr , ExprKind , FnDecl , HirId , Node , PathSegment , UnOp } ;
7+ use rustc_hir:: { BinOpKind , Body , Expr , ExprKind , FnDecl , HirId , Node , UnOp } ;
88use rustc_hir_typeck:: expr_use_visitor:: { Delegate , ExprUseVisitor , PlaceWithHirId } ;
99use rustc_lint:: { LateContext , LateLintPass } ;
1010use rustc_middle:: hir:: nested_filter;
1111use rustc_middle:: mir:: FakeReadCause ;
1212use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
1313use rustc_session:: declare_lint_pass;
14- use rustc_span:: Span ;
1514use rustc_span:: def_id:: LocalDefId ;
15+ use rustc_span:: { Span , Symbol } ;
1616
1717declare_clippy_lint ! {
1818 /// ### What it does
@@ -111,7 +111,7 @@ struct UnwrapInfo<'tcx> {
111111 /// The check, like `x.is_ok()`
112112 check : & ' tcx Expr < ' tcx > ,
113113 /// The check's name, like `is_ok`
114- check_name : & ' tcx PathSegment < ' tcx > ,
114+ check_name : Symbol ,
115115 /// The branch where the check takes place, like `if x.is_ok() { .. }`
116116 branch : & ' tcx Expr < ' tcx > ,
117117 /// Whether `is_some()` or `is_ok()` was called (as opposed to `is_err()` or `is_none()`).
@@ -133,12 +133,12 @@ fn collect_unwrap_info<'tcx>(
133133 invert : bool ,
134134 is_entire_condition : bool ,
135135) -> Vec < UnwrapInfo < ' tcx > > {
136- fn is_relevant_option_call ( cx : & LateContext < ' _ > , ty : Ty < ' _ > , method_name : & str ) -> bool {
137- is_type_diagnostic_item ( cx, ty, sym:: Option ) && [ "is_some" , " is_none" ] . contains ( & method_name )
136+ fn is_relevant_option_call ( cx : & LateContext < ' _ > , ty : Ty < ' _ > , method_name : Symbol ) -> bool {
137+ is_type_diagnostic_item ( cx, ty, sym:: Option ) && matches ! ( method_name , sym :: is_none | sym :: is_some )
138138 }
139139
140- fn is_relevant_result_call ( cx : & LateContext < ' _ > , ty : Ty < ' _ > , method_name : & str ) -> bool {
141- is_type_diagnostic_item ( cx, ty, sym:: Result ) && [ "is_ok" , " is_err" ] . contains ( & method_name )
140+ fn is_relevant_result_call ( cx : & LateContext < ' _ > , ty : Ty < ' _ > , method_name : Symbol ) -> bool {
141+ is_type_diagnostic_item ( cx, ty, sym:: Result ) && matches ! ( method_name , sym :: is_err | sym :: is_ok )
142142 }
143143
144144 if let ExprKind :: Binary ( op, left, right) = & expr. kind {
@@ -155,14 +155,10 @@ fn collect_unwrap_info<'tcx>(
155155 } else if let ExprKind :: MethodCall ( method_name, receiver, [ ] , _) = & expr. kind
156156 && let Some ( local_id) = path_to_local ( receiver)
157157 && let ty = cx. typeck_results ( ) . expr_ty ( receiver)
158- && let name = method_name. ident . as_str ( )
158+ && let name = method_name. ident . name
159159 && ( is_relevant_option_call ( cx, ty, name) || is_relevant_result_call ( cx, ty, name) )
160160 {
161- let unwrappable = match name {
162- "is_some" | "is_ok" => true ,
163- "is_err" | "is_none" => false ,
164- _ => unreachable ! ( ) ,
165- } ;
161+ let unwrappable = matches ! ( name, sym:: is_some | sym:: is_ok) ;
166162 let safe_to_unwrap = unwrappable != invert;
167163 let kind = if is_type_diagnostic_item ( cx, ty, sym:: Option ) {
168164 UnwrappableKind :: Option
@@ -174,7 +170,7 @@ fn collect_unwrap_info<'tcx>(
174170 local_id,
175171 if_expr,
176172 check: expr,
177- check_name: method_name ,
173+ check_name: name ,
178174 branch,
179175 safe_to_unwrap,
180176 kind,
@@ -184,12 +180,12 @@ fn collect_unwrap_info<'tcx>(
184180 Vec :: new ( )
185181}
186182
187- /// A HIR visitor delegate that checks if a local variable of type `Option<_> ` is mutated,
188- /// *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.
189185/// The reason for why we allow that one specifically is that `.as_mut()` cannot change
190- /// 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
191187/// `is_some` + `unwrap` is equivalent to `if let Some(..) = ..`, which it would not be if
192- /// 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` .
193189/// (And also `.as_mut()` is a somewhat common method that is still worth linting on.)
194190struct MutationVisitor < ' tcx > {
195191 is_mutated : bool ,
@@ -198,13 +194,13 @@ struct MutationVisitor<'tcx> {
198194}
199195
200196/// Checks if the parent of the expression pointed at by the given `HirId` is a call to
201- /// `Option:: as_mut`.
197+ /// `. as_mut() `.
202198///
203199/// Used by the mutation visitor to specifically allow `.as_mut()` calls.
204200/// In particular, the `HirId` that the visitor receives is the id of the local expression
205201/// (i.e. the `x` in `x.as_mut()`), and that is the reason for why we care about its parent
206202/// expression: that will be where the actual method call is.
207- fn is_option_as_mut_use ( tcx : TyCtxt < ' _ > , expr_id : HirId ) -> bool {
203+ fn is_as_mut_use ( tcx : TyCtxt < ' _ > , expr_id : HirId ) -> bool {
208204 if let Node :: Expr ( mutating_expr) = tcx. parent_hir_node ( expr_id)
209205 && let ExprKind :: MethodCall ( path, _, [ ] , _) = mutating_expr. kind
210206 {
@@ -218,7 +214,7 @@ impl<'tcx> Delegate<'tcx> for MutationVisitor<'tcx> {
218214 fn borrow ( & mut self , cat : & PlaceWithHirId < ' tcx > , diag_expr_id : HirId , bk : ty:: BorrowKind ) {
219215 if let ty:: BorrowKind :: Mutable = bk
220216 && is_potentially_local_place ( self . local_id , & cat. place )
221- && !is_option_as_mut_use ( self . tcx , diag_expr_id)
217+ && !is_as_mut_use ( self . tcx , diag_expr_id)
222218 {
223219 self . is_mutated = true ;
224220 }
@@ -278,12 +274,10 @@ enum AsRefKind {
278274/// If it isn't, the expression itself is returned.
279275fn consume_option_as_ref < ' tcx > ( expr : & ' tcx Expr < ' tcx > ) -> ( & ' tcx Expr < ' tcx > , Option < AsRefKind > ) {
280276 if let ExprKind :: MethodCall ( path, recv, [ ] , _) = expr. kind {
281- if path. ident . name == sym:: as_ref {
282- ( recv, Some ( AsRefKind :: AsRef ) )
283- } else if path. ident . name == sym:: as_mut {
284- ( recv, Some ( AsRefKind :: AsMut ) )
285- } else {
286- ( expr, None )
277+ match path. ident . name {
278+ sym:: as_ref => ( recv, Some ( AsRefKind :: AsRef ) ) ,
279+ sym:: as_mut => ( recv, Some ( AsRefKind :: AsMut ) ) ,
280+ _ => ( expr, None ) ,
287281 }
288282 } else {
289283 ( expr, None )
@@ -334,8 +328,7 @@ impl<'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'_, 'tcx> {
334328 expr. span ,
335329 format ! (
336330 "called `{}` on `{unwrappable_variable_name}` after checking its variant with `{}`" ,
337- method_name. ident. name,
338- unwrappable. check_name. ident. as_str( ) ,
331+ method_name. ident. name, unwrappable. check_name,
339332 ) ,
340333 |diag| {
341334 if is_entire_condition {
0 commit comments