mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 21:23:56 +00:00
Use the spans returned by utils::method_calls
This commit is contained in:
parent
832c0830ec
commit
dac81d867b
2 changed files with 31 additions and 25 deletions
|
@ -1000,7 +1000,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
|
|||
return;
|
||||
}
|
||||
|
||||
let (method_names, arg_lists) = method_calls(expr, 2);
|
||||
let (method_names, arg_lists, method_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();
|
||||
|
||||
|
@ -1020,7 +1020,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
|
|||
["map", "find"] => lint_find_map(cx, expr, arg_lists[1], arg_lists[0]),
|
||||
["flat_map", "filter"] => lint_filter_flat_map(cx, expr, arg_lists[1], arg_lists[0]),
|
||||
["flat_map", "filter_map"] => lint_filter_map_flat_map(cx, expr, arg_lists[1], arg_lists[0]),
|
||||
["flat_map", ..] => lint_flat_map_identity(cx, expr, arg_lists[0]),
|
||||
["flat_map", ..] => lint_flat_map_identity(cx, expr, arg_lists[0], method_spans[0]),
|
||||
["flatten", "map"] => lint_map_flatten(cx, expr, arg_lists[1]),
|
||||
["is_some", "find"] => lint_search_is_some(cx, expr, "find", arg_lists[1], arg_lists[0]),
|
||||
["is_some", "position"] => lint_search_is_some(cx, expr, "position", arg_lists[1], arg_lists[0]),
|
||||
|
@ -1035,7 +1035,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
|
|||
["collect", "cloned"] => lint_iter_cloned_collect(cx, expr, arg_lists[1]),
|
||||
["as_ref"] => lint_asref(cx, expr, "as_ref", arg_lists[0]),
|
||||
["as_mut"] => lint_asref(cx, expr, "as_mut", arg_lists[0]),
|
||||
["fold", ..] => lint_unnecessary_fold(cx, expr, arg_lists[0]),
|
||||
["fold", ..] => lint_unnecessary_fold(cx, expr, arg_lists[0], method_spans[0]),
|
||||
["filter_map", ..] => unnecessary_filter_map::lint(cx, expr, arg_lists[0]),
|
||||
["count", "map"] => lint_suspicious_map(cx, expr),
|
||||
_ => {},
|
||||
|
@ -1712,11 +1712,12 @@ fn lint_iter_cloned_collect<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &hir::Ex
|
|||
}
|
||||
}
|
||||
|
||||
fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args: &[hir::Expr]) {
|
||||
fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args: &[hir::Expr], fold_span: Span) {
|
||||
fn check_fold_with_op(
|
||||
cx: &LateContext<'_, '_>,
|
||||
expr: &hir::Expr,
|
||||
fold_args: &[hir::Expr],
|
||||
fold_span: Span,
|
||||
op: hir::BinOpKind,
|
||||
replacement_method_name: &str,
|
||||
replacement_has_args: bool,
|
||||
|
@ -1738,8 +1739,6 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
|
|||
if match_var(&*left_expr, first_arg_ident);
|
||||
if replacement_has_args || match_var(&*right_expr, second_arg_ident);
|
||||
|
||||
if let hir::ExprKind::MethodCall(_, span, _) = &expr.node;
|
||||
|
||||
then {
|
||||
let mut applicability = Applicability::MachineApplicable;
|
||||
let sugg = if replacement_has_args {
|
||||
|
@ -1759,7 +1758,7 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
|
|||
span_lint_and_sugg(
|
||||
cx,
|
||||
UNNECESSARY_FOLD,
|
||||
span.with_hi(expr.span.hi()),
|
||||
fold_span.with_hi(expr.span.hi()),
|
||||
// TODO #2371 don't suggest e.g., .any(|x| f(x)) if we can suggest .any(f)
|
||||
"this `.fold` can be written more succinctly using another method",
|
||||
"try",
|
||||
|
@ -1783,10 +1782,18 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
|
|||
// Check if the first argument to .fold is a suitable literal
|
||||
if let hir::ExprKind::Lit(ref lit) = fold_args[1].node {
|
||||
match lit.node {
|
||||
ast::LitKind::Bool(false) => check_fold_with_op(cx, expr, fold_args, hir::BinOpKind::Or, "any", true),
|
||||
ast::LitKind::Bool(true) => check_fold_with_op(cx, expr, fold_args, hir::BinOpKind::And, "all", true),
|
||||
ast::LitKind::Int(0, _) => check_fold_with_op(cx, expr, fold_args, hir::BinOpKind::Add, "sum", false),
|
||||
ast::LitKind::Int(1, _) => check_fold_with_op(cx, expr, fold_args, hir::BinOpKind::Mul, "product", false),
|
||||
ast::LitKind::Bool(false) => {
|
||||
check_fold_with_op(cx, expr, fold_args, fold_span, hir::BinOpKind::Or, "any", true)
|
||||
},
|
||||
ast::LitKind::Bool(true) => {
|
||||
check_fold_with_op(cx, expr, fold_args, fold_span, hir::BinOpKind::And, "all", true)
|
||||
},
|
||||
ast::LitKind::Int(0, _) => {
|
||||
check_fold_with_op(cx, expr, fold_args, fold_span, hir::BinOpKind::Add, "sum", false)
|
||||
},
|
||||
ast::LitKind::Int(1, _) => {
|
||||
check_fold_with_op(cx, expr, fold_args, fold_span, hir::BinOpKind::Mul, "product", false)
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
@ -2323,22 +2330,21 @@ fn lint_flat_map_identity<'a, 'tcx>(
|
|||
cx: &LateContext<'a, 'tcx>,
|
||||
expr: &'tcx hir::Expr,
|
||||
flat_map_args: &'tcx [hir::Expr],
|
||||
flat_map_span: Span,
|
||||
) {
|
||||
if match_trait_method(cx, expr, &paths::ITERATOR) {
|
||||
let arg_node = &flat_map_args[1].node;
|
||||
|
||||
let apply_lint = |message: &str| {
|
||||
if let hir::ExprKind::MethodCall(_, span, _) = &expr.node {
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
FLAT_MAP_IDENTITY,
|
||||
span.with_hi(expr.span.hi()),
|
||||
message,
|
||||
"try",
|
||||
"flatten()".to_string(),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
FLAT_MAP_IDENTITY,
|
||||
flat_map_span.with_hi(expr.span.hi()),
|
||||
message,
|
||||
"try",
|
||||
"flatten()".to_string(),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
};
|
||||
|
||||
if_chain! {
|
||||
|
|
|
@ -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,
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue