-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #11864 - GuillaumeGomez:option_map_or_err_ok, r=flip1995
Create new lint `option_map_or_err_ok` Fixes #10045. For the following code: ```rust let opt = Some(1); opt.map_or(Err("error"), Ok); ``` It suggests to instead write: ```rust let opt = Some(1); opt.ok_or("error"); ``` r? `@flip1995` changelog: Create new lint `option_map_or_err_ok`
- Loading branch information
Showing
8 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet; | ||
use clippy_utils::ty::is_type_diagnostic_item; | ||
use clippy_utils::{is_res_lang_ctor, path_res}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::LangItem::{ResultErr, ResultOk}; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::symbol::sym; | ||
|
||
use super::OPTION_MAP_OR_ERR_OK; | ||
|
||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx Expr<'tcx>, | ||
recv: &'tcx Expr<'_>, | ||
or_expr: &'tcx Expr<'_>, | ||
map_expr: &'tcx Expr<'_>, | ||
) { | ||
// We check that it's called on an `Option` type. | ||
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Option) | ||
// We check that first we pass an `Err`. | ||
&& let ExprKind::Call(call, &[arg]) = or_expr.kind | ||
&& is_res_lang_ctor(cx, path_res(cx, call), ResultErr) | ||
// And finally we check that it is mapped as `Ok`. | ||
&& is_res_lang_ctor(cx, path_res(cx, map_expr), ResultOk) | ||
{ | ||
let msg = "called `map_or(Err(_), Ok)` on an `Option` value"; | ||
let self_snippet = snippet(cx, recv.span, ".."); | ||
let err_snippet = snippet(cx, arg.span, ".."); | ||
span_lint_and_sugg( | ||
cx, | ||
OPTION_MAP_OR_ERR_OK, | ||
expr.span, | ||
msg, | ||
"try using `ok_or` instead", | ||
format!("{self_snippet}.ok_or({err_snippet})"), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#![warn(clippy::option_map_or_err_ok)] | ||
|
||
fn main() { | ||
let x = Some("a"); | ||
let _ = x.ok_or("a"); | ||
//~^ ERROR: called `map_or(Err(_), Ok)` on an `Option` value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#![warn(clippy::option_map_or_err_ok)] | ||
|
||
fn main() { | ||
let x = Some("a"); | ||
let _ = x.map_or(Err("a"), Ok); | ||
//~^ ERROR: called `map_or(Err(_), Ok)` on an `Option` value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error: called `map_or(Err(_), Ok)` on an `Option` value | ||
--> $DIR/option_map_or_err_ok.rs:5:13 | ||
| | ||
LL | let _ = x.map_or(Err("a"), Ok); | ||
| ^^^^^^^^^^^^^^^^^^^^^^ help: try using `ok_or` instead: `x.ok_or("a")` | ||
| | ||
= note: `-D clippy::option-map-or-err-ok` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::option_map_or_err_ok)]` | ||
|
||
error: aborting due to previous error | ||
|