Skip to content

Commit f5c941a

Browse files
committed
Update OPTION_MAP_UNWRAP_OR lint
Add a suggestion to replace `map(f).unwrap_or(None)` with `and_then(f)`.
1 parent 12a7d14 commit f5c941a

File tree

3 files changed

+260
-244
lines changed

3 files changed

+260
-244
lines changed

clippy_lints/src/methods.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,29 +1150,35 @@ fn lint_ok_expect(cx: &LateContext, expr: &hir::Expr, ok_args: &[hir::Expr]) {
11501150
fn lint_map_unwrap_or(cx: &LateContext, expr: &hir::Expr, map_args: &[hir::Expr], unwrap_args: &[hir::Expr]) {
11511151
// lint if the caller of `map()` is an `Option`
11521152
if match_type(cx, cx.tables.expr_ty(&map_args[0]), &paths::OPTION) {
1153-
// lint message
1154-
let msg = "called `map(f).unwrap_or(a)` on an Option value. This can be done more directly by calling \
1155-
`map_or(a, f)` instead";
11561153
// get snippets for args to map() and unwrap_or()
11571154
let map_snippet = snippet(cx, map_args[1].span, "..");
11581155
let unwrap_snippet = snippet(cx, unwrap_args[1].span, "..");
1156+
// lint message
1157+
let arg = if unwrap_snippet == "None" { "None" } else { "a" };
1158+
let suggest = if unwrap_snippet == "None" { "and_then(f)" } else { "map_or(a, f)" };
1159+
let msg = &format!(
1160+
"called `map(f).unwrap_or({})` on an Option value. \
1161+
This can be done more directly by calling `{}` instead",
1162+
arg,
1163+
suggest
1164+
);
11591165
// lint, with note if neither arg is > 1 line and both map() and
11601166
// unwrap_or() have the same span
11611167
let multiline = map_snippet.lines().count() > 1 || unwrap_snippet.lines().count() > 1;
11621168
let same_span = map_args[1].span.ctxt() == unwrap_args[1].span.ctxt();
11631169
if same_span && !multiline {
1164-
span_note_and_lint(
1165-
cx,
1166-
OPTION_MAP_UNWRAP_OR,
1167-
expr.span,
1168-
msg,
1169-
expr.span,
1170-
&format!(
1171-
"replace `map({0}).unwrap_or({1})` with `map_or({1}, {0})`",
1172-
map_snippet,
1173-
unwrap_snippet
1174-
),
1170+
let suggest = if unwrap_snippet == "None" {
1171+
format!("and_then({})", map_snippet)
1172+
} else {
1173+
format!("map_or({}, {})", unwrap_snippet, map_snippet)
1174+
};
1175+
let note = format!(
1176+
"replace `map({}).unwrap_or({})` with `{}`",
1177+
map_snippet,
1178+
unwrap_snippet,
1179+
suggest
11751180
);
1181+
span_note_and_lint(cx, OPTION_MAP_UNWRAP_OR, expr.span, msg, expr.span, &note);
11761182
} else if same_span && multiline {
11771183
span_lint(cx, OPTION_MAP_UNWRAP_OR, expr.span, msg);
11781184
};

tests/ui/methods.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ fn option_methods() {
108108
.unwrap_or({
109109
0
110110
});
111+
// map(f).unwrap_or(None) case
112+
let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
111113
// macro case
112114
let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
113115

0 commit comments

Comments
 (0)