Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map_clone: avoid suggesting copied() for &mut #5530

Merged
merged 1 commit into from
Apr 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions clippy_lints/src/map_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rustc_ast::ast::Ident;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::mir::Mutability;
use rustc_middle::ty;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::Span;
Expand Down Expand Up @@ -58,7 +59,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapClone {
let closure_expr = remove_blocks(&closure_body.value);
then {
match closure_body.params[0].pat.kind {
hir::PatKind::Ref(ref inner, _) => if let hir::PatKind::Binding(
hir::PatKind::Ref(ref inner, hir::Mutability::Not) => if let hir::PatKind::Binding(
hir::BindingAnnotation::Unannotated, .., name, None
) = inner.kind {
if ident_eq(name, closure_expr) {
Expand All @@ -69,7 +70,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapClone {
match closure_expr.kind {
hir::ExprKind::Unary(hir::UnOp::UnDeref, ref inner) => {
if ident_eq(name, inner) {
if let ty::Ref(..) = cx.tables.expr_ty(inner).kind {
if let ty::Ref(.., Mutability::Not) = cx.tables.expr_ty(inner).kind {
lint(cx, e.span, args[0].span, true);
}
}
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/map_clone.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![allow(clippy::clone_on_copy, clippy::redundant_clone)]
#![allow(clippy::missing_docs_in_private_items)]
#![allow(clippy::redundant_closure_for_method_calls)]
#![allow(clippy::many_single_char_names)]

fn main() {
let _: Vec<i8> = vec![5_i8; 6].iter().copied().collect();
Expand Down Expand Up @@ -33,4 +34,14 @@ fn main() {
let v: Vec<Rc<u32>> = vec![Rc::new(0_u32)];
let _: Vec<u32> = v.into_iter().map(|x| *x).collect();
}

// Issue #5524 mutable references
{
let mut c = 42;
let v = vec![&mut c];
let _: Vec<u32> = v.into_iter().map(|x| *x).collect();
let mut d = 21;
let v = vec![&mut d];
let _: Vec<u32> = v.into_iter().map(|&mut x| x).collect();
}
}
11 changes: 11 additions & 0 deletions tests/ui/map_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![allow(clippy::clone_on_copy, clippy::redundant_clone)]
#![allow(clippy::missing_docs_in_private_items)]
#![allow(clippy::redundant_closure_for_method_calls)]
#![allow(clippy::many_single_char_names)]

fn main() {
let _: Vec<i8> = vec![5_i8; 6].iter().map(|x| *x).collect();
Expand Down Expand Up @@ -33,4 +34,14 @@ fn main() {
let v: Vec<Rc<u32>> = vec![Rc::new(0_u32)];
let _: Vec<u32> = v.into_iter().map(|x| *x).collect();
}

// Issue #5524 mutable references
{
let mut c = 42;
let v = vec![&mut c];
let _: Vec<u32> = v.into_iter().map(|x| *x).collect();
let mut d = 21;
let v = vec![&mut d];
let _: Vec<u32> = v.into_iter().map(|&mut x| x).collect();
}
}
12 changes: 6 additions & 6 deletions tests/ui/map_clone.stderr
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
error: You are using an explicit closure for copying elements
--> $DIR/map_clone.rs:9:22
--> $DIR/map_clone.rs:10:22
|
LL | let _: Vec<i8> = vec![5_i8; 6].iter().map(|x| *x).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `copied` method: `vec![5_i8; 6].iter().copied()`
|
= note: `-D clippy::map-clone` implied by `-D warnings`

error: You are using an explicit closure for cloning elements
--> $DIR/map_clone.rs:10:26
--> $DIR/map_clone.rs:11:26
|
LL | let _: Vec<String> = vec![String::new()].iter().map(|x| x.clone()).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `cloned` method: `vec![String::new()].iter().cloned()`

error: You are using an explicit closure for copying elements
--> $DIR/map_clone.rs:11:23
--> $DIR/map_clone.rs:12:23
|
LL | let _: Vec<u32> = vec![42, 43].iter().map(|&x| x).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `copied` method: `vec![42, 43].iter().copied()`

error: You are using an explicit closure for copying elements
--> $DIR/map_clone.rs:13:26
--> $DIR/map_clone.rs:14:26
|
LL | let _: Option<u64> = Some(&16).map(|b| *b);
| ^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `copied` method: `Some(&16).copied()`

error: You are using an explicit closure for copying elements
--> $DIR/map_clone.rs:14:25
--> $DIR/map_clone.rs:15:25
|
LL | let _: Option<u8> = Some(&1).map(|x| x.clone());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `copied` method: `Some(&1).copied()`

error: You are needlessly cloning iterator elements
--> $DIR/map_clone.rs:25:29
--> $DIR/map_clone.rs:26:29
|
LL | let _ = std::env::args().map(|v| v.clone());
| ^^^^^^^^^^^^^^^^^^^ help: Remove the `map` call
Expand Down