Skip to content

Commit b97eaab

Browse files
committed
Auto merge of #11387 - y21:issue11371, r=blyxyas
[`unnecessary_unwrap`]: lint on `.as_ref().unwrap()` Closes #11371 This turned out to be a little more code than I originally thought, because the lint also makes sure to not lint if the user tries to mutate the option: ```rs if option.is_some() { option = None; option.unwrap(); // don't lint here } ``` ... which means that even if we taught this lint to recognize `.as_mut()`, it would *still* not lint because that would count as a mutation. So we need to allow `.as_mut()` calls but reject other kinds of mutations. Unfortunately it doesn't look like this is possible with `is_potentially_mutated` (seeing what kind of mutation happened). This replaces it with a custom little visitor that does basically what it did before, but also allows `.as_mut()`. changelog: [`unnecessary_unwrap`]: lint on `.as_ref().unwrap()`
2 parents 5cc5f27 + f80c55d commit b97eaab

File tree

4 files changed

+235
-10
lines changed

4 files changed

+235
-10
lines changed

clippy_lints/src/unwrap.rs

+103-8
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,55 @@ 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+
/// *except* for if `Option::as_mut` is called.
200+
/// The reason for why we allow that one specifically is that `.as_mut()` cannot change
201+
/// the option to `None`, and that is important because this lint relies on the fact that
202+
/// `is_some` + `unwrap` is equivalent to `if let Some(..) = ..`, which it would not be if
203+
/// the option is changed to None between `is_some` and `unwrap`.
204+
/// (And also `.as_mut()` is a somewhat common method that is still worth linting on.)
205+
struct MutationVisitor<'tcx> {
206+
is_mutated: bool,
207+
local_id: HirId,
208+
tcx: TyCtxt<'tcx>,
209+
}
210+
211+
/// Checks if the parent of the expression pointed at by the given `HirId` is a call to
212+
/// `Option::as_mut`.
213+
///
214+
/// Used by the mutation visitor to specifically allow `.as_mut()` calls.
215+
/// In particular, the `HirId` that the visitor receives is the id of the local expression
216+
/// (i.e. the `x` in `x.as_mut()`), and that is the reason for why we care about its parent
217+
/// expression: that will be where the actual method call is.
218+
fn is_option_as_mut_use(tcx: TyCtxt<'_>, expr_id: HirId) -> bool {
219+
if let Node::Expr(mutating_expr) = tcx.hir().get_parent(expr_id)
220+
&& let ExprKind::MethodCall(path, ..) = mutating_expr.kind
221+
{
222+
path.ident.name.as_str() == "as_mut"
223+
} else {
224+
false
225+
}
226+
}
227+
228+
impl<'tcx> Delegate<'tcx> for MutationVisitor<'tcx> {
229+
fn borrow(&mut self, cat: &PlaceWithHirId<'tcx>, diag_expr_id: HirId, bk: ty::BorrowKind) {
230+
if let ty::BorrowKind::MutBorrow = bk
231+
&& is_potentially_local_place(self.local_id, &cat.place)
232+
&& !is_option_as_mut_use(self.tcx, diag_expr_id)
233+
{
234+
self.is_mutated = true;
235+
}
236+
}
237+
238+
fn mutate(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {
239+
self.is_mutated = true;
240+
}
241+
242+
fn consume(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
243+
244+
fn fake_read(&mut self, _: &PlaceWithHirId<'tcx>, _: FakeReadCause, _: HirId) {}
245+
}
246+
195247
impl<'a, 'tcx> UnwrappableVariablesVisitor<'a, 'tcx> {
196248
fn visit_branch(
197249
&mut self,
@@ -202,10 +254,26 @@ impl<'a, 'tcx> UnwrappableVariablesVisitor<'a, 'tcx> {
202254
) {
203255
let prev_len = self.unwrappables.len();
204256
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-
{
208-
// if the variable is mutated, we don't know whether it can be unwrapped:
257+
let mut delegate = MutationVisitor {
258+
tcx: self.cx.tcx,
259+
is_mutated: false,
260+
local_id: unwrap_info.local_id,
261+
};
262+
263+
let infcx = self.cx.tcx.infer_ctxt().build();
264+
let mut vis = ExprUseVisitor::new(
265+
&mut delegate,
266+
&infcx,
267+
cond.hir_id.owner.def_id,
268+
self.cx.param_env,
269+
self.cx.typeck_results(),
270+
);
271+
vis.walk_expr(cond);
272+
vis.walk_expr(branch);
273+
274+
if delegate.is_mutated {
275+
// if the variable is mutated, we don't know whether it can be unwrapped.
276+
// it might have been changed to `None` in between `is_some` + `unwrap`.
209277
continue;
210278
}
211279
self.unwrappables.push(unwrap_info);
@@ -215,6 +283,27 @@ impl<'a, 'tcx> UnwrappableVariablesVisitor<'a, 'tcx> {
215283
}
216284
}
217285

286+
enum AsRefKind {
287+
AsRef,
288+
AsMut,
289+
}
290+
291+
/// Checks if the expression is a method call to `as_{ref,mut}` and returns the receiver of it.
292+
/// If it isn't, the expression itself is returned.
293+
fn consume_option_as_ref<'tcx>(expr: &'tcx Expr<'tcx>) -> (&'tcx Expr<'tcx>, Option<AsRefKind>) {
294+
if let ExprKind::MethodCall(path, recv, ..) = expr.kind {
295+
if path.ident.name == sym::as_ref {
296+
(recv, Some(AsRefKind::AsRef))
297+
} else if path.ident.name.as_str() == "as_mut" {
298+
(recv, Some(AsRefKind::AsMut))
299+
} else {
300+
(expr, None)
301+
}
302+
} else {
303+
(expr, None)
304+
}
305+
}
306+
218307
impl<'a, 'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'a, 'tcx> {
219308
type NestedFilter = nested_filter::OnlyBodies;
220309

@@ -233,6 +322,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'a, 'tcx> {
233322
// find `unwrap[_err]()` calls:
234323
if_chain! {
235324
if let ExprKind::MethodCall(method_name, self_arg, ..) = expr.kind;
325+
let (self_arg, as_ref_kind) = consume_option_as_ref(self_arg);
236326
if let Some(id) = path_to_local(self_arg);
237327
if [sym::unwrap, sym::expect, sym!(unwrap_err)].contains(&method_name.ident.name);
238328
let call_to_unwrap = [sym::unwrap, sym::expect].contains(&method_name.ident.name);
@@ -268,7 +358,12 @@ impl<'a, 'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'a, 'tcx> {
268358
unwrappable.check.span.with_lo(unwrappable.if_expr.span.lo()),
269359
"try",
270360
format!(
271-
"if let {suggested_pattern} = {unwrappable_variable_name}",
361+
"if let {suggested_pattern} = {borrow_prefix}{unwrappable_variable_name}",
362+
borrow_prefix = match as_ref_kind {
363+
Some(AsRefKind::AsRef) => "&",
364+
Some(AsRefKind::AsMut) => "&mut ",
365+
None => "",
366+
},
272367
),
273368
// We don't track how the unwrapped value is used inside the
274369
// 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

+51
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,57 @@ 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+
// This should not lint. Statics are, at the time of writing, not linted on anyway,
171+
// but if at some point they are supported by this lint, it should correctly see that
172+
// `X` is being mutated and not suggest `if let Some(..) = X {}`
173+
static mut X: Option<i32> = Some(123);
174+
unsafe {
175+
if X.is_some() {
176+
X = None;
177+
X.unwrap();
178+
}
179+
}
180+
}
181+
131182
fn check_expect() {
132183
let x = Some(());
133184
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)