Skip to content

Commit

Permalink
Auto merge of #9722 - ebobrow:question-mark, r=Manishearth
Browse files Browse the repository at this point in the history
`question_mark` don't lint on `if let Err` with `else`

cc #9518

AFAICT the only time this would be a valid suggestion is the rather esoteric

```rust
let _ = if let Err(e) = x {
    return Err(e);
} else {
    // no side effects
    x.unwrap()
}
```

which doesn't seem worth checking to me. Please correct me if I'm missing something.

changelog: [`question_mark`] don't lint on `if let Err` with `else`
  • Loading branch information
bors committed Oct 27, 2022
2 parents 710999d + 98250af commit f5d225d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions clippy_lints/src/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ fn is_early_return(smbl: Symbol, cx: &LateContext<'_>, if_block: &IfBlockType<'_
&& expr_return_none_or_err(smbl, cx, if_else.unwrap(), let_expr, Some(let_pat_sym)))
|| is_res_lang_ctor(cx, res, ResultErr)
&& expr_return_none_or_err(smbl, cx, if_then, let_expr, Some(let_pat_sym))
&& if_else.is_none()
},
_ => false,
}
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/question_mark.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
return func_returning_result();
}

// no warning
let _ = if let Err(e) = x { Err(e) } else { Ok(0) };

Ok(y)
}

Expand Down
3 changes: 3 additions & 0 deletions tests/ui/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
return func_returning_result();
}

// no warning
let _ = if let Err(e) = x { Err(e) } else { Ok(0) };

Ok(y)
}

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/question_mark.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ LL | | }
| |_____^ help: replace it with: `x?;`

error: this block may be rewritten with the `?` operator
--> $DIR/question_mark.rs:193:5
--> $DIR/question_mark.rs:196:5
|
LL | / if let Err(err) = func_returning_result() {
LL | | return Err(err);
LL | | }
| |_____^ help: replace it with: `func_returning_result()?;`

error: this block may be rewritten with the `?` operator
--> $DIR/question_mark.rs:200:5
--> $DIR/question_mark.rs:203:5
|
LL | / if let Err(err) = func_returning_result() {
LL | | return Err(err);
Expand Down

0 comments on commit f5d225d

Please sign in to comment.