Auto merge of #4454 - BO41:search_is_some, r=flip1995

Dereference one less on search_is_some and make it auto-fixable

Fixes #4453

changelog: none
This commit is contained in:
bors 2019-09-04 07:53:44 +00:00
commit 8239b7616f
6 changed files with 94 additions and 59 deletions

View file

@ -280,7 +280,7 @@ impl_lint_pass!(OuterExpnDataPass => [OUTER_EXPN_EXPN_DATA]);
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OuterExpnDataPass {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
let (method_names, arg_lists) = method_calls(expr, 2);
let (method_names, arg_lists, spans) = method_calls(expr, 2);
let method_names: Vec<LocalInternedString> = method_names.iter().map(|s| s.as_str()).collect();
let method_names: Vec<&str> = method_names.iter().map(std::convert::AsRef::as_ref).collect();
if_chain! {
@ -294,10 +294,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OuterExpnDataPass {
span_lint_and_sugg(
cx,
OUTER_EXPN_EXPN_DATA,
expr.span.trim_start(self_arg.span).unwrap_or(expr.span),
spans[1].with_hi(expr.span.hi()),
"usage of `outer_expn().expn_data()`",
"try",
".outer_expn_data()".to_string(),
"outer_expn_data()".to_string(),
Applicability::MachineApplicable,
);
}

View file

@ -342,26 +342,28 @@ pub fn resolve_node(cx: &LateContext<'_, '_>, qpath: &QPath, id: HirId) -> Res {
}
/// Returns the method names and argument list of nested method call expressions that make up
/// `expr`.
pub fn method_calls(expr: &Expr, max_depth: usize) -> (Vec<Symbol>, Vec<&[Expr]>) {
/// `expr`. method/span lists are sorted with the most recent call first.
pub fn method_calls(expr: &Expr, max_depth: usize) -> (Vec<Symbol>, Vec<&[Expr]>, Vec<Span>) {
let mut method_names = Vec::with_capacity(max_depth);
let mut arg_lists = Vec::with_capacity(max_depth);
let mut spans = Vec::with_capacity(max_depth);
let mut current = expr;
for _ in 0..max_depth {
if let ExprKind::MethodCall(path, _, args) = &current.node {
if let ExprKind::MethodCall(path, span, args) = &current.node {
if args.iter().any(|e| e.span.from_expansion()) {
break;
}
method_names.push(path.ident.name);
arg_lists.push(&**args);
spans.push(*span);
current = &args[0];
} else {
break;
}
}
(method_names, arg_lists)
(method_names, arg_lists, spans)
}
/// Matches an `Expr` against a chain of methods, and return the matched `Expr`s.