-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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 #12169 - GuillaumeGomez:unnecessary_result_map_or_else,…
… r=llogiq Add new `unnecessary_result_map_or_else` lint Fixes rust-lang/rust-clippy#7328. r? `@llogiq` changelog: Add new `unnecessary_result_map_or_else` lint
- Loading branch information
Showing
8 changed files
with
291 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
95 changes: 95 additions & 0 deletions
95
clippy_lints/src/methods/unnecessary_result_map_or_else.rs
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,95 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::peel_blocks; | ||
use clippy_utils::source::snippet; | ||
use clippy_utils::ty::is_type_diagnostic_item; | ||
use rustc_errors::Applicability; | ||
use rustc_hir as hir; | ||
use rustc_hir::{Closure, Expr, ExprKind, HirId, QPath, Stmt, StmtKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::symbol::sym; | ||
|
||
use super::UNNECESSARY_RESULT_MAP_OR_ELSE; | ||
|
||
fn emit_lint(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, def_arg: &Expr<'_>) { | ||
let msg = "unused \"map closure\" when calling `Result::map_or_else` value"; | ||
let self_snippet = snippet(cx, recv.span, ".."); | ||
let err_snippet = snippet(cx, def_arg.span, ".."); | ||
span_lint_and_sugg( | ||
cx, | ||
UNNECESSARY_RESULT_MAP_OR_ELSE, | ||
expr.span, | ||
msg, | ||
"consider using `unwrap_or_else`", | ||
format!("{self_snippet}.unwrap_or_else({err_snippet})"), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
|
||
fn get_last_chain_binding_hir_id(mut hir_id: HirId, statements: &[Stmt<'_>]) -> Option<HirId> { | ||
for stmt in statements { | ||
if let StmtKind::Local(local) = stmt.kind | ||
&& let Some(init) = local.init | ||
&& let ExprKind::Path(QPath::Resolved(_, path)) = init.kind | ||
&& let hir::def::Res::Local(local_hir_id) = path.res | ||
&& local_hir_id == hir_id | ||
{ | ||
hir_id = local.pat.hir_id; | ||
} else { | ||
return None; | ||
} | ||
} | ||
Some(hir_id) | ||
} | ||
|
||
fn handle_qpath( | ||
cx: &LateContext<'_>, | ||
expr: &Expr<'_>, | ||
recv: &Expr<'_>, | ||
def_arg: &Expr<'_>, | ||
expected_hir_id: HirId, | ||
qpath: QPath<'_>, | ||
) { | ||
if let QPath::Resolved(_, path) = qpath | ||
&& let hir::def::Res::Local(hir_id) = path.res | ||
&& expected_hir_id == hir_id | ||
{ | ||
emit_lint(cx, expr, recv, def_arg); | ||
} | ||
} | ||
|
||
/// lint use of `_.map_or_else(|err| err, |n| n)` for `Result`s. | ||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx Expr<'_>, | ||
recv: &'tcx Expr<'_>, | ||
def_arg: &'tcx Expr<'_>, | ||
map_arg: &'tcx Expr<'_>, | ||
) { | ||
// lint if the caller of `map_or_else()` is a `Result` | ||
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Result) | ||
&& let ExprKind::Closure(&Closure { body, .. }) = map_arg.kind | ||
&& let body = cx.tcx.hir().body(body) | ||
&& let Some(first_param) = body.params.first() | ||
{ | ||
let body_expr = peel_blocks(body.value); | ||
|
||
match body_expr.kind { | ||
ExprKind::Path(qpath) => { | ||
handle_qpath(cx, expr, recv, def_arg, first_param.pat.hir_id, qpath); | ||
}, | ||
// If this is a block (that wasn't peeled off), then it means there are statements. | ||
ExprKind::Block(block, _) => { | ||
if let Some(block_expr) = block.expr | ||
// First we ensure that this is a "binding chain" (each statement is a binding | ||
// of the previous one) and that it is a binding of the closure argument. | ||
&& let Some(last_chain_binding_id) = | ||
get_last_chain_binding_hir_id(first_param.pat.hir_id, block.stmts) | ||
&& let ExprKind::Path(qpath) = block_expr.kind | ||
{ | ||
handle_qpath(cx, expr, recv, def_arg, last_chain_binding_id, qpath); | ||
} | ||
}, | ||
_ => {}, | ||
} | ||
} | ||
} |
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,61 @@ | ||
#![warn(clippy::unnecessary_result_map_or_else)] | ||
#![allow(clippy::unnecessary_literal_unwrap, clippy::let_and_return, clippy::let_unit_value)] | ||
|
||
fn main() { | ||
let x: Result<(), ()> = Ok(()); | ||
x.unwrap_or_else(|err| err); //~ ERROR: unused "map closure" when calling | ||
|
||
// Type ascribtion. | ||
let x: Result<(), ()> = Ok(()); | ||
x.unwrap_or_else(|err: ()| err); //~ ERROR: unused "map closure" when calling | ||
|
||
// Auto-deref. | ||
let y = String::new(); | ||
let x: Result<&String, &String> = Ok(&y); | ||
let y: &str = x.unwrap_or_else(|err| err); //~ ERROR: unused "map closure" when calling | ||
|
||
// Temporary variable. | ||
let x: Result<(), ()> = Ok(()); | ||
x.unwrap_or_else(|err| err); | ||
|
||
// Should not warn. | ||
let x: Result<usize, usize> = Ok(0); | ||
x.map_or_else(|err| err, |n| n + 1); | ||
|
||
// Should not warn. | ||
let y = (); | ||
let x: Result<(), ()> = Ok(()); | ||
x.map_or_else(|err| err, |_| y); | ||
|
||
// Should not warn. | ||
let y = (); | ||
let x: Result<(), ()> = Ok(()); | ||
x.map_or_else( | ||
|err| err, | ||
|_| { | ||
let tmp = y; | ||
tmp | ||
}, | ||
); | ||
|
||
// Should not warn. | ||
let x: Result<usize, usize> = Ok(1); | ||
x.map_or_else( | ||
|err| err, | ||
|n| { | ||
let tmp = n + 1; | ||
tmp | ||
}, | ||
); | ||
|
||
// Should not warn. | ||
let y = 0; | ||
let x: Result<usize, usize> = Ok(1); | ||
x.map_or_else( | ||
|err| err, | ||
|n| { | ||
let tmp = n; | ||
y | ||
}, | ||
); | ||
} |
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,69 @@ | ||
#![warn(clippy::unnecessary_result_map_or_else)] | ||
#![allow(clippy::unnecessary_literal_unwrap, clippy::let_and_return, clippy::let_unit_value)] | ||
|
||
fn main() { | ||
let x: Result<(), ()> = Ok(()); | ||
x.map_or_else(|err| err, |n| n); //~ ERROR: unused "map closure" when calling | ||
|
||
// Type ascribtion. | ||
let x: Result<(), ()> = Ok(()); | ||
x.map_or_else(|err: ()| err, |n: ()| n); //~ ERROR: unused "map closure" when calling | ||
|
||
// Auto-deref. | ||
let y = String::new(); | ||
let x: Result<&String, &String> = Ok(&y); | ||
let y: &str = x.map_or_else(|err| err, |n| n); //~ ERROR: unused "map closure" when calling | ||
|
||
// Temporary variable. | ||
let x: Result<(), ()> = Ok(()); | ||
x.map_or_else( | ||
//~^ ERROR: unused "map closure" when calling | ||
|err| err, | ||
|n| { | ||
let tmp = n; | ||
let tmp2 = tmp; | ||
tmp2 | ||
}, | ||
); | ||
|
||
// Should not warn. | ||
let x: Result<usize, usize> = Ok(0); | ||
x.map_or_else(|err| err, |n| n + 1); | ||
|
||
// Should not warn. | ||
let y = (); | ||
let x: Result<(), ()> = Ok(()); | ||
x.map_or_else(|err| err, |_| y); | ||
|
||
// Should not warn. | ||
let y = (); | ||
let x: Result<(), ()> = Ok(()); | ||
x.map_or_else( | ||
|err| err, | ||
|_| { | ||
let tmp = y; | ||
tmp | ||
}, | ||
); | ||
|
||
// Should not warn. | ||
let x: Result<usize, usize> = Ok(1); | ||
x.map_or_else( | ||
|err| err, | ||
|n| { | ||
let tmp = n + 1; | ||
tmp | ||
}, | ||
); | ||
|
||
// Should not warn. | ||
let y = 0; | ||
let x: Result<usize, usize> = Ok(1); | ||
x.map_or_else( | ||
|err| err, | ||
|n| { | ||
let tmp = n; | ||
y | ||
}, | ||
); | ||
} |
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,35 @@ | ||
error: unused "map closure" when calling `Result::map_or_else` value | ||
--> $DIR/unnecessary_result_map_or_else.rs:6:5 | ||
| | ||
LL | x.map_or_else(|err| err, |n| n); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `unwrap_or_else`: `x.unwrap_or_else(|err| err)` | ||
| | ||
= note: `-D clippy::unnecessary-result-map-or-else` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_result_map_or_else)]` | ||
|
||
error: unused "map closure" when calling `Result::map_or_else` value | ||
--> $DIR/unnecessary_result_map_or_else.rs:10:5 | ||
| | ||
LL | x.map_or_else(|err: ()| err, |n: ()| n); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `unwrap_or_else`: `x.unwrap_or_else(|err: ()| err)` | ||
|
||
error: unused "map closure" when calling `Result::map_or_else` value | ||
--> $DIR/unnecessary_result_map_or_else.rs:15:19 | ||
| | ||
LL | let y: &str = x.map_or_else(|err| err, |n| n); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `unwrap_or_else`: `x.unwrap_or_else(|err| err)` | ||
|
||
error: unused "map closure" when calling `Result::map_or_else` value | ||
--> $DIR/unnecessary_result_map_or_else.rs:19:5 | ||
| | ||
LL | / x.map_or_else( | ||
LL | | | ||
LL | | |err| err, | ||
LL | | |n| { | ||
... | | ||
LL | | }, | ||
LL | | ); | ||
| |_____^ help: consider using `unwrap_or_else`: `x.unwrap_or_else(|err| err)` | ||
|
||
error: aborting due to 4 previous errors | ||
|