Fix error span to play nicely with rustfix

This commit is contained in:
Phil Ellison 2018-01-16 21:20:55 +00:00
parent ad164939ed
commit a64d19cc0e
4 changed files with 23 additions and 9 deletions

View File

@ -9,7 +9,7 @@ use std::borrow::Cow;
use std::fmt;
use std::iter;
use syntax::ast;
use syntax::codemap::Span;
use syntax::codemap::{Span, BytePos};
use utils::{get_arg_name, get_trait_def_id, implements_trait, in_external_macro, in_macro, is_copy, is_self, is_self_ty,
iter_input_pats, last_path_segment, match_def_path, match_path, match_qpath, match_trait_method,
match_type, method_chain_args, return_ty, remove_blocks, same_tys, single_segment_path, snippet, span_lint,
@ -1157,10 +1157,13 @@ fn lint_fold_any(cx: &LateContext, expr: &hir::Expr, fold_args: &[hir::Expr]) {
then {
let right_source = snippet(cx, right_expr.span, "EXPR");
// Span containing `.fold(...)`
let fold_span = fold_args[0].span.next_point().with_hi(fold_args[2].span.hi() + BytePos(1));
span_lint_and_sugg(
cx,
FOLD_ANY,
expr.span,
fold_span,
// TODO: don't suggest .any(|x| f(x)) if we can suggest .any(f)
"this `.fold` can more succintly be expressed as `.any`",
"try",

View File

@ -606,7 +606,7 @@ pub fn span_lint_and_then<'a, 'tcx: 'a, T: LintContext<'tcx>, F>(
/// --> $DIR/methods.rs:390:13
/// |
/// 390 | let _ = (0..3).fold(false, |acc, x| acc || x > 2);
/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)`
/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)`
/// |
/// = note: `-D fold-any` implied by `-D warnings`
/// </pre>

View File

@ -400,6 +400,11 @@ fn fold_any_ignores_non_boolean_accumalator() {
let _ = (0..3).fold(0, |acc, x| acc + if x > 2 { 1 } else { 0 });
}
/// Should trigger the `FOLD_ANY` lint, with the error span including exactly `.fold(...)`
fn fold_any_span_for_multi_element_chain() {
let _ = (0..3).map(|x| 2 * x).fold(false, |acc, x| acc || x > 2);
}
#[allow(similar_names)]
fn main() {
let opt = Some(0);

View File

@ -494,17 +494,23 @@ error: called `skip(x).next()` on an iterator. This is more succinctly expressed
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: this `.fold` can more succintly be expressed as `.any`
--> $DIR/methods.rs:390:13
--> $DIR/methods.rs:390:19
|
390 | let _ = (0..3).fold(false, |acc, x| acc || x > 2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| 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:406:13
error: this `.fold` can more succintly be expressed as `.any`
--> $DIR/methods.rs:405:34
|
406 | let _ = opt.unwrap();
405 | let _ = (0..3).map(|x| 2 * x).fold(false, |acc, x| acc || x > 2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)`
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:411:13
|
411 | let _ = opt.unwrap();
| ^^^^^^^^^^^^
|
= note: `-D option-unwrap-used` implied by `-D warnings`