@@ -4,15 +4,15 @@ use clippy_utils::usage::is_potentially_local_place;
4
4
use clippy_utils:: { higher, path_to_local, sym} ;
5
5
use rustc_errors:: Applicability ;
6
6
use 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 } ;
8
8
use rustc_hir_typeck:: expr_use_visitor:: { Delegate , ExprUseVisitor , PlaceWithHirId } ;
9
9
use rustc_lint:: { LateContext , LateLintPass } ;
10
10
use rustc_middle:: hir:: nested_filter;
11
11
use rustc_middle:: mir:: FakeReadCause ;
12
12
use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
13
13
use rustc_session:: declare_lint_pass;
14
- use rustc_span:: Span ;
15
14
use rustc_span:: def_id:: LocalDefId ;
15
+ use rustc_span:: { Span , Symbol } ;
16
16
17
17
declare_clippy_lint ! {
18
18
/// ### What it does
@@ -111,7 +111,7 @@ struct UnwrapInfo<'tcx> {
111
111
/// The check, like `x.is_ok()`
112
112
check : & ' tcx Expr < ' tcx > ,
113
113
/// The check's name, like `is_ok`
114
- check_name : & ' tcx PathSegment < ' tcx > ,
114
+ check_name : Symbol ,
115
115
/// The branch where the check takes place, like `if x.is_ok() { .. }`
116
116
branch : & ' tcx Expr < ' tcx > ,
117
117
/// 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>(
133
133
invert : bool ,
134
134
is_entire_condition : bool ,
135
135
) -> 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 )
138
138
}
139
139
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 )
142
142
}
143
143
144
144
if let ExprKind :: Binary ( op, left, right) = & expr. kind {
@@ -155,14 +155,10 @@ fn collect_unwrap_info<'tcx>(
155
155
} else if let ExprKind :: MethodCall ( method_name, receiver, [ ] , _) = & expr. kind
156
156
&& let Some ( local_id) = path_to_local ( receiver)
157
157
&& let ty = cx. typeck_results ( ) . expr_ty ( receiver)
158
- && let name = method_name. ident . as_str ( )
158
+ && let name = method_name. ident . name
159
159
&& ( is_relevant_option_call ( cx, ty, name) || is_relevant_result_call ( cx, ty, name) )
160
160
{
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) ;
166
162
let safe_to_unwrap = unwrappable != invert;
167
163
let kind = if is_type_diagnostic_item ( cx, ty, sym:: Option ) {
168
164
UnwrappableKind :: Option
@@ -174,7 +170,7 @@ fn collect_unwrap_info<'tcx>(
174
170
local_id,
175
171
if_expr,
176
172
check: expr,
177
- check_name: method_name ,
173
+ check_name: name ,
178
174
branch,
179
175
safe_to_unwrap,
180
176
kind,
@@ -184,12 +180,12 @@ fn collect_unwrap_info<'tcx>(
184
180
Vec :: new ( )
185
181
}
186
182
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.
189
185
/// 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
191
187
/// `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` .
193
189
/// (And also `.as_mut()` is a somewhat common method that is still worth linting on.)
194
190
struct MutationVisitor < ' tcx > {
195
191
is_mutated : bool ,
@@ -198,13 +194,13 @@ struct MutationVisitor<'tcx> {
198
194
}
199
195
200
196
/// Checks if the parent of the expression pointed at by the given `HirId` is a call to
201
- /// `Option:: as_mut`.
197
+ /// `. as_mut() `.
202
198
///
203
199
/// Used by the mutation visitor to specifically allow `.as_mut()` calls.
204
200
/// In particular, the `HirId` that the visitor receives is the id of the local expression
205
201
/// (i.e. the `x` in `x.as_mut()`), and that is the reason for why we care about its parent
206
202
/// 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 {
208
204
if let Node :: Expr ( mutating_expr) = tcx. parent_hir_node ( expr_id)
209
205
&& let ExprKind :: MethodCall ( path, _, [ ] , _) = mutating_expr. kind
210
206
{
@@ -218,7 +214,7 @@ impl<'tcx> Delegate<'tcx> for MutationVisitor<'tcx> {
218
214
fn borrow ( & mut self , cat : & PlaceWithHirId < ' tcx > , diag_expr_id : HirId , bk : ty:: BorrowKind ) {
219
215
if let ty:: BorrowKind :: Mutable = bk
220
216
&& 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)
222
218
{
223
219
self . is_mutated = true ;
224
220
}
@@ -278,12 +274,10 @@ enum AsRefKind {
278
274
/// If it isn't, the expression itself is returned.
279
275
fn consume_option_as_ref < ' tcx > ( expr : & ' tcx Expr < ' tcx > ) -> ( & ' tcx Expr < ' tcx > , Option < AsRefKind > ) {
280
276
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 ) ,
287
281
}
288
282
} else {
289
283
( expr, None )
@@ -334,8 +328,7 @@ impl<'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'_, 'tcx> {
334
328
expr. span ,
335
329
format ! (
336
330
"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,
339
332
) ,
340
333
|diag| {
341
334
if is_entire_condition {
0 commit comments