Skip to content

Commit 48f1b17

Browse files
committed
Documentation changes and bugfixes
1 parent 7751283 commit 48f1b17

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

clippy_lints/src/option_if_let_else.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
1010
declare_clippy_lint! {
1111
/// **What it does:**
1212
/// Detects people who use `if let Some(v) = ... { y } else { x }`
13-
/// when they could use `Option::map_or` instead.
13+
/// when they could use `Option::map_or` or `Option::map_or_else`
14+
/// instead.
1415
///
1516
/// **Why is this bad?**
1617
/// Using the dedicated function in the Option class is clearer and
17-
/// more concise than an if let
18+
/// more concise than an if let expression.
1819
///
1920
/// **Known problems:**
20-
/// Using `Option::map_or` requires it to compute the or value every
21-
/// function call, even if the value is unneeded. In some circumstances,
22-
/// it might be better to use Option::map_or_else. For example,
21+
/// Using `Option::map_or` (as the lint presently suggests) requires
22+
/// it to compute the else value every function call, even if the
23+
/// value is unneeded. It might be better to use Option::map_or_else
24+
/// if the code is either very expensive or if it changes some state
25+
/// (i.e. mutates a value). For example,
2326
/// ```rust
2427
/// # let optional = Some(0);
2528
/// let _ = if let Some(x) = optional {
@@ -67,9 +70,10 @@ declare_clippy_lint! {
6770

6871
declare_lint_pass!(OptionIfLetElse => [OPTION_IF_LET_ELSE]);
6972

70-
/// If this is the option if let/else thing we're detecting, then this
71-
/// function returns Some(Option<_> compared, expression if Option is
72-
/// Some, expression if Option is None). Otherwise, it returns None
73+
/// If this expression is the option if let/else construct we're
74+
/// detecting, then this function returns Some(Option<_> compared,
75+
/// expression if Option is Some, expression if Option is None).
76+
/// Otherwise, it returns None.
7377
fn detect_option_if_let_else<'a>(cx: &LateContext<'_, '_>, expr: &'a Expr<'_>) -> Option<(String, String, String)> {
7478
if_chain! {
7579
if let ExprKind::Match(let_body, arms, MatchSource::IfLetDesugar{contains_else_clause: true}) = &expr.kind;
@@ -112,9 +116,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OptionIfLetElse {
112116
cx,
113117
OPTION_IF_LET_ELSE,
114118
expr.span,
115-
"use Option::map_or_else instead of an if let/else",
119+
"use Option::map_or instead of an if let/else",
116120
"try",
117-
format!("{}.map_or_else({}, {})", option, else_func, map),
121+
format!("{}.map_or({}, {})", option, else_func, map),
118122
Applicability::MaybeIncorrect,
119123
);
120124
}

tests/ui/author/blocks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![feature(stmt_expr_attributes)]
2-
#![allow(redundant_semicolons, clippy::no_effect)]
2+
#![allow(redundant_semicolon, clippy::no_effect)]
33

44
#[rustfmt::skip]
55
fn main() {

tests/ui/option_if_let_else.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
error: use Option::map_or_else instead of an if let/else
1+
error: use Option::map_or instead of an if let/else
22
--> $DIR/option_if_let_else.rs:4:5
33
|
44
LL | / if let Some(x) = string {
55
LL | | (true, x)
66
LL | | } else {
77
LL | | (false, "hello")
88
LL | | }
9-
| |_____^ help: try: `string.map_or_else((false, "hello"), |x| (true, x))`
9+
| |_____^ help: try: `string.map_or((false, "hello"), |x| (true, x))`
1010
|
1111
= note: `-D clippy::option-if-let-else` implied by `-D warnings`
1212

13-
error: use Option::map_or_else instead of an if let/else
13+
error: use Option::map_or instead of an if let/else
1414
--> $DIR/option_if_let_else.rs:12:5
1515
|
1616
LL | / if let Some(x) = arg {
@@ -23,17 +23,17 @@ LL | | }
2323
|
2424
help: try
2525
|
26-
LL | arg.map_or_else(13, |x| {
26+
LL | arg.map_or(13, |x| {
2727
LL | let y = x * x;
2828
LL | y * y
2929
LL | })
3030
|
3131

32-
error: use Option::map_or_else instead of an if let/else
32+
error: use Option::map_or instead of an if let/else
3333
--> $DIR/option_if_let_else.rs:26:13
3434
|
3535
LL | let _ = if let Some(x) = optional { x + 2 } else { 5 };
36-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `optional.map_or_else(5, |x| x + 2)`
36+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `optional.map_or(5, |x| x + 2)`
3737

3838
error: aborting due to 3 previous errors
3939

tests/ui/swap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
clippy::blacklisted_name,
66
clippy::no_effect,
77
clippy::redundant_clone,
8-
redundant_semicolons,
8+
redundant_semicolon,
99
unused_assignments
1010
)]
1111

0 commit comments

Comments
 (0)