Skip to content

Commit 1f75845

Browse files
committed
Reduce indentation and add comment about lint name
1 parent ffe7125 commit 1f75845

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

clippy_lints/src/option_if_let_else.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ declare_clippy_lint! {
6464
/// y*y
6565
/// }, |foo| foo);
6666
/// ```
67+
// FIXME: Before moving this lint out of nursery, the lint name needs to be updated. It now also
68+
// covers matches and `Result`.
6769
#[clippy::version = "1.47.0"]
6870
pub OPTION_IF_LET_ELSE,
6971
nursery,
@@ -235,24 +237,25 @@ fn is_none_or_err_arm(cx: &LateContext<'_>, arm: &Arm<'_>) -> bool {
235237

236238
impl<'tcx> LateLintPass<'tcx> for OptionIfLetElse {
237239
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
238-
// Don't lint macros, because it behaves weirdly
239-
// and don't lint constant as well
240-
if !expr.span.from_expansion() && !in_constant(cx, expr.hir_id) {
241-
let detection = detect_option_if_let_else(cx, expr).or_else(|| detect_option_match(cx, expr));
242-
if let Some(det) = detection {
243-
span_lint_and_sugg(
244-
cx,
245-
OPTION_IF_LET_ELSE,
246-
expr.span,
247-
format!("use Option::{} instead of an if let/else", det.method_sugg).as_str(),
248-
"try",
249-
format!(
250-
"{}.{}({}, {})",
251-
det.option, det.method_sugg, det.none_expr, det.some_expr
252-
),
253-
Applicability::MaybeIncorrect,
254-
);
255-
}
240+
// Don't lint macros and constants
241+
if expr.span.from_expansion() || in_constant(cx, expr.hir_id) {
242+
return;
243+
}
244+
245+
let detection = detect_option_if_let_else(cx, expr).or_else(|| detect_option_match(cx, expr));
246+
if let Some(det) = detection {
247+
span_lint_and_sugg(
248+
cx,
249+
OPTION_IF_LET_ELSE,
250+
expr.span,
251+
format!("use Option::{} instead of an if let/else", det.method_sugg).as_str(),
252+
"try",
253+
format!(
254+
"{}.{}({}, {})",
255+
det.option, det.method_sugg, det.none_expr, det.some_expr
256+
),
257+
Applicability::MaybeIncorrect,
258+
);
256259
}
257260
}
258261
}

tests/ui/option_if_let_else.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ LL ~ });
207207
|
208208

209209
error: use Option::map_or instead of an if let/else
210-
--> $DIR/option_if_let_else.rs:212:13
210+
--> $DIR/option_if_let_else.rs:213:13
211211
|
212212
LL | let _ = match s {
213213
| _____________^
@@ -217,7 +217,7 @@ LL | | };
217217
| |_____^ help: try: `s.map_or(1, |string| string.len())`
218218

219219
error: use Option::map_or instead of an if let/else
220-
--> $DIR/option_if_let_else.rs:216:13
220+
--> $DIR/option_if_let_else.rs:217:13
221221
|
222222
LL | let _ = match Some(10) {
223223
| _____________^
@@ -227,7 +227,7 @@ LL | | };
227227
| |_____^ help: try: `Some(10).map_or(5, |a| a + 1)`
228228

229229
error: use Option::map_or instead of an if let/else
230-
--> $DIR/option_if_let_else.rs:222:13
230+
--> $DIR/option_if_let_else.rs:223:13
231231
|
232232
LL | let _ = match res {
233233
| _____________^
@@ -237,7 +237,7 @@ LL | | };
237237
| |_____^ help: try: `res.map_or(1, |a| a + 1)`
238238

239239
error: use Option::map_or instead of an if let/else
240-
--> $DIR/option_if_let_else.rs:226:13
240+
--> $DIR/option_if_let_else.rs:227:13
241241
|
242242
LL | let _ = match res {
243243
| _____________^
@@ -247,7 +247,7 @@ LL | | };
247247
| |_____^ help: try: `res.map_or(1, |a| a + 1)`
248248

249249
error: use Option::map_or instead of an if let/else
250-
--> $DIR/option_if_let_else.rs:230:13
250+
--> $DIR/option_if_let_else.rs:231:13
251251
|
252252
LL | let _ = if let Ok(a) = res { a + 1 } else { 5 };
253253
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `res.map_or(5, |a| a + 1)`

0 commit comments

Comments
 (0)