Add ignored_result_err lint to detect discarded Result error variants - #17326
Add ignored_result_err lint to detect discarded Result error variants#17326scuzzycheese wants to merge 1 commit into
ignored_result_err lint to detect discarded Result error variants#17326Conversation
|
Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @dswij (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
Why was this reviewer chosen?The reviewer was selected based on:
|
|
Lintcheck changes for bf50705
This comment will be updated if you push new changes |
|
r? clippy |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
49263b1 to
714fead
Compare
714fead to
39df31c
Compare
|
Re-opening PR, I made mistake by resetting my origin. |
|
@rustbot ready |
|
Hi @flip1995 |
This comment has been minimized.
This comment has been minimized.
714fead to
efc87cd
Compare
This comment has been minimized.
This comment has been minimized.
|
@rustbot ready |
This comment has been minimized.
This comment has been minimized.
efc87cd to
e2ccefa
Compare
This comment has been minimized.
This comment has been minimized.
|
@rustbot ready |
|
Hi @flip1995 |
This comment has been minimized.
This comment has been minimized.
e2ccefa to
718b619
Compare
This comment has been minimized.
This comment has been minimized.
|
Hi @flip1995 |
This comment has been minimized.
This comment has been minimized.
718b619 to
b25cce9
Compare
This comment has been minimized.
This comment has been minimized.
|
Hi @flip1995 |
| if let Ok(res) = some_call() { | ||
| //~^ ignored_result_err | ||
| println!("{res}"); | ||
| } else { |
There was a problem hiding this comment.
One more idea for a test: else if Err(e) = some_call()
This should not lint, as the error is not discarded.
Before trying to detect this in this lint, please check if there is already a lint for this pattern (also in pedantic/restriction) . If there is, the lint added in this PR doesn't need to also cover it.
There was a problem hiding this comment.
I just want to clarify, do you mean adding a test like this?
// Should NOT lint — `if let Err(e)` binds the error, it is not discarded
if let Err(e) = some_call() {
println!("failed: {e}");
}
Or do you mean specifically on conjunction with the Ok Branch?
if let Ok(res) = some_call() {
//~^ ignored_result_err
println!("{res}");
} else if let Err(e) = some_call() {
println!("failed: {e}");
}
My assumption is that those are two separate calls to some_call(), and the error for the first call is discarded, but not discarded for the second call.
I'm going to assume you're talking about the first example which shouldn't lint, and the second one should still lint.
Unless I'm completely misunderstanding what you mean. :D
There was a problem hiding this comment.
I was talking about the second example.. kinda. The first is also a good test to add though.
Small modification for the second example: only call some_call once:
let sc = some_call()
if let Ok(res) = sc {
//~^ ignored_result_err
println!("{res}");
} else if let Err(e) = sc {
println!("failed: {e}");
}Please check if another lint also detects this pattern and if so, no need to spend time on making this lint detect it.
There was a problem hiding this comment.
I couldn't find any lints that detect this pattern.
The ones that are a closest match are:
- redundant_pattern_matching
- This detects
if let Ok(_) = x where the binding is unused → .is_ok() - However our arms bind and use res/e
- This detects
- needless_match
- This detects if-let/match that reconstructs the same value
(Ok(v) => Ok(v), Some(v)=>Some(v)). A no-op - My arms don't reconstruct; they consume the values
- This detects if-let/match that reconstructs the same value
- manual_ok_err
- This detect arms that map to
Some(v)/None → .ok()/.err() - Our arms produce side effects (
println!), notOption
- This detect arms that map to
- option_if_let_else
- This detects
if let Some(v) = opt {…} else {…} → map_or - Option only, and single
if let/else, notOk + else if let Err
- This detects
Anyway, I've updated this to detect that pattern, and it catches your use case. Nice catch! Thank you!
c3aee70 to
df410d8
Compare
This comment has been minimized.
This comment has been minimized.
|
This lint has been nominated for inclusion. |
df410d8 to
74600f6
Compare
This comment has been minimized.
This comment has been minimized.
74600f6 to
3c48edf
Compare
This comment has been minimized.
This comment has been minimized.
3c48edf to
0b0e132
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Add a new restriction lint that warns when the Err variant of a Result
is implicitly discarded. The lint detects three patterns:
- `if let Ok(x) = expr` (with or without else)
- `while let Ok(x) = expr`
- `let Ok(x) = expr else { ... }`
In all these cases, the error value is lost, preventing detailed logging
and making error recovery impossible. The lint suggests using `match`
with an explicit `Err(e)` binding instead.
This is an opt-in restriction lint, enabled with:
#![warn(clippy::ignored_result_err)]
A configuration option `allow-ignored-result-err-in-tests = true` can be
set in clippy.toml to suppress the lint in `#[test]` functions and
`#[cfg(test)]` modules.
Tested via the compile-test UI test harness with cases covering all three
patterns, a negative case for `match` with bound Err, and a ui-toml test
verifying the allow-in-tests configuration works correctly.
0b0e132 to
bf50705
Compare
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
View all comments
changelog: new lint: [
ignored_result_err]: Addignored_result_errlint to detect discarded Result error variants. fixes #16107