Skip to content

Commit c4e24c8

Browse files
committed
Move find_all_ret_expressions into utils
1 parent 5ae1e97 commit c4e24c8

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

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

clippy_lints/src/unnecessary_wrap.rs

+6-126
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

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub mod paths;
1919
pub mod ptr;
2020
pub mod sugg;
2121
pub mod usage;
22+
pub mod visitors;
2223

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

0 commit comments

Comments
 (0)