Skip to content

Commit 42c6492

Browse files
committed
[unnecessary_unwrap]: lint on .as_ref().unwrap()
1 parent 4932d05 commit 42c6492

File tree

4 files changed

+210
-9
lines changed

4 files changed

+210
-9
lines changed

clippy_lints/src/unwrap.rs

+89-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
use clippy_utils::diagnostics::span_lint_hir_and_then;
22
use clippy_utils::ty::is_type_diagnostic_item;
3-
use clippy_utils::usage::is_potentially_mutated;
3+
use clippy_utils::usage::is_potentially_local_place;
44
use clippy_utils::{higher, path_to_local};
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
77
use rustc_hir::intravisit::{walk_expr, walk_fn, FnKind, Visitor};
8-
use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, HirId, PathSegment, UnOp};
8+
use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, HirId, Node, PathSegment, UnOp};
9+
use rustc_hir_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, PlaceWithHirId};
10+
use rustc_infer::infer::TyCtxtInferExt;
911
use rustc_lint::{LateContext, LateLintPass};
1012
use rustc_middle::hir::nested_filter;
1113
use rustc_middle::lint::in_external_macro;
12-
use rustc_middle::ty::Ty;
14+
use rustc_middle::mir::FakeReadCause;
15+
use rustc_middle::ty::{self, Ty, TyCtxt};
1316
use rustc_session::{declare_lint_pass, declare_tool_lint};
1417
use rustc_span::def_id::LocalDefId;
1518
use rustc_span::source_map::Span;
@@ -192,6 +195,43 @@ fn collect_unwrap_info<'tcx>(
192195
Vec::new()
193196
}
194197

198+
/// A HIR visitor delegate that checks if a local variable of type `Option<_>` is mutated,
199+
/// unless `Option::as_mut` is called.
200+
struct MutationVisitor<'tcx> {
201+
is_mutated: bool,
202+
local_id: HirId,
203+
tcx: TyCtxt<'tcx>,
204+
}
205+
206+
fn is_option_as_mut_use(tcx: TyCtxt<'_>, expr_id: HirId) -> bool {
207+
if let Node::Expr(mutating_expr) = tcx.hir().get_parent(expr_id)
208+
&& let ExprKind::MethodCall(path, ..) = mutating_expr.kind
209+
{
210+
path.ident.name.as_str() == "as_mut"
211+
} else {
212+
false
213+
}
214+
}
215+
216+
impl<'tcx> Delegate<'tcx> for MutationVisitor<'tcx> {
217+
fn borrow(&mut self, cat: &PlaceWithHirId<'tcx>, diag_expr_id: HirId, bk: ty::BorrowKind) {
218+
if let ty::BorrowKind::MutBorrow = bk
219+
&& is_potentially_local_place(self.local_id, &cat.place)
220+
&& !is_option_as_mut_use(self.tcx, diag_expr_id)
221+
{
222+
self.is_mutated = true;
223+
}
224+
}
225+
226+
fn mutate(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {
227+
self.is_mutated = true;
228+
}
229+
230+
fn consume(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
231+
232+
fn fake_read(&mut self, _: &PlaceWithHirId<'tcx>, _: FakeReadCause, _: HirId) {}
233+
}
234+
195235
impl<'a, 'tcx> UnwrappableVariablesVisitor<'a, 'tcx> {
196236
fn visit_branch(
197237
&mut self,
@@ -202,9 +242,24 @@ impl<'a, 'tcx> UnwrappableVariablesVisitor<'a, 'tcx> {
202242
) {
203243
let prev_len = self.unwrappables.len();
204244
for unwrap_info in collect_unwrap_info(self.cx, if_expr, cond, branch, else_branch, true) {
205-
if is_potentially_mutated(unwrap_info.local_id, cond, self.cx)
206-
|| is_potentially_mutated(unwrap_info.local_id, branch, self.cx)
207-
{
245+
let mut delegate = MutationVisitor {
246+
tcx: self.cx.tcx,
247+
is_mutated: false,
248+
local_id: unwrap_info.local_id,
249+
};
250+
251+
let infcx = self.cx.tcx.infer_ctxt().build();
252+
let mut vis = ExprUseVisitor::new(
253+
&mut delegate,
254+
&infcx,
255+
cond.hir_id.owner.def_id,
256+
self.cx.param_env,
257+
self.cx.typeck_results(),
258+
);
259+
vis.walk_expr(cond);
260+
vis.walk_expr(branch);
261+
262+
if delegate.is_mutated {
208263
// if the variable is mutated, we don't know whether it can be unwrapped:
209264
continue;
210265
}
@@ -215,6 +270,27 @@ impl<'a, 'tcx> UnwrappableVariablesVisitor<'a, 'tcx> {
215270
}
216271
}
217272

273+
enum AsRefKind {
274+
AsRef,
275+
AsMut,
276+
}
277+
278+
/// Checks if the expression is a method call to `as_{ref,mut}` and returns the receiver of it.
279+
/// If it isn't, the expression itself is returned.
280+
fn consume_option_as_ref<'tcx>(expr: &'tcx Expr<'tcx>) -> (&'tcx Expr<'tcx>, Option<AsRefKind>) {
281+
if let ExprKind::MethodCall(path, recv, ..) = expr.kind {
282+
if path.ident.name == sym::as_ref {
283+
(recv, Some(AsRefKind::AsRef))
284+
} else if path.ident.name.as_str() == "as_mut" {
285+
(recv, Some(AsRefKind::AsMut))
286+
} else {
287+
(expr, None)
288+
}
289+
} else {
290+
(expr, None)
291+
}
292+
}
293+
218294
impl<'a, 'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'a, 'tcx> {
219295
type NestedFilter = nested_filter::OnlyBodies;
220296

@@ -233,6 +309,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'a, 'tcx> {
233309
// find `unwrap[_err]()` calls:
234310
if_chain! {
235311
if let ExprKind::MethodCall(method_name, self_arg, ..) = expr.kind;
312+
let (self_arg, as_ref_kind) = consume_option_as_ref(self_arg);
236313
if let Some(id) = path_to_local(self_arg);
237314
if [sym::unwrap, sym::expect, sym!(unwrap_err)].contains(&method_name.ident.name);
238315
let call_to_unwrap = [sym::unwrap, sym::expect].contains(&method_name.ident.name);
@@ -268,7 +345,12 @@ impl<'a, 'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'a, 'tcx> {
268345
unwrappable.check.span.with_lo(unwrappable.if_expr.span.lo()),
269346
"try",
270347
format!(
271-
"if let {suggested_pattern} = {unwrappable_variable_name}",
348+
"if let {suggested_pattern} = {borrow_prefix}{unwrappable_variable_name}",
349+
borrow_prefix = match as_ref_kind {
350+
Some(AsRefKind::AsRef) => "&",
351+
Some(AsRefKind::AsMut) => "&mut ",
352+
None => "",
353+
},
272354
),
273355
// We don't track how the unwrapped value is used inside the
274356
// block or suggest deleting the unwrap, so we can't offer a

clippy_utils/src/usage.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use core::ops::ControlFlow;
44
use hir::def::Res;
55
use rustc_hir::intravisit::{self, Visitor};
66
use rustc_hir::{self as hir, Expr, ExprKind, HirId, HirIdSet};
7-
use rustc_hir_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
7+
use rustc_hir_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, Place, PlaceBase, PlaceWithHirId};
88
use rustc_infer::infer::TyCtxtInferExt;
99
use rustc_lint::LateContext;
1010
use rustc_middle::hir::nested_filter;
@@ -37,6 +37,17 @@ pub fn is_potentially_mutated<'tcx>(variable: HirId, expr: &'tcx Expr<'_>, cx: &
3737
mutated_variables(expr, cx).map_or(true, |mutated| mutated.contains(&variable))
3838
}
3939

40+
pub fn is_potentially_local_place(local_id: HirId, place: &Place<'_>) -> bool {
41+
match place.base {
42+
PlaceBase::Local(id) => id == local_id,
43+
PlaceBase::Upvar(_) => {
44+
// Conservatively assume yes.
45+
true
46+
},
47+
_ => false,
48+
}
49+
}
50+
4051
struct MutVarsDelegate {
4152
used_mutably: HirIdSet,
4253
skip: bool,

tests/ui/checked_unwrap/simple_conditionals.rs

+40
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,46 @@ fn main() {
128128
assert!(x.is_ok(), "{:?}", x.unwrap_err());
129129
}
130130

131+
fn issue11371() {
132+
let option = Some(());
133+
134+
if option.is_some() {
135+
option.as_ref().unwrap();
136+
//~^ ERROR: called `unwrap` on `option` after checking its variant with `is_some`
137+
} else {
138+
option.as_ref().unwrap();
139+
//~^ ERROR: this call to `unwrap()` will always panic
140+
}
141+
142+
let result = Ok::<(), ()>(());
143+
144+
if result.is_ok() {
145+
result.as_ref().unwrap();
146+
//~^ ERROR: called `unwrap` on `result` after checking its variant with `is_ok`
147+
} else {
148+
result.as_ref().unwrap();
149+
//~^ ERROR: this call to `unwrap()` will always panic
150+
}
151+
152+
let mut option = Some(());
153+
if option.is_some() {
154+
option.as_mut().unwrap();
155+
//~^ ERROR: called `unwrap` on `option` after checking its variant with `is_some`
156+
} else {
157+
option.as_mut().unwrap();
158+
//~^ ERROR: this call to `unwrap()` will always panic
159+
}
160+
161+
let mut result = Ok::<(), ()>(());
162+
if result.is_ok() {
163+
result.as_mut().unwrap();
164+
//~^ ERROR: called `unwrap` on `result` after checking its variant with `is_ok`
165+
} else {
166+
result.as_mut().unwrap();
167+
//~^ ERROR: this call to `unwrap()` will always panic
168+
}
169+
}
170+
131171
fn check_expect() {
132172
let x = Some(());
133173
if x.is_some() {

tests/ui/checked_unwrap/simple_conditionals.stderr

+69-1
Original file line numberDiff line numberDiff line change
@@ -168,5 +168,73 @@ LL | if x.is_err() {
168168
LL | x.unwrap_err();
169169
| ^^^^^^^^^^^^^^
170170

171-
error: aborting due to 17 previous errors
171+
error: called `unwrap` on `option` after checking its variant with `is_some`
172+
--> $DIR/simple_conditionals.rs:135:9
173+
|
174+
LL | if option.is_some() {
175+
| ------------------- help: try: `if let Some(..) = &option`
176+
LL | option.as_ref().unwrap();
177+
| ^^^^^^^^^^^^^^^^^^^^^^^^
178+
179+
error: this call to `unwrap()` will always panic
180+
--> $DIR/simple_conditionals.rs:138:9
181+
|
182+
LL | if option.is_some() {
183+
| ---------------- because of this check
184+
...
185+
LL | option.as_ref().unwrap();
186+
| ^^^^^^^^^^^^^^^^^^^^^^^^
187+
188+
error: called `unwrap` on `result` after checking its variant with `is_ok`
189+
--> $DIR/simple_conditionals.rs:145:9
190+
|
191+
LL | if result.is_ok() {
192+
| ----------------- help: try: `if let Ok(..) = &result`
193+
LL | result.as_ref().unwrap();
194+
| ^^^^^^^^^^^^^^^^^^^^^^^^
195+
196+
error: this call to `unwrap()` will always panic
197+
--> $DIR/simple_conditionals.rs:148:9
198+
|
199+
LL | if result.is_ok() {
200+
| -------------- because of this check
201+
...
202+
LL | result.as_ref().unwrap();
203+
| ^^^^^^^^^^^^^^^^^^^^^^^^
204+
205+
error: called `unwrap` on `option` after checking its variant with `is_some`
206+
--> $DIR/simple_conditionals.rs:154:9
207+
|
208+
LL | if option.is_some() {
209+
| ------------------- help: try: `if let Some(..) = &mut option`
210+
LL | option.as_mut().unwrap();
211+
| ^^^^^^^^^^^^^^^^^^^^^^^^
212+
213+
error: this call to `unwrap()` will always panic
214+
--> $DIR/simple_conditionals.rs:157:9
215+
|
216+
LL | if option.is_some() {
217+
| ---------------- because of this check
218+
...
219+
LL | option.as_mut().unwrap();
220+
| ^^^^^^^^^^^^^^^^^^^^^^^^
221+
222+
error: called `unwrap` on `result` after checking its variant with `is_ok`
223+
--> $DIR/simple_conditionals.rs:163:9
224+
|
225+
LL | if result.is_ok() {
226+
| ----------------- help: try: `if let Ok(..) = &mut result`
227+
LL | result.as_mut().unwrap();
228+
| ^^^^^^^^^^^^^^^^^^^^^^^^
229+
230+
error: this call to `unwrap()` will always panic
231+
--> $DIR/simple_conditionals.rs:166:9
232+
|
233+
LL | if result.is_ok() {
234+
| -------------- because of this check
235+
...
236+
LL | result.as_mut().unwrap();
237+
| ^^^^^^^^^^^^^^^^^^^^^^^^
238+
239+
error: aborting due to 25 previous errors
172240

0 commit comments

Comments
 (0)