forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
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 rust-lang#5694 - wangtheo:issue-5626, r=matthiaskrgr
rust-lang#5626: lint iterator.map(|x| x) changelog: adds a new lint for iterator.map(|x| x) (see rust-lang/rust-clippy#5626) The code also lints for result.map(|x| x) and option.map(|x| x). Also, I'm not sure if I'm checking for type adjustments correctly and I can't think of an example where .map(|x| x) would apply type adjustments.
- Loading branch information
Showing
10 changed files
with
228 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
use crate::utils::{ | ||
is_adjusted, is_type_diagnostic_item, match_path, match_trait_method, match_var, paths, remove_blocks, | ||
span_lint_and_sugg, | ||
}; | ||
use if_chain::if_chain; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Body, Expr, ExprKind, Pat, PatKind, QPath, StmtKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Checks for instances of `map(f)` where `f` is the identity function. | ||
/// | ||
/// **Why is this bad?** It can be written more concisely without the call to `map`. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust | ||
/// let x = [1, 2, 3]; | ||
/// let y: Vec<_> = x.iter().map(|x| x).map(|x| 2*x).collect(); | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// let x = [1, 2, 3]; | ||
/// let y: Vec<_> = x.iter().map(|x| 2*x).collect(); | ||
/// ``` | ||
pub MAP_IDENTITY, | ||
complexity, | ||
"using iterator.map(|x| x)" | ||
} | ||
|
||
declare_lint_pass!(MapIdentity => [MAP_IDENTITY]); | ||
|
||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapIdentity { | ||
fn check_expr(&mut self, cx: &LateContext<'_, '_>, expr: &Expr<'_>) { | ||
if expr.span.from_expansion() { | ||
return; | ||
} | ||
|
||
if_chain! { | ||
if let Some([caller, func]) = get_map_argument(cx, expr); | ||
if is_expr_identity_function(cx, func); | ||
then { | ||
span_lint_and_sugg( | ||
cx, | ||
MAP_IDENTITY, | ||
expr.span.trim_start(caller.span).unwrap(), | ||
"unnecessary map of the identity function", | ||
"remove the call to `map`", | ||
String::new(), | ||
Applicability::MachineApplicable | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Returns the arguments passed into map() if the expression is a method call to | ||
/// map(). Otherwise, returns None. | ||
fn get_map_argument<'a>(cx: &LateContext<'_, '_>, expr: &'a Expr<'a>) -> Option<&'a [Expr<'a>]> { | ||
if_chain! { | ||
if let ExprKind::MethodCall(ref method, _, ref args, _) = expr.kind; | ||
if args.len() == 2 && method.ident.as_str() == "map"; | ||
let caller_ty = cx.tables.expr_ty(&args[0]); | ||
if match_trait_method(cx, expr, &paths::ITERATOR) | ||
|| is_type_diagnostic_item(cx, caller_ty, sym!(result_type)) | ||
|| is_type_diagnostic_item(cx, caller_ty, sym!(option_type)); | ||
then { | ||
Some(args) | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
|
||
/// Checks if an expression represents the identity function | ||
/// Only examines closures and `std::convert::identity` | ||
fn is_expr_identity_function(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool { | ||
match expr.kind { | ||
ExprKind::Closure(_, _, body_id, _, _) => is_body_identity_function(cx, cx.tcx.hir().body(body_id)), | ||
ExprKind::Path(QPath::Resolved(_, ref path)) => match_path(path, &paths::STD_CONVERT_IDENTITY), | ||
_ => false, | ||
} | ||
} | ||
|
||
/// Checks if a function's body represents the identity function | ||
/// Looks for bodies of the form `|x| x`, `|x| return x`, `|x| { return x }` or `|x| { | ||
/// return x; }` | ||
fn is_body_identity_function(cx: &LateContext<'_, '_>, func: &Body<'_>) -> bool { | ||
let params = func.params; | ||
let body = remove_blocks(&func.value); | ||
|
||
// if there's less/more than one parameter, then it is not the identity function | ||
if params.len() != 1 { | ||
return false; | ||
} | ||
|
||
match body.kind { | ||
ExprKind::Path(QPath::Resolved(None, _)) => match_expr_param(cx, body, params[0].pat), | ||
ExprKind::Ret(Some(ref ret_val)) => match_expr_param(cx, ret_val, params[0].pat), | ||
ExprKind::Block(ref block, _) => { | ||
if_chain! { | ||
if block.stmts.len() == 1; | ||
if let StmtKind::Semi(ref expr) | StmtKind::Expr(ref expr) = block.stmts[0].kind; | ||
if let ExprKind::Ret(Some(ref ret_val)) = expr.kind; | ||
then { | ||
match_expr_param(cx, ret_val, params[0].pat) | ||
} else { | ||
false | ||
} | ||
} | ||
}, | ||
_ => false, | ||
} | ||
} | ||
|
||
/// Returns true iff an expression returns the same thing as a parameter's pattern | ||
fn match_expr_param(cx: &LateContext<'_, '_>, expr: &Expr<'_>, pat: &Pat<'_>) -> bool { | ||
if let PatKind::Binding(_, _, ident, _) = pat.kind { | ||
match_var(expr, ident.name) && !(cx.tables.hir_owner == Some(expr.hir_id.owner) && is_adjusted(cx, expr)) | ||
} else { | ||
false | ||
} | ||
} |
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
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,23 @@ | ||
// run-rustfix | ||
#![warn(clippy::map_identity)] | ||
#![allow(clippy::needless_return)] | ||
|
||
fn main() { | ||
let x: [u16; 3] = [1, 2, 3]; | ||
// should lint | ||
let _: Vec<_> = x.iter().map(not_identity).collect(); | ||
let _: Vec<_> = x.iter().collect(); | ||
let _: Option<u8> = Some(3); | ||
let _: Result<i8, f32> = Ok(-3); | ||
// should not lint | ||
let _: Vec<_> = x.iter().map(|x| 2 * x).collect(); | ||
let _: Vec<_> = x.iter().map(not_identity).map(|x| return x - 4).collect(); | ||
let _: Option<u8> = None.map(|x: u8| x - 1); | ||
let _: Result<i8, f32> = Err(2.3).map(|x: i8| { | ||
return x + 3; | ||
}); | ||
} | ||
|
||
fn not_identity(x: &u16) -> u16 { | ||
*x | ||
} |
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,25 @@ | ||
// run-rustfix | ||
#![warn(clippy::map_identity)] | ||
#![allow(clippy::needless_return)] | ||
|
||
fn main() { | ||
let x: [u16; 3] = [1, 2, 3]; | ||
// should lint | ||
let _: Vec<_> = x.iter().map(not_identity).map(|x| return x).collect(); | ||
let _: Vec<_> = x.iter().map(std::convert::identity).map(|y| y).collect(); | ||
let _: Option<u8> = Some(3).map(|x| x); | ||
let _: Result<i8, f32> = Ok(-3).map(|x| { | ||
return x; | ||
}); | ||
// should not lint | ||
let _: Vec<_> = x.iter().map(|x| 2 * x).collect(); | ||
let _: Vec<_> = x.iter().map(not_identity).map(|x| return x - 4).collect(); | ||
let _: Option<u8> = None.map(|x: u8| x - 1); | ||
let _: Result<i8, f32> = Err(2.3).map(|x: i8| { | ||
return x + 3; | ||
}); | ||
} | ||
|
||
fn not_identity(x: &u16) -> u16 { | ||
*x | ||
} |
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,37 @@ | ||
error: unnecessary map of the identity function | ||
--> $DIR/map_identity.rs:8:47 | ||
| | ||
LL | let _: Vec<_> = x.iter().map(not_identity).map(|x| return x).collect(); | ||
| ^^^^^^^^^^^^^^^^^^ help: remove the call to `map` | ||
| | ||
= note: `-D clippy::map-identity` implied by `-D warnings` | ||
|
||
error: unnecessary map of the identity function | ||
--> $DIR/map_identity.rs:9:57 | ||
| | ||
LL | let _: Vec<_> = x.iter().map(std::convert::identity).map(|y| y).collect(); | ||
| ^^^^^^^^^^^ help: remove the call to `map` | ||
|
||
error: unnecessary map of the identity function | ||
--> $DIR/map_identity.rs:9:29 | ||
| | ||
LL | let _: Vec<_> = x.iter().map(std::convert::identity).map(|y| y).collect(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove the call to `map` | ||
|
||
error: unnecessary map of the identity function | ||
--> $DIR/map_identity.rs:10:32 | ||
| | ||
LL | let _: Option<u8> = Some(3).map(|x| x); | ||
| ^^^^^^^^^^^ help: remove the call to `map` | ||
|
||
error: unnecessary map of the identity function | ||
--> $DIR/map_identity.rs:11:36 | ||
| | ||
LL | let _: Result<i8, f32> = Ok(-3).map(|x| { | ||
| ____________________________________^ | ||
LL | | return x; | ||
LL | | }); | ||
| |______^ help: remove the call to `map` | ||
|
||
error: aborting due to 5 previous errors | ||
|