then {
let right_source = snippet(cx, right_expr.span, "EXPR");
- span_lint(
+ span_lint_and_sugg(
cx,
FOLD_ANY,
expr.span,
// TODO: don't suggest .any(|x| f(x)) if we can suggest .any(f)
- &format!(
- ".fold(false, |{f}, {s}| {f} || {r})) is more succinctly expressed as .any(|{s}| {r})",
- f = first_arg_ident,
+ "this `.fold` can more succintly be expressed as `.any`",
+ "try",
+ format!(
+ ".any(|{s}| {r})",
s = second_arg_ident,
r = right_source
- ),
+ )
);
}
}
db.docs_link(lint);
}
+/// Add a span lint with a suggestion on how to fix it.
+///
+/// These suggestions can be parsed by rustfix to allow it to automatically fix your code.
+/// In the example below, `help` is `"try"` and `sugg` is the suggested replacement `".any(|x| x > 2)"`.
+///
+/// <pre>
+/// error: This `.fold` can be more succinctly expressed as `.any`
+/// --> $DIR/methods.rs:390:13
+/// |
+/// 390 | let _ = (0..3).fold(false, |acc, x| acc || x > 2);
+/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)`
+/// |
+/// = note: `-D fold-any` implied by `-D warnings`
+/// </pre>
pub fn span_lint_and_sugg<'a, 'tcx: 'a, T: LintContext<'tcx>>(
cx: &'a T,
lint: &'static Lint,
}
/// Checks implementation of the `FOLD_ANY` lint
-fn fold_any_ignore_initial_value_of_true() {
+fn fold_any_ignores_initial_value_of_true() {
let _ = (0..3).fold(true, |acc, x| acc || x > 2);
}
+/// Checks implementation of the `FOLD_ANY` lint
+fn fold_any_ignores_non_boolean_accumalator() {
+ let _ = (0..3).fold(0, |acc, x| acc + if x > 2 { 1 } else { 0 });
+}
+
#[allow(similar_names)]
fn main() {
let opt = Some(0);
382 | let _ = &some_vec[..].iter().skip(3).next();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-error: .fold(false, |acc, x| acc || x > 2)) is more succinctly expressed as .any(|x| x > 2)
+error: this `.fold` can more succintly be expressed as `.any`
--> $DIR/methods.rs:390:13
|
390 | let _ = (0..3).fold(false, |acc, x| acc || x > 2);
- | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)`
|
= note: `-D fold-any` implied by `-D warnings`
error: used unwrap() on an Option value. If you don't want to handle the None case gracefully, consider using expect() to provide a better panic message
- --> $DIR/methods.rs:401:13
+ --> $DIR/methods.rs:406:13
|
-401 | let _ = opt.unwrap();
+406 | let _ = opt.unwrap();
| ^^^^^^^^^^^^
|
= note: `-D option-unwrap-used` implied by `-D warnings`