Skip to content

Add lint for opt.map_or(None, f) #2128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions clippy_lints/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,24 @@ declare_lint! {
`map_or_else(g, f)`"
}

/// **What it does:** Checks for usage of `_.map_or(None, _)`.
///
/// **Why is this bad?** Readability, this can be written more concisely as
/// `_.and_then(_)`.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// opt.map_or(None, |a| a + 1)
/// ```
declare_lint! {
pub OPTION_MAP_OR_NONE,
Warn,
"using `Option.map_or(None, f)`, which is more succinctly expressed as \
`map_or_else(g, f)`"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy paste error

}

/// **What it does:** Checks for usage of `_.filter(_).next()`.
///
/// **Why is this bad?** Readability, this can be written more concisely as
Expand Down Expand Up @@ -574,6 +592,7 @@ impl LintPass for Pass {
OK_EXPECT,
OPTION_MAP_UNWRAP_OR,
OPTION_MAP_UNWRAP_OR_ELSE,
OPTION_MAP_OR_NONE,
OR_FUN_CALL,
CHARS_NEXT_CMP,
CHARS_LAST_CMP,
Expand Down Expand Up @@ -620,6 +639,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
lint_map_unwrap_or(cx, expr, arglists[0], arglists[1]);
} else if let Some(arglists) = method_chain_args(expr, &["map", "unwrap_or_else"]) {
lint_map_unwrap_or_else(cx, expr, arglists[0], arglists[1]);
} else if let Some(arglists) = method_chain_args(expr, &["map_or"]) {
lint_map_or_none(cx, expr, arglists[0]);
} else if let Some(arglists) = method_chain_args(expr, &["filter", "next"]) {
lint_filter_next(cx, expr, arglists[0]);
} else if let Some(arglists) = method_chain_args(expr, &["filter", "map"]) {
Expand Down Expand Up @@ -1220,6 +1241,37 @@ fn lint_map_unwrap_or_else<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir
}
}

/// lint use of `_.map_or(None, _)` for `Option`s
fn lint_map_or_none<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr, map_or_args: &'tcx [hir::Expr]) {
// check if the first non-self argument to map_or() is None
let map_or_arg_is_none = if let hir::Expr_::ExprPath(ref qpath) = map_or_args[1].node {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to ensure that the type is Option before doing this check. Otherwise a random type with a map_or function can cause this lint to panic if the map_or has zero arguments (so just the self).

match_qpath(qpath, &paths::OPTION_NONE)
} else {
false
};

if match_type(cx, cx.tables.expr_ty(&map_or_args[0]), &paths::OPTION) && map_or_arg_is_none {
// lint message
let msg = "called `map_or(None, f)` on an Option value. This can be done more directly by calling \
`and_then(f)` instead";
let map_or_none_snippet = snippet(cx, map_or_args[1].span, "..");
let map_or_func_snippet = snippet(cx, map_or_args[2].span, "..");
let multiline = map_or_func_snippet.lines().count() > 1 || map_or_none_snippet.lines().count() > 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please get rid of the multiline check (I know all the others still do it, but they are rather old lints). Use span_lint_and_then + span_suggestion to produce the suggestion. Rustc will take care of the rest.

if multiline {
span_lint(cx, OPTION_MAP_OR_NONE, expr.span, msg);
} else {
span_note_and_lint(
cx,
OPTION_MAP_OR_NONE,
expr.span,
msg,
expr.span,
&format!("replace `map_or({0}, {1})` with `and_then({1})`", map_or_none_snippet, map_or_func_snippet)
);
}
}
}

/// lint use of `filter().next()` for `Iterators`
fn lint_filter_next<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr, filter_args: &'tcx [hir::Expr]) {
// lint if caller of `.filter().next()` is an Iterator
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ macro_rules! opt_map {
/// Checks implementation of the following lints:
/// * `OPTION_MAP_UNWRAP_OR`
/// * `OPTION_MAP_UNWRAP_OR_ELSE`
/// * `OPTION_MAP_OR_NONE`
fn option_methods() {
let opt = Some(1);

Expand Down Expand Up @@ -137,6 +138,15 @@ fn option_methods() {
);
// macro case
let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0); // should not lint

// Check OPTION_MAP_OR_NONE
// single line case
let _ = opt.map_or(None, |x| Some(x + 1));
// multi line case
let _ = opt.map_or(None, |x| {
Some(x + 1)
}
);
}

/// Struct to generate false positives for things with .iter()
Expand Down
Loading