unify the lints

This commit is contained in:
Oliver Schneider 2016-06-22 10:44:46 +02:00
parent ac6e7b2957
commit 5ccbf3d437
No known key found for this signature in database
GPG key ID: 56D6EEA0FC67AC46
5 changed files with 30 additions and 42 deletions

View file

@ -268,9 +268,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
items_after_statements::ITEMS_AFTER_STATEMENTS,
matches::SINGLE_MATCH_ELSE,
mem_forget::MEM_FORGET,
methods::FILTER_FLAT_MAP,
methods::FILTER_MAP,
methods::FILTER_MAP_FLAT_MAP,
methods::OPTION_UNWRAP_USED,
methods::RESULT_UNWRAP_USED,
methods::WRONG_PUB_SELF_CONVENTION,

View file

@ -159,11 +159,11 @@ declare_lint! {
"using `filter(p).next()`, which is more succinctly expressed as `.find(p)`"
}
/// **What it does:** This lint `Warn`s on `_.filter(_).map(_)`.
/// **What it does:** This lint `Warn`s on `_.filter(_).map(_)`, `_.filter(_).flat_map(_)`, `_.filter_map(_).flat_map(_)` and similar.
///
/// **Why is this bad?** Readability, this can be written more concisely as `_.filter_map(_)`.
/// **Why is this bad?** Readability, this can be written more concisely as a single method call
///
/// **Known problems:** Often requires a condition + Option creation in `filter_map`
/// **Known problems:** Often requires a condition + Option/Iterator creation inside the closure
///
/// **Example:** `iter.filter(|x| x == 0).map(|x| x * 2)`
declare_lint! {
@ -171,30 +171,6 @@ declare_lint! {
"using `filter(_).map(_)`, which is more succinctly expressed as `.filter_map(_)`"
}
/// **What it does:** This lint `Warn`s on `_.filter(_).flat_map(_)`.
///
/// **Why is this bad?** Readability, this just needs the `flat_map` to return an empty iterator, if the value should be filtered.
///
/// **Known problems:** Often requires a condition + Iterator creation in `flat_map`
///
/// **Example:** `iter.filter(|x| x == 0).flat_map(|x| x.bits())`
declare_lint! {
pub FILTER_FLAT_MAP, Allow,
"using `filter(_).flat_map(_)`, which can be rewritten using just the flat_map"
}
/// **What it does:** This lint `Warn`s on `_.filter_map(_).flat_map(_)`.
///
/// **Why is this bad?** Readability, this just needs the `flat_map` to return an empty iterator, if the value should be filtered.
///
/// **Known problems:** Often requires a condition + Iterator creation in `flat_map`
///
/// **Example:** `iter.filter_map(|x| x.process()).flat_map(|x| x.bits())`
declare_lint! {
pub FILTER_MAP_FLAT_MAP, Allow,
"using `filter_map(_).flat_map(_)`, which can be rewritten using just the flat_map"
}
/// **What it does:** This lint `Warn`s on an iterator search (such as `find()`, `position()`, or
/// `rposition()`) followed by a call to `is_some()`.
///
@ -393,8 +369,6 @@ impl LintPass for Pass {
SEARCH_IS_SOME,
TEMPORARY_CSTRING_AS_PTR,
FILTER_MAP,
FILTER_FLAT_MAP,
FILTER_MAP_FLAT_MAP,
ITER_NTH)
}
}
@ -420,6 +394,8 @@ impl LateLintPass for Pass {
lint_filter_next(cx, expr, arglists[0]);
} else if let Some(arglists) = method_chain_args(expr, &["filter", "map"]) {
lint_filter_map(cx, expr, arglists[0], arglists[1]);
} else if let Some(arglists) = method_chain_args(expr, &["filter_map", "map"]) {
lint_filter_map_map(cx, expr, arglists[0], arglists[1]);
} else if let Some(arglists) = method_chain_args(expr, &["filter", "flat_map"]) {
lint_filter_flat_map(cx, expr, arglists[0], arglists[1]);
} else if let Some(arglists) = method_chain_args(expr, &["filter_map", "flat_map"]) {
@ -884,8 +860,19 @@ fn lint_filter_next(cx: &LateContext, expr: &hir::Expr, filter_args: &MethodArgs
fn lint_filter_map(cx: &LateContext, expr: &hir::Expr, _filter_args: &MethodArgs, _map_args: &MethodArgs) {
// 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.";
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);
}
}
// Type of MethodArgs is potentially a Vec
/// lint use of `filter().map()` for `Iterators`
fn lint_filter_map_map(cx: &LateContext, expr: &hir::Expr, _filter_args: &MethodArgs, _map_args: &MethodArgs) {
// 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);
}
}
@ -895,9 +882,10 @@ fn lint_filter_map(cx: &LateContext, expr: &hir::Expr, _filter_args: &MethodArgs
fn lint_filter_flat_map(cx: &LateContext, expr: &hir::Expr, _filter_args: &MethodArgs, _map_args: &MethodArgs) {
// 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(..)` \
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_FLAT_MAP, expr.span, msg);
span_lint(cx, FILTER_MAP, expr.span, msg);
}
}
@ -906,9 +894,10 @@ fn lint_filter_flat_map(cx: &LateContext, expr: &hir::Expr, _filter_args: &Metho
fn lint_filter_map_flat_map(cx: &LateContext, expr: &hir::Expr, _filter_args: &MethodArgs, _map_args: &MethodArgs) {
// 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(..)` \
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_FLAT_MAP, expr.span, msg);
span_lint(cx, FILTER_MAP, expr.span, msg);
}
}