Skip to content

Commit dac81d8

Browse files
committed
Use the spans returned by utils::method_calls
1 parent 832c083 commit dac81d8

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
10001000
return;
10011001
}
10021002

1003-
let (method_names, arg_lists) = method_calls(expr, 2);
1003+
let (method_names, arg_lists, method_spans) = method_calls(expr, 2);
10041004
let method_names: Vec<LocalInternedString> = method_names.iter().map(|s| s.as_str()).collect();
10051005
let method_names: Vec<&str> = method_names.iter().map(std::convert::AsRef::as_ref).collect();
10061006

@@ -1020,7 +1020,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
10201020
["map", "find"] => lint_find_map(cx, expr, arg_lists[1], arg_lists[0]),
10211021
["flat_map", "filter"] => lint_filter_flat_map(cx, expr, arg_lists[1], arg_lists[0]),
10221022
["flat_map", "filter_map"] => lint_filter_map_flat_map(cx, expr, arg_lists[1], arg_lists[0]),
1023-
["flat_map", ..] => lint_flat_map_identity(cx, expr, arg_lists[0]),
1023+
["flat_map", ..] => lint_flat_map_identity(cx, expr, arg_lists[0], method_spans[0]),
10241024
["flatten", "map"] => lint_map_flatten(cx, expr, arg_lists[1]),
10251025
["is_some", "find"] => lint_search_is_some(cx, expr, "find", arg_lists[1], arg_lists[0]),
10261026
["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 {
10351035
["collect", "cloned"] => lint_iter_cloned_collect(cx, expr, arg_lists[1]),
10361036
["as_ref"] => lint_asref(cx, expr, "as_ref", arg_lists[0]),
10371037
["as_mut"] => lint_asref(cx, expr, "as_mut", arg_lists[0]),
1038-
["fold", ..] => lint_unnecessary_fold(cx, expr, arg_lists[0]),
1038+
["fold", ..] => lint_unnecessary_fold(cx, expr, arg_lists[0], method_spans[0]),
10391039
["filter_map", ..] => unnecessary_filter_map::lint(cx, expr, arg_lists[0]),
10401040
["count", "map"] => lint_suspicious_map(cx, expr),
10411041
_ => {},
@@ -1712,11 +1712,12 @@ fn lint_iter_cloned_collect<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &hir::Ex
17121712
}
17131713
}
17141714

1715-
fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args: &[hir::Expr]) {
1715+
fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args: &[hir::Expr], fold_span: Span) {
17161716
fn check_fold_with_op(
17171717
cx: &LateContext<'_, '_>,
17181718
expr: &hir::Expr,
17191719
fold_args: &[hir::Expr],
1720+
fold_span: Span,
17201721
op: hir::BinOpKind,
17211722
replacement_method_name: &str,
17221723
replacement_has_args: bool,
@@ -1738,8 +1739,6 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
17381739
if match_var(&*left_expr, first_arg_ident);
17391740
if replacement_has_args || match_var(&*right_expr, second_arg_ident);
17401741

1741-
if let hir::ExprKind::MethodCall(_, span, _) = &expr.node;
1742-
17431742
then {
17441743
let mut applicability = Applicability::MachineApplicable;
17451744
let sugg = if replacement_has_args {
@@ -1759,7 +1758,7 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
17591758
span_lint_and_sugg(
17601759
cx,
17611760
UNNECESSARY_FOLD,
1762-
span.with_hi(expr.span.hi()),
1761+
fold_span.with_hi(expr.span.hi()),
17631762
// TODO #2371 don't suggest e.g., .any(|x| f(x)) if we can suggest .any(f)
17641763
"this `.fold` can be written more succinctly using another method",
17651764
"try",
@@ -1783,10 +1782,18 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
17831782
// Check if the first argument to .fold is a suitable literal
17841783
if let hir::ExprKind::Lit(ref lit) = fold_args[1].node {
17851784
match lit.node {
1786-
ast::LitKind::Bool(false) => check_fold_with_op(cx, expr, fold_args, hir::BinOpKind::Or, "any", true),
1787-
ast::LitKind::Bool(true) => check_fold_with_op(cx, expr, fold_args, hir::BinOpKind::And, "all", true),
1788-
ast::LitKind::Int(0, _) => check_fold_with_op(cx, expr, fold_args, hir::BinOpKind::Add, "sum", false),
1789-
ast::LitKind::Int(1, _) => check_fold_with_op(cx, expr, fold_args, hir::BinOpKind::Mul, "product", false),
1785+
ast::LitKind::Bool(false) => {
1786+
check_fold_with_op(cx, expr, fold_args, fold_span, hir::BinOpKind::Or, "any", true)
1787+
},
1788+
ast::LitKind::Bool(true) => {
1789+
check_fold_with_op(cx, expr, fold_args, fold_span, hir::BinOpKind::And, "all", true)
1790+
},
1791+
ast::LitKind::Int(0, _) => {
1792+
check_fold_with_op(cx, expr, fold_args, fold_span, hir::BinOpKind::Add, "sum", false)
1793+
},
1794+
ast::LitKind::Int(1, _) => {
1795+
check_fold_with_op(cx, expr, fold_args, fold_span, hir::BinOpKind::Mul, "product", false)
1796+
},
17901797
_ => (),
17911798
}
17921799
}
@@ -2323,22 +2330,21 @@ fn lint_flat_map_identity<'a, 'tcx>(
23232330
cx: &LateContext<'a, 'tcx>,
23242331
expr: &'tcx hir::Expr,
23252332
flat_map_args: &'tcx [hir::Expr],
2333+
flat_map_span: Span,
23262334
) {
23272335
if match_trait_method(cx, expr, &paths::ITERATOR) {
23282336
let arg_node = &flat_map_args[1].node;
23292337

23302338
let apply_lint = |message: &str| {
2331-
if let hir::ExprKind::MethodCall(_, span, _) = &expr.node {
2332-
span_lint_and_sugg(
2333-
cx,
2334-
FLAT_MAP_IDENTITY,
2335-
span.with_hi(expr.span.hi()),
2336-
message,
2337-
"try",
2338-
"flatten()".to_string(),
2339-
Applicability::MachineApplicable,
2340-
);
2341-
}
2339+
span_lint_and_sugg(
2340+
cx,
2341+
FLAT_MAP_IDENTITY,
2342+
flat_map_span.with_hi(expr.span.hi()),
2343+
message,
2344+
"try",
2345+
"flatten()".to_string(),
2346+
Applicability::MachineApplicable,
2347+
);
23422348
};
23432349

23442350
if_chain! {

clippy_lints/src/utils/internal_lints.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl_lint_pass!(OuterExpnDataPass => [OUTER_EXPN_EXPN_DATA]);
280280

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

0 commit comments

Comments
 (0)