Address some review comments

This commit is contained in:
Phil Ellison 2018-01-14 18:18:09 +00:00
parent 360f2359d5
commit 70a5535ffa
4 changed files with 30 additions and 10 deletions

View File

@ -1153,17 +1153,18 @@ fn lint_fold_any(cx: &LateContext, expr: &hir::Expr, fold_args: &[hir::Expr]) {
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
),
)
);
}
}

View File

@ -596,6 +596,20 @@ pub fn span_lint_and_then<'a, 'tcx: 'a, T: LintContext<'tcx>, F>(
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,

View File

@ -391,10 +391,15 @@ fn fold_any() {
}
/// 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);

View File

@ -493,18 +493,18 @@ error: called `skip(x).next()` on an iterator. This is more succinctly expressed
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`