Skip to content

Commit 5740af7

Browse files
committed
Move find_all_ret_expressions into utils
1 parent c17e278 commit 5740af7

File tree

4 files changed

+133
-250
lines changed

4 files changed

+133
-250
lines changed

clippy_lints/src/methods/bind_instead_of_map.rs

Lines changed: 1 addition & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
use super::{contains_return, BIND_INSTEAD_OF_MAP};
22
use crate::utils::{
33
in_macro, match_qpath, match_type, method_calls, multispan_sugg_with_applicability, paths, remove_blocks, snippet,
4-
snippet_with_macro_callsite, span_lint_and_sugg, span_lint_and_then,
4+
snippet_with_macro_callsite, span_lint_and_sugg, span_lint_and_then, visitors::find_all_ret_expressions,
55
};
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
88
use rustc_hir as hir;
9-
use rustc_hir::intravisit::{self, Visitor};
109
use rustc_lint::LateContext;
11-
use rustc_middle::hir::map::Map;
1210
use rustc_span::Span;
1311

1412
pub(crate) struct OptionAndThenSome;
@@ -193,124 +191,3 @@ pub(crate) trait BindInsteadOfMap {
193191
}
194192
}
195193
}
196-
197-
/// returns `true` if expr contains match expr desugared from try
198-
fn contains_try(expr: &hir::Expr<'_>) -> bool {
199-
struct TryFinder {
200-
found: bool,
201-
}
202-
203-
impl<'hir> intravisit::Visitor<'hir> for TryFinder {
204-
type Map = Map<'hir>;
205-
206-
fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
207-
intravisit::NestedVisitorMap::None
208-
}
209-
210-
fn visit_expr(&mut self, expr: &'hir hir::Expr<'hir>) {
211-
if self.found {
212-
return;
213-
}
214-
match expr.kind {
215-
hir::ExprKind::Match(_, _, hir::MatchSource::TryDesugar) => self.found = true,
216-
_ => intravisit::walk_expr(self, expr),
217-
}
218-
}
219-
}
220-
221-
let mut visitor = TryFinder { found: false };
222-
visitor.visit_expr(expr);
223-
visitor.found
224-
}
225-
226-
fn find_all_ret_expressions<'hir, F>(_cx: &LateContext<'_>, expr: &'hir hir::Expr<'hir>, callback: F) -> bool
227-
where
228-
F: FnMut(&'hir hir::Expr<'hir>) -> bool,
229-
{
230-
struct RetFinder<F> {
231-
in_stmt: bool,
232-
failed: bool,
233-
cb: F,
234-
}
235-
236-
struct WithStmtGuarg<'a, F> {
237-
val: &'a mut RetFinder<F>,
238-
prev_in_stmt: bool,
239-
}
240-
241-
impl<F> RetFinder<F> {
242-
fn inside_stmt(&mut self, in_stmt: bool) -> WithStmtGuarg<'_, F> {
243-
let prev_in_stmt = std::mem::replace(&mut self.in_stmt, in_stmt);
244-
WithStmtGuarg {
245-
val: self,
246-
prev_in_stmt,
247-
}
248-
}
249-
}
250-
251-
impl<F> std::ops::Deref for WithStmtGuarg<'_, F> {
252-
type Target = RetFinder<F>;
253-
254-
fn deref(&self) -> &Self::Target {
255-
self.val
256-
}
257-
}
258-
259-
impl<F> std::ops::DerefMut for WithStmtGuarg<'_, F> {
260-
fn deref_mut(&mut self) -> &mut Self::Target {
261-
self.val
262-
}
263-
}
264-
265-
impl<F> Drop for WithStmtGuarg<'_, F> {
266-
fn drop(&mut self) {
267-
self.val.in_stmt = self.prev_in_stmt;
268-
}
269-
}
270-
271-
impl<'hir, F: FnMut(&'hir hir::Expr<'hir>) -> bool> intravisit::Visitor<'hir> for RetFinder<F> {
272-
type Map = Map<'hir>;
273-
274-
fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
275-
intravisit::NestedVisitorMap::None
276-
}
277-
278-
fn visit_stmt(&mut self, stmt: &'hir hir::Stmt<'_>) {
279-
intravisit::walk_stmt(&mut *self.inside_stmt(true), stmt)
280-
}
281-
282-
fn visit_expr(&mut self, expr: &'hir hir::Expr<'_>) {
283-
if self.failed {
284-
return;
285-
}
286-
if self.in_stmt {
287-
match expr.kind {
288-
hir::ExprKind::Ret(Some(expr)) => self.inside_stmt(false).visit_expr(expr),
289-
_ => intravisit::walk_expr(self, expr),
290-
}
291-
} else {
292-
match expr.kind {
293-
hir::ExprKind::Match(cond, arms, _) => {
294-
self.inside_stmt(true).visit_expr(cond);
295-
for arm in arms {
296-
self.visit_expr(arm.body);
297-
}
298-
},
299-
hir::ExprKind::Block(..) => intravisit::walk_expr(self, expr),
300-
hir::ExprKind::Ret(Some(expr)) => self.visit_expr(expr),
301-
_ => self.failed |= !(self.cb)(expr),
302-
}
303-
}
304-
}
305-
}
306-
307-
!contains_try(expr) && {
308-
let mut ret_finder = RetFinder {
309-
in_stmt: false,
310-
failed: false,
311-
cb: callback,
312-
};
313-
ret_finder.visit_expr(expr);
314-
!ret_finder.failed
315-
}
316-
}

clippy_lints/src/unnecessary_wrap.rs

Lines changed: 6 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
use crate::utils::{is_type_diagnostic_item, match_qpath, paths, return_ty, snippet, span_lint_and_then};
1+
use crate::utils::{
2+
is_type_diagnostic_item, match_qpath, paths, return_ty, snippet, span_lint_and_then,
3+
visitors::find_all_ret_expressions,
4+
};
25
use if_chain::if_chain;
36
use rustc_errors::Applicability;
4-
use rustc_hir::intravisit::{FnKind, Visitor};
7+
use rustc_hir::intravisit::FnKind;
58
use rustc_hir::*;
69
use rustc_lint::{LateContext, LateLintPass};
7-
use rustc_middle::{hir::map::Map, ty::subst::GenericArgKind};
10+
use rustc_middle::ty::subst::GenericArgKind;
811
use rustc_session::{declare_lint_pass, declare_tool_lint};
912
use rustc_span::Span;
1013

@@ -125,126 +128,3 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWrap {
125128
}
126129
}
127130
}
128-
129-
// code below is copied from `bind_instead_of_map`
130-
131-
fn find_all_ret_expressions<'hir, F>(_cx: &LateContext<'_>, expr: &'hir Expr<'hir>, callback: F) -> bool
132-
where
133-
F: FnMut(&'hir Expr<'hir>) -> bool,
134-
{
135-
struct RetFinder<F> {
136-
in_stmt: bool,
137-
failed: bool,
138-
cb: F,
139-
}
140-
141-
struct WithStmtGuarg<'a, F> {
142-
val: &'a mut RetFinder<F>,
143-
prev_in_stmt: bool,
144-
}
145-
146-
impl<F> RetFinder<F> {
147-
fn inside_stmt(&mut self, in_stmt: bool) -> WithStmtGuarg<'_, F> {
148-
let prev_in_stmt = std::mem::replace(&mut self.in_stmt, in_stmt);
149-
WithStmtGuarg {
150-
val: self,
151-
prev_in_stmt,
152-
}
153-
}
154-
}
155-
156-
impl<F> std::ops::Deref for WithStmtGuarg<'_, F> {
157-
type Target = RetFinder<F>;
158-
159-
fn deref(&self) -> &Self::Target {
160-
self.val
161-
}
162-
}
163-
164-
impl<F> std::ops::DerefMut for WithStmtGuarg<'_, F> {
165-
fn deref_mut(&mut self) -> &mut Self::Target {
166-
self.val
167-
}
168-
}
169-
170-
impl<F> Drop for WithStmtGuarg<'_, F> {
171-
fn drop(&mut self) {
172-
self.val.in_stmt = self.prev_in_stmt;
173-
}
174-
}
175-
176-
impl<'hir, F: FnMut(&'hir Expr<'hir>) -> bool> intravisit::Visitor<'hir> for RetFinder<F> {
177-
type Map = Map<'hir>;
178-
179-
fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
180-
intravisit::NestedVisitorMap::None
181-
}
182-
183-
fn visit_stmt(&mut self, stmt: &'hir Stmt<'_>) {
184-
intravisit::walk_stmt(&mut *self.inside_stmt(true), stmt)
185-
}
186-
187-
fn visit_expr(&mut self, expr: &'hir Expr<'_>) {
188-
if self.failed {
189-
return;
190-
}
191-
if self.in_stmt {
192-
match expr.kind {
193-
ExprKind::Ret(Some(expr)) => self.inside_stmt(false).visit_expr(expr),
194-
_ => intravisit::walk_expr(self, expr),
195-
}
196-
} else {
197-
match expr.kind {
198-
ExprKind::Match(cond, arms, _) => {
199-
self.inside_stmt(true).visit_expr(cond);
200-
for arm in arms {
201-
self.visit_expr(arm.body);
202-
}
203-
},
204-
ExprKind::Block(..) => intravisit::walk_expr(self, expr),
205-
ExprKind::Ret(Some(expr)) => self.visit_expr(expr),
206-
_ => self.failed |= !(self.cb)(expr),
207-
}
208-
}
209-
}
210-
}
211-
212-
!contains_try(expr) && {
213-
let mut ret_finder = RetFinder {
214-
in_stmt: false,
215-
failed: false,
216-
cb: callback,
217-
};
218-
ret_finder.visit_expr(expr);
219-
!ret_finder.failed
220-
}
221-
}
222-
223-
/// returns `true` if expr contains match expr desugared from try
224-
fn contains_try(expr: &Expr<'_>) -> bool {
225-
struct TryFinder {
226-
found: bool,
227-
}
228-
229-
impl<'hir> intravisit::Visitor<'hir> for TryFinder {
230-
type Map = Map<'hir>;
231-
232-
fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
233-
intravisit::NestedVisitorMap::None
234-
}
235-
236-
fn visit_expr(&mut self, expr: &'hir Expr<'hir>) {
237-
if self.found {
238-
return;
239-
}
240-
match expr.kind {
241-
ExprKind::Match(_, _, MatchSource::TryDesugar) => self.found = true,
242-
_ => intravisit::walk_expr(self, expr),
243-
}
244-
}
245-
}
246-
247-
let mut visitor = TryFinder { found: false };
248-
visitor.visit_expr(expr);
249-
visitor.found
250-
}

clippy_lints/src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub mod paths;
2020
pub mod ptr;
2121
pub mod sugg;
2222
pub mod usage;
23+
pub mod visitors;
2324

2425
pub use self::attrs::*;
2526
pub use self::diagnostics::*;

0 commit comments

Comments
 (0)