Skip to content

Commit 607b829

Browse files
committed
Auto merge of #4382 - jeremystucki:unnecessary_fold_span, r=flip1995
Change span of unnecessary_fold lint Resolves #4381 changelog: Change linted span of `unnecessary_fold`
2 parents a3da66d + fdf82eb commit 607b829

File tree

4 files changed

+44
-23
lines changed

4 files changed

+44
-23
lines changed

clippy_lints/src/methods/mod.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc::ty::{self, Predicate, Ty};
1515
use rustc::{declare_lint_pass, declare_tool_lint};
1616
use rustc_errors::Applicability;
1717
use syntax::ast;
18-
use syntax::source_map::{BytePos, Span};
18+
use syntax::source_map::Span;
1919
use syntax::symbol::LocalInternedString;
2020

2121
use crate::utils::paths;
@@ -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
}

tests/ui/unnecessary_fold.fixed

+8
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,12 @@ fn unnecessary_fold_should_ignore() {
4141
let _ = [(0..2), (0..3)].iter().fold(1, |a, b| a * b.len());
4242
}
4343

44+
/// Should lint only the line containing the fold
45+
fn unnecessary_fold_over_multiple_lines() {
46+
let _ = (0..3)
47+
.map(|x| x + 1)
48+
.filter(|x| x % 2 == 0)
49+
.any(|x| x > 2);
50+
}
51+
4452
fn main() {}

tests/ui/unnecessary_fold.rs

+8
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,12 @@ fn unnecessary_fold_should_ignore() {
4141
let _ = [(0..2), (0..3)].iter().fold(1, |a, b| a * b.len());
4242
}
4343

44+
/// Should lint only the line containing the fold
45+
fn unnecessary_fold_over_multiple_lines() {
46+
let _ = (0..3)
47+
.map(|x| x + 1)
48+
.filter(|x| x % 2 == 0)
49+
.fold(false, |acc, x| acc || x > 2);
50+
}
51+
4452
fn main() {}

tests/ui/unnecessary_fold.stderr

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
11
error: this `.fold` can be written more succinctly using another method
2-
--> $DIR/unnecessary_fold.rs:8:19
2+
--> $DIR/unnecessary_fold.rs:8:20
33
|
44
LL | let _ = (0..3).fold(false, |acc, x| acc || x > 2);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `any(|x| x > 2)`
66
|
77
= note: `-D clippy::unnecessary-fold` implied by `-D warnings`
88

99
error: this `.fold` can be written more succinctly using another method
10-
--> $DIR/unnecessary_fold.rs:10:19
10+
--> $DIR/unnecessary_fold.rs:10:20
1111
|
1212
LL | let _ = (0..3).fold(true, |acc, x| acc && x > 2);
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.all(|x| x > 2)`
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `all(|x| x > 2)`
1414

1515
error: this `.fold` can be written more succinctly using another method
16-
--> $DIR/unnecessary_fold.rs:12:24
16+
--> $DIR/unnecessary_fold.rs:12:25
1717
|
1818
LL | let _: i32 = (0..3).fold(0, |acc, x| acc + x);
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.sum()`
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `sum()`
2020

2121
error: this `.fold` can be written more succinctly using another method
22-
--> $DIR/unnecessary_fold.rs:14:24
22+
--> $DIR/unnecessary_fold.rs:14:25
2323
|
2424
LL | let _: i32 = (0..3).fold(1, |acc, x| acc * x);
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.product()`
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `product()`
2626

2727
error: this `.fold` can be written more succinctly using another method
28-
--> $DIR/unnecessary_fold.rs:19:40
28+
--> $DIR/unnecessary_fold.rs:19:41
2929
|
3030
LL | let _: bool = (0..3).map(|x| 2 * x).fold(false, |acc, x| acc || x > 2);
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)`
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `any(|x| x > 2)`
3232

33-
error: aborting due to 5 previous errors
33+
error: this `.fold` can be written more succinctly using another method
34+
--> $DIR/unnecessary_fold.rs:49:10
35+
|
36+
LL | .fold(false, |acc, x| acc || x > 2);
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `any(|x| x > 2)`
38+
39+
error: aborting due to 6 previous errors
3440

0 commit comments

Comments
 (0)