diff options
| author | Jason Newcomb <jsnewcomb@pm.me> | 2021-04-17 20:35:39 -0400 |
|---|---|---|
| committer | Jason Newcomb <jsnewcomb@pm.me> | 2021-05-20 09:03:47 -0400 |
| commit | 6d4dc35882a55610e9da81237d62bea1c0c1634e (patch) | |
| tree | b64bbdd7c9e90986f0d9d028147dd24e44230e24 /clippy_utils/src | |
| parent | 6e03a306ac44c6670064c68197df8813fe1cac06 (diff) | |
| download | rust-6d4dc35882a55610e9da81237d62bea1c0c1634e.tar.gz rust-6d4dc35882a55610e9da81237d62bea1c0c1634e.zip | |
Improve `needless_borrow` lint
Suggest changing usages of ref bindings to match the new type Split out some cases into new lint `ref_binding_to_reference`
Diffstat (limited to 'clippy_utils/src')
| -rw-r--r-- | clippy_utils/src/higher.rs | 2 | ||||
| -rw-r--r-- | clippy_utils/src/lib.rs | 4 | ||||
| -rw-r--r-- | clippy_utils/src/visitors.rs | 37 |
3 files changed, 15 insertions, 28 deletions
diff --git a/clippy_utils/src/higher.rs b/clippy_utils/src/higher.rs index 0c0e4d3b4ce..25b4491616c 100644 --- a/clippy_utils/src/higher.rs +++ b/clippy_utils/src/higher.rs @@ -70,7 +70,7 @@ pub fn range<'a>(expr: &'a hir::Expr<'_>) -> Option<Range<'a>> { limits: ast::RangeLimits::Closed, }) }, - hir::ExprKind::Struct(ref path, ref fields, None) => match path { + hir::ExprKind::Struct(path, ref fields, None) => match path { hir::QPath::LangItem(hir::LangItem::RangeFull, _) => Some(Range { start: None, end: None, diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index 82250151aab..2b9b214daa7 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -1254,12 +1254,12 @@ pub fn match_function_call<'tcx>( path: &[&str], ) -> Option<&'tcx [Expr<'tcx>]> { if_chain! { - if let ExprKind::Call(ref fun, ref args) = expr.kind; + if let ExprKind::Call(ref fun, args) = expr.kind; if let ExprKind::Path(ref qpath) = fun.kind; if let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id(); if match_def_path(cx, fun_def_id, path); then { - return Some(&args) + return Some(args) } }; None diff --git a/clippy_utils/src/visitors.rs b/clippy_utils/src/visitors.rs index ecdc666b5f6..73f132eef4d 100644 --- a/clippy_utils/src/visitors.rs +++ b/clippy_utils/src/visitors.rs @@ -189,34 +189,21 @@ impl<'v> Visitor<'v> for LocalUsedVisitor<'v> { } } +/// A type which can be visited. pub trait Visitable<'tcx> { - fn visit<V: Visitor<'tcx>>(self, v: &mut V); + /// Calls the corresponding `visit_*` function on the visitor. + fn visit<V: Visitor<'tcx>>(self, visitor: &mut V); } -impl Visitable<'tcx> for &'tcx Expr<'tcx> { - fn visit<V: Visitor<'tcx>>(self, v: &mut V) { - v.visit_expr(self) - } -} -impl Visitable<'tcx> for &'tcx Block<'tcx> { - fn visit<V: Visitor<'tcx>>(self, v: &mut V) { - v.visit_block(self) - } -} -impl<'tcx> Visitable<'tcx> for &'tcx Stmt<'tcx> { - fn visit<V: Visitor<'tcx>>(self, v: &mut V) { - v.visit_stmt(self) - } -} -impl<'tcx> Visitable<'tcx> for &'tcx Body<'tcx> { - fn visit<V: Visitor<'tcx>>(self, v: &mut V) { - v.visit_body(self) - } -} -impl<'tcx> Visitable<'tcx> for &'tcx Arm<'tcx> { - fn visit<V: Visitor<'tcx>>(self, v: &mut V) { - v.visit_arm(self) - } +macro_rules! visitable_ref { + ($t:ident, $f:ident) => { + impl Visitable<'tcx> for &'tcx $t<'tcx> { + fn visit<V: Visitor<'tcx>>(self, visitor: &mut V) { + visitor.$f(self); + } + } + }; } +visitable_ref!(Block, visit_block); /// Calls the given function for each break expression. pub fn visit_break_exprs<'tcx>( |
