|
| 1 | +use std::ops::ControlFlow; |
| 2 | + |
1 | 3 | use clippy_utils::diagnostics::span_lint_and_then;
|
| 4 | +use clippy_utils::path_to_local_id; |
2 | 5 | use clippy_utils::source::snippet;
|
3 |
| -use clippy_utils::visitors::is_local_used; |
| 6 | +use clippy_utils::visitors::{Descend, Visitable, for_each_expr}; |
4 | 7 | use rustc_data_structures::fx::FxHashMap;
|
5 | 8 | use rustc_hir::def::Res;
|
6 | 9 | use rustc_hir::def_id::LocalDefId;
|
@@ -175,17 +178,39 @@ fn is_shadow(cx: &LateContext<'_>, owner: LocalDefId, first: ItemLocalId, second
|
175 | 178 | false
|
176 | 179 | }
|
177 | 180 |
|
| 181 | +/// Checks if the given local is used, except for in child expression of `except`. |
| 182 | +/// |
| 183 | +/// This is a version of [`is_local_used`](clippy_utils::visitors::is_local_used), used to |
| 184 | +/// implement the fix for <https://github.com/rust-lang/rust-clippy/issues/10780>. |
| 185 | +pub fn is_local_used_except<'tcx>( |
| 186 | + cx: &LateContext<'tcx>, |
| 187 | + visitable: impl Visitable<'tcx>, |
| 188 | + id: HirId, |
| 189 | + except: Option<HirId>, |
| 190 | +) -> bool { |
| 191 | + for_each_expr(cx, visitable, |e| { |
| 192 | + if except.is_some_and(|it| it == e.hir_id) { |
| 193 | + ControlFlow::Continue(Descend::No) |
| 194 | + } else if path_to_local_id(e, id) { |
| 195 | + ControlFlow::Break(()) |
| 196 | + } else { |
| 197 | + ControlFlow::Continue(Descend::Yes) |
| 198 | + } |
| 199 | + }) |
| 200 | + .is_some() |
| 201 | +} |
| 202 | + |
178 | 203 | fn lint_shadow(cx: &LateContext<'_>, pat: &Pat<'_>, shadowed: HirId, span: Span) {
|
179 | 204 | let (lint, msg) = match find_init(cx, pat.hir_id) {
|
180 |
| - Some(expr) if is_self_shadow(cx, pat, expr, shadowed) => { |
| 205 | + Some((expr, _)) if is_self_shadow(cx, pat, expr, shadowed) => { |
181 | 206 | let msg = format!(
|
182 | 207 | "`{}` is shadowed by itself in `{}`",
|
183 | 208 | snippet(cx, pat.span, "_"),
|
184 | 209 | snippet(cx, expr.span, "..")
|
185 | 210 | );
|
186 | 211 | (SHADOW_SAME, msg)
|
187 | 212 | },
|
188 |
| - Some(expr) if is_local_used(cx, expr, shadowed) => { |
| 213 | + Some((expr, except)) if is_local_used_except(cx, expr, shadowed, except) => { |
189 | 214 | let msg = format!("`{}` is shadowed", snippet(cx, pat.span, "_"));
|
190 | 215 | (SHADOW_REUSE, msg)
|
191 | 216 | },
|
@@ -232,15 +257,32 @@ fn is_self_shadow(cx: &LateContext<'_>, pat: &Pat<'_>, mut expr: &Expr<'_>, hir_
|
232 | 257 |
|
233 | 258 | /// Finds the "init" expression for a pattern: `let <pat> = <init>;` (or `if let`) or
|
234 | 259 | /// `match <init> { .., <pat> => .., .. }`
|
235 |
| -fn find_init<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<&'tcx Expr<'tcx>> { |
236 |
| - for (_, node) in cx.tcx.hir().parent_iter(hir_id) { |
| 260 | +/// |
| 261 | +/// For closure arguments passed to a method call, returns the method call, and the `HirId` of the |
| 262 | +/// closure (which will later be skipped). This is for <https://github.com/rust-lang/rust-clippy/issues/10780> |
| 263 | +fn find_init<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<(&'tcx Expr<'tcx>, Option<HirId>)> { |
| 264 | + for (hir_id, node) in cx.tcx.hir().parent_iter(hir_id) { |
237 | 265 | let init = match node {
|
238 |
| - Node::Arm(_) | Node::Pat(_) => continue, |
| 266 | + Node::Arm(_) | Node::Pat(_) | Node::Param(_) => continue, |
239 | 267 | Node::Expr(expr) => match expr.kind {
|
240 |
| - ExprKind::Match(e, _, _) | ExprKind::Let(&LetExpr { init: e, .. }) => Some(e), |
| 268 | + ExprKind::Match(e, _, _) | ExprKind::Let(&LetExpr { init: e, .. }) => Some((e, None)), |
| 269 | + // If we're a closure argument, then a parent call is also an associated item. |
| 270 | + ExprKind::Closure(_) => { |
| 271 | + if let Some((_, node)) = cx.tcx.hir().parent_iter(hir_id).next() { |
| 272 | + match node { |
| 273 | + Node::Expr(expr) => match expr.kind { |
| 274 | + ExprKind::MethodCall(_, _, _, _) | ExprKind::Call(_, _) => Some((expr, Some(hir_id))), |
| 275 | + _ => None, |
| 276 | + }, |
| 277 | + _ => None, |
| 278 | + } |
| 279 | + } else { |
| 280 | + None |
| 281 | + } |
| 282 | + }, |
241 | 283 | _ => None,
|
242 | 284 | },
|
243 |
| - Node::LetStmt(local) => local.init, |
| 285 | + Node::LetStmt(local) => local.init.map(|init| (init, None)), |
244 | 286 | _ => None,
|
245 | 287 | };
|
246 | 288 | return init;
|
|
0 commit comments