diff options
| author | bors <bors@rust-lang.org> | 2023-08-28 20:29:42 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-08-28 20:29:42 +0000 |
| commit | b97eaab558bd37f665b10a79fd5aecea3dde920f (patch) | |
| tree | c9df2bb8a6f2391d1302454d9bc19ec8937115b0 /clippy_utils | |
| parent | 5cc5f2789912f3516b37c7e79887feafa768cc44 (diff) | |
| parent | f80c55deb58424e19de1a43c7a4d7a9ab70030c9 (diff) | |
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()`
Diffstat (limited to 'clippy_utils')
| -rw-r--r-- | clippy_utils/src/usage.rs | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/clippy_utils/src/usage.rs b/clippy_utils/src/usage.rs index 39ef76348d7..ec131c7f6a3 100644 --- a/clippy_utils/src/usage.rs +++ b/clippy_utils/src/usage.rs @@ -4,7 +4,7 @@ use core::ops::ControlFlow; use hir::def::Res; use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::{self as hir, Expr, ExprKind, HirId, HirIdSet}; -use rustc_hir_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId}; +use rustc_hir_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, Place, PlaceBase, PlaceWithHirId}; use rustc_infer::infer::TyCtxtInferExt; use rustc_lint::LateContext; use rustc_middle::hir::nested_filter; @@ -37,6 +37,17 @@ pub fn is_potentially_mutated<'tcx>(variable: HirId, expr: &'tcx Expr<'_>, cx: & mutated_variables(expr, cx).map_or(true, |mutated| mutated.contains(&variable)) } +pub fn is_potentially_local_place(local_id: HirId, place: &Place<'_>) -> bool { + match place.base { + PlaceBase::Local(id) => id == local_id, + PlaceBase::Upvar(_) => { + // Conservatively assume yes. + true + }, + _ => false, + } +} + struct MutVarsDelegate { used_mutably: HirIdSet, skip: bool, |
