Skip to content

Commit 4cbd116

Browse files
committed
unwrap.rs: internal naming cleanup
Since its conception, the lint has been extended from `Option` only to `Option` and `Result`, but some function names and comment still references only `Option`. Also, one cascaded `if` was replaced by a shorter `match`.
1 parent 668a292 commit 4cbd116

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

clippy_lints/src/unwrap.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,12 @@ fn collect_unwrap_info<'tcx>(
180180
Vec::new()
181181
}
182182

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.
185185
/// 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
187187
/// `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`.
189189
/// (And also `.as_mut()` is a somewhat common method that is still worth linting on.)
190190
struct MutationVisitor<'tcx> {
191191
is_mutated: bool,
@@ -194,13 +194,13 @@ struct MutationVisitor<'tcx> {
194194
}
195195

196196
/// Checks if the parent of the expression pointed at by the given `HirId` is a call to
197-
/// `Option::as_mut`.
197+
/// `.as_mut()`.
198198
///
199199
/// Used by the mutation visitor to specifically allow `.as_mut()` calls.
200200
/// In particular, the `HirId` that the visitor receives is the id of the local expression
201201
/// (i.e. the `x` in `x.as_mut()`), and that is the reason for why we care about its parent
202202
/// 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 {
204204
if let Node::Expr(mutating_expr) = tcx.parent_hir_node(expr_id)
205205
&& let ExprKind::MethodCall(path, _, [], _) = mutating_expr.kind
206206
{
@@ -214,7 +214,7 @@ impl<'tcx> Delegate<'tcx> for MutationVisitor<'tcx> {
214214
fn borrow(&mut self, cat: &PlaceWithHirId<'tcx>, diag_expr_id: HirId, bk: ty::BorrowKind) {
215215
if let ty::BorrowKind::Mutable = bk
216216
&& 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)
218218
{
219219
self.is_mutated = true;
220220
}
@@ -272,12 +272,10 @@ enum AsRefKind {
272272
/// If it isn't, the expression itself is returned.
273273
fn consume_option_as_ref<'tcx>(expr: &'tcx Expr<'tcx>) -> (&'tcx Expr<'tcx>, Option<AsRefKind>) {
274274
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),
281279
}
282280
} else {
283281
(expr, None)

0 commit comments

Comments
 (0)