Skip to content

Commit 08f658b

Browse files
committed
Use different span
1 parent 214d499 commit 08f658b

File tree

1 file changed

+10
-11
lines changed
  • clippy_lints/src/methods

1 file changed

+10
-11
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,7 @@ fn lint_iter_cloned_collect<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &hir::Ex
16631663
fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args: &[hir::Expr]) {
16641664
fn check_fold_with_op(
16651665
cx: &LateContext<'_, '_>,
1666+
expr: &hir::Expr,
16661667
fold_args: &[hir::Expr],
16671668
op: hir::BinOpKind,
16681669
replacement_method_name: &str,
@@ -1685,30 +1686,28 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
16851686
if match_var(&*left_expr, first_arg_ident);
16861687
if replacement_has_args || match_var(&*right_expr, second_arg_ident);
16871688

1688-
then {
1689-
// Span containing `.fold(...)`
1690-
let next_point = cx.sess().source_map().next_point(fold_args[0].span);
1691-
let fold_span = next_point.with_hi(fold_args[2].span.hi() + BytePos(1));
1689+
if let hir::ExprKind::MethodCall(_, span, _) = &expr.node;
16921690

1691+
then {
16931692
let mut applicability = Applicability::MachineApplicable;
16941693
let sugg = if replacement_has_args {
16951694
format!(
1696-
".{replacement}(|{s}| {r})",
1695+
"{replacement}(|{s}| {r})",
16971696
replacement = replacement_method_name,
16981697
s = second_arg_ident,
16991698
r = snippet_with_applicability(cx, right_expr.span, "EXPR", &mut applicability),
17001699
)
17011700
} else {
17021701
format!(
1703-
".{replacement}()",
1702+
"{replacement}()",
17041703
replacement = replacement_method_name,
17051704
)
17061705
};
17071706

17081707
span_lint_and_sugg(
17091708
cx,
17101709
UNNECESSARY_FOLD,
1711-
fold_span,
1710+
span.with_hi(expr.span.hi()),
17121711
// TODO #2371 don't suggest e.g., .any(|x| f(x)) if we can suggest .any(f)
17131712
"this `.fold` can be written more succinctly using another method",
17141713
"try",
@@ -1732,10 +1731,10 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
17321731
// Check if the first argument to .fold is a suitable literal
17331732
if let hir::ExprKind::Lit(ref lit) = fold_args[1].node {
17341733
match lit.node {
1735-
ast::LitKind::Bool(false) => check_fold_with_op(cx, fold_args, hir::BinOpKind::Or, "any", true),
1736-
ast::LitKind::Bool(true) => check_fold_with_op(cx, fold_args, hir::BinOpKind::And, "all", true),
1737-
ast::LitKind::Int(0, _) => check_fold_with_op(cx, fold_args, hir::BinOpKind::Add, "sum", false),
1738-
ast::LitKind::Int(1, _) => check_fold_with_op(cx, fold_args, hir::BinOpKind::Mul, "product", false),
1734+
ast::LitKind::Bool(false) => check_fold_with_op(cx, expr, fold_args, hir::BinOpKind::Or, "any", true),
1735+
ast::LitKind::Bool(true) => check_fold_with_op(cx, expr, fold_args, hir::BinOpKind::And, "all", true),
1736+
ast::LitKind::Int(0, _) => check_fold_with_op(cx, expr, fold_args, hir::BinOpKind::Add, "sum", false),
1737+
ast::LitKind::Int(1, _) => check_fold_with_op(cx, expr, fold_args, hir::BinOpKind::Mul, "product", false),
17391738
_ => (),
17401739
}
17411740
}

0 commit comments

Comments
 (0)