Clean up methods/mod.rs

This commit is contained in:
Yuki Okushi 2020-01-24 17:04:37 +09:00
parent 2c7cfa8321
commit 2dc73c45d5

View File

@ -2133,14 +2133,12 @@ fn lint_iter_nth<'a, 'tcx>(
return; // caller is not a type that we want to lint
};
span_lint(
span_help_and_lint(
cx,
ITER_NTH,
expr.span,
&format!(
"called `.iter{0}().nth()` on a {1}. Calling `.get{0}()` is both faster and more readable",
mut_str, caller_type
),
&format!("called `.iter{0}().nth()` on a {1}", mut_str, caller_type),
&format!("Calling `.get{}()` is both faster and more readable", mut_str),
);
}
@ -2244,11 +2242,12 @@ fn lint_get_unwrap<'a, 'tcx>(
fn lint_iter_skip_next(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>) {
// lint if caller of skip is an Iterator
if match_trait_method(cx, expr, &paths::ITERATOR) {
span_lint(
span_help_and_lint(
cx,
ITER_SKIP_NEXT,
expr.span,
"called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`",
"called `skip(x).next()` on an iterator",
"This is more succinctly expressed by calling `nth(x)`.",
);
}
}
@ -2304,15 +2303,15 @@ fn lint_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, unwrap_args: &[hi
};
if let Some((lint, kind, none_value)) = mess {
span_lint(
span_help_and_lint(
cx,
lint,
expr.span,
&format!("used `unwrap()` on `{}` value", kind,),
&format!(
"used `unwrap()` on `{}` value. If you don't want to handle the `{}` case gracefully, consider \
using `expect()` to provide a better panic \
message",
kind, none_value
"If you don't want to handle the `{}` case gracefully, consider \
using `expect()` to provide a better panic message.",
none_value,
),
);
}
@ -2331,14 +2330,12 @@ fn lint_expect(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, expect_args: &[hi
};
if let Some((lint, kind, none_value)) = mess {
span_lint(
span_help_and_lint(
cx,
lint,
expr.span,
&format!(
"used `expect()` on `{}` value. If this value is an `{}` it will panic",
kind, none_value
),
&format!("used `expect()` on `{}` value", kind,),
&format!("If this value is an `{}`, it will panic.", none_value,),
);
}
}
@ -2353,11 +2350,12 @@ fn lint_ok_expect(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, ok_args: &[hir
if has_debug_impl(error_type, cx);
then {
span_lint(
span_help_and_lint(
cx,
OK_EXPECT,
expr.span,
"called `ok().expect()` on a `Result` value. You can call `expect()` directly on the `Result`",
"called `ok().expect()` on a `Result` value",
"You can call `expect()` directly on the `Result`",
);
}
}
@ -2372,14 +2370,15 @@ fn lint_map_flatten<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<
let self_snippet = snippet(cx, map_args[0].span, "..");
let func_snippet = snippet(cx, map_args[1].span, "..");
let hint = format!("{0}.flat_map({1})", self_snippet, func_snippet);
span_lint_and_then(cx, MAP_FLATTEN, expr.span, msg, |db| {
db.span_suggestion(
expr.span,
"try using `flat_map` instead",
hint,
Applicability::MachineApplicable,
);
});
span_lint_and_sugg(
cx,
MAP_FLATTEN,
expr.span,
msg,
"try using `flat_map` instead",
hint,
Applicability::MachineApplicable,
);
}
}
@ -2474,14 +2473,15 @@ fn lint_map_or_none<'a, 'tcx>(
let map_or_self_snippet = snippet(cx, map_or_args[0].span, "..");
let map_or_func_snippet = snippet(cx, map_or_args[2].span, "..");
let hint = format!("{0}.and_then({1})", map_or_self_snippet, map_or_func_snippet);
span_lint_and_then(cx, OPTION_MAP_OR_NONE, expr.span, msg, |db| {
db.span_suggestion(
expr.span,
"try using `and_then` instead",
hint,
Applicability::MachineApplicable, // snippet
);
});
span_lint_and_sugg(
cx,
OPTION_MAP_OR_NONE,
expr.span,
msg,
"try using `and_then` instead",
hint,
Applicability::MachineApplicable,
);
}
}
}
@ -2607,9 +2607,9 @@ fn lint_filter_map<'a, 'tcx>(
) {
// lint if caller of `.filter().map()` is an Iterator
if match_trait_method(cx, expr, &paths::ITERATOR) {
let msg = "called `filter(p).map(q)` on an `Iterator`. \
This is more succinctly expressed by calling `.filter_map(..)` instead.";
span_lint(cx, FILTER_MAP, expr.span, msg);
let msg = "called `filter(p).map(q)` on an `Iterator`";
let hint = "This is more succinctly expressed by calling `.filter_map(..)` instead.";
span_help_and_lint(cx, FILTER_MAP, expr.span, msg, hint);
}
}
@ -2647,9 +2647,9 @@ fn lint_find_map<'a, 'tcx>(
) {
// lint if caller of `.filter().map()` is an Iterator
if match_trait_method(cx, &map_args[0], &paths::ITERATOR) {
let msg = "called `find(p).map(q)` on an `Iterator`. \
This is more succinctly expressed by calling `.find_map(..)` instead.";
span_lint(cx, FIND_MAP, expr.span, msg);
let msg = "called `find(p).map(q)` on an `Iterator`";
let hint = "This is more succinctly expressed by calling `.find_map(..)` instead.";
span_help_and_lint(cx, FIND_MAP, expr.span, msg, hint);
}
}
@ -2662,9 +2662,9 @@ fn lint_filter_map_map<'a, 'tcx>(
) {
// lint if caller of `.filter().map()` is an Iterator
if match_trait_method(cx, expr, &paths::ITERATOR) {
let msg = "called `filter_map(p).map(q)` on an `Iterator`. \
This is more succinctly expressed by only calling `.filter_map(..)` instead.";
span_lint(cx, FILTER_MAP, expr.span, msg);
let msg = "called `filter_map(p).map(q)` on an `Iterator`";
let hint = "This is more succinctly expressed by only calling `.filter_map(..)` instead.";
span_help_and_lint(cx, FILTER_MAP, expr.span, msg, hint);
}
}
@ -2677,10 +2677,10 @@ fn lint_filter_flat_map<'a, 'tcx>(
) {
// lint if caller of `.filter().flat_map()` is an Iterator
if match_trait_method(cx, expr, &paths::ITERATOR) {
let msg = "called `filter(p).flat_map(q)` on an `Iterator`. \
This is more succinctly expressed by calling `.flat_map(..)` \
and filtering by returning an empty Iterator.";
span_lint(cx, FILTER_MAP, expr.span, msg);
let msg = "called `filter(p).flat_map(q)` on an `Iterator`";
let hint = "This is more succinctly expressed by calling `.flat_map(..)` \
and filtering by returning an empty Iterator.";
span_help_and_lint(cx, FILTER_MAP, expr.span, msg, hint);
}
}
@ -2693,10 +2693,10 @@ fn lint_filter_map_flat_map<'a, 'tcx>(
) {
// lint if caller of `.filter_map().flat_map()` is an Iterator
if match_trait_method(cx, expr, &paths::ITERATOR) {
let msg = "called `filter_map(p).flat_map(q)` on an `Iterator`. \
This is more succinctly expressed by calling `.flat_map(..)` \
and filtering by returning an empty Iterator.";
span_lint(cx, FILTER_MAP, expr.span, msg);
let msg = "called `filter_map(p).flat_map(q)` on an `Iterator`";
let hint = "This is more succinctly expressed by calling `.flat_map(..)` \
and filtering by returning an empty Iterator.";
span_help_and_lint(cx, FILTER_MAP, expr.span, msg, hint);
}
}