2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::source::snippet_with_applicability;
|
|
|
|
use clippy_utils::ty::{is_copy, is_type_diagnostic_item};
|
2022-01-27 14:12:45 +00:00
|
|
|
use clippy_utils::{is_trait_method, meets_msrv, msrvs, peel_blocks};
|
2018-10-02 13:13:43 +00:00
|
|
|
use if_chain::if_chain;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_errors::Applicability;
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir as hir;
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-04-25 20:49:06 +00:00
|
|
|
use rustc_middle::mir::Mutability;
|
2020-03-30 09:02:14 +00:00
|
|
|
use rustc_middle::ty;
|
2020-11-23 12:51:04 +00:00
|
|
|
use rustc_middle::ty::adjustment::Adjust;
|
2022-01-27 14:12:45 +00:00
|
|
|
use rustc_semver::RustcVersion;
|
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2020-05-08 11:57:01 +00:00
|
|
|
use rustc_span::symbol::Ident;
|
2020-11-05 13:29:48 +00:00
|
|
|
use rustc_span::{sym, Span};
|
2018-10-02 13:13:43 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for usage of `map(|x| x.clone())` or
|
2020-11-23 12:51:04 +00:00
|
|
|
/// dereferencing closures for `Copy` types, on `Iterator` or `Option`,
|
|
|
|
/// and suggests `cloned()` or `copied()` instead
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Readability, this can be written more concisely
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```rust
|
|
|
|
/// let x = vec![42, 43];
|
|
|
|
/// let y = x.iter();
|
|
|
|
/// let z = y.map(|i| *i);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// The correct use would be:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// let x = vec![42, 43];
|
|
|
|
/// let y = x.iter();
|
|
|
|
/// let z = y.cloned();
|
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2018-10-02 13:13:43 +00:00
|
|
|
pub MAP_CLONE,
|
|
|
|
style,
|
|
|
|
"using `iterator.map(|x| x.clone())`, or dereferencing closures for `Copy` types"
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:12:45 +00:00
|
|
|
pub struct MapClone {
|
|
|
|
msrv: Option<RustcVersion>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(MapClone => [MAP_CLONE]);
|
|
|
|
|
|
|
|
impl MapClone {
|
|
|
|
pub fn new(msrv: Option<RustcVersion>) -> Self {
|
|
|
|
Self { msrv }
|
|
|
|
}
|
|
|
|
}
|
2018-10-02 13:13:43 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for MapClone {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, e: &hir::Expr<'_>) {
|
2019-08-19 16:30:32 +00:00
|
|
|
if e.span.from_expansion() {
|
2018-10-02 13:13:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if_chain! {
|
2021-12-01 17:17:50 +00:00
|
|
|
if let hir::ExprKind::MethodCall(method, args, _) = e.kind;
|
2018-10-02 13:13:43 +00:00
|
|
|
if args.len() == 2;
|
2021-01-15 09:56:44 +00:00
|
|
|
if method.ident.name == sym::map;
|
2020-07-17 08:47:04 +00:00
|
|
|
let ty = cx.typeck_results().expr_ty(&args[0]);
|
2021-10-02 23:51:01 +00:00
|
|
|
if is_type_diagnostic_item(cx, ty, sym::Option) || is_trait_method(cx, e, sym::Iterator);
|
2022-06-11 19:25:25 +00:00
|
|
|
if let hir::ExprKind::Closure { body, .. } = args[1].kind;
|
2018-10-02 13:13:43 +00:00
|
|
|
then {
|
2022-06-11 19:25:25 +00:00
|
|
|
let closure_body = cx.tcx.hir().body(body);
|
2021-12-17 12:40:22 +00:00
|
|
|
let closure_expr = peel_blocks(&closure_body.value);
|
2019-09-27 15:16:06 +00:00
|
|
|
match closure_body.params[0].pat.kind {
|
2021-04-08 15:50:13 +00:00
|
|
|
hir::PatKind::Ref(inner, hir::Mutability::Not) => if let hir::PatKind::Binding(
|
2019-02-03 07:12:07 +00:00
|
|
|
hir::BindingAnnotation::Unannotated, .., name, None
|
2019-09-27 15:16:06 +00:00
|
|
|
) = inner.kind {
|
2019-01-15 06:09:47 +00:00
|
|
|
if ident_eq(name, closure_expr) {
|
2022-01-27 14:12:45 +00:00
|
|
|
self.lint_explicit_closure(cx, e.span, args[0].span, true);
|
2019-01-15 06:09:47 +00:00
|
|
|
}
|
2018-10-02 13:13:43 +00:00
|
|
|
},
|
2019-02-03 07:12:07 +00:00
|
|
|
hir::PatKind::Binding(hir::BindingAnnotation::Unannotated, .., name, None) => {
|
2019-09-27 15:16:06 +00:00
|
|
|
match closure_expr.kind {
|
2021-04-08 15:50:13 +00:00
|
|
|
hir::ExprKind::Unary(hir::UnOp::Deref, inner) => {
|
2019-12-22 09:26:51 +00:00
|
|
|
if ident_eq(name, inner) {
|
2020-08-03 22:18:29 +00:00
|
|
|
if let ty::Ref(.., Mutability::Not) = cx.typeck_results().expr_ty(inner).kind() {
|
2022-01-27 14:12:45 +00:00
|
|
|
self.lint_explicit_closure(cx, e.span, args[0].span, true);
|
2019-12-22 09:26:51 +00:00
|
|
|
}
|
2018-11-27 20:14:15 +00:00
|
|
|
}
|
|
|
|
},
|
2021-12-01 17:17:50 +00:00
|
|
|
hir::ExprKind::MethodCall(method, [obj], _) => if_chain! {
|
2020-11-23 12:51:04 +00:00
|
|
|
if ident_eq(name, obj) && method.ident.name == sym::clone;
|
2021-03-12 14:30:50 +00:00
|
|
|
if let Some(fn_id) = cx.typeck_results().type_dependent_def_id(closure_expr.hir_id);
|
|
|
|
if let Some(trait_id) = cx.tcx.trait_of_item(fn_id);
|
|
|
|
if cx.tcx.lang_items().clone_trait().map_or(false, |id| id == trait_id);
|
2020-11-23 12:51:04 +00:00
|
|
|
// no autoderefs
|
|
|
|
if !cx.typeck_results().expr_adjustments(obj).iter()
|
|
|
|
.any(|a| matches!(a.kind, Adjust::Deref(Some(..))));
|
|
|
|
then {
|
|
|
|
let obj_ty = cx.typeck_results().expr_ty(obj);
|
|
|
|
if let ty::Ref(_, ty, mutability) = obj_ty.kind() {
|
|
|
|
if matches!(mutability, Mutability::Not) {
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 03:13:38 +00:00
|
|
|
let copy = is_copy(cx, *ty);
|
2022-01-27 14:12:45 +00:00
|
|
|
self.lint_explicit_closure(cx, e.span, args[0].span, copy);
|
2020-11-23 12:51:04 +00:00
|
|
|
}
|
2019-01-15 06:09:47 +00:00
|
|
|
} else {
|
|
|
|
lint_needless_cloning(cx, e.span, args[0].span);
|
|
|
|
}
|
2018-11-27 20:14:15 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {},
|
2018-10-02 13:13:43 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-27 14:12:45 +00:00
|
|
|
|
|
|
|
extract_msrv_attr!(LateContext);
|
2018-10-02 13:13:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 07:12:26 +00:00
|
|
|
fn ident_eq(name: Ident, path: &hir::Expr<'_>) -> bool {
|
2021-04-08 15:50:13 +00:00
|
|
|
if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = path.kind {
|
2019-01-15 06:09:47 +00:00
|
|
|
path.segments.len() == 1 && path.segments[0].ident == name
|
|
|
|
} else {
|
|
|
|
false
|
2018-10-02 13:13:43 +00:00
|
|
|
}
|
2018-10-02 13:18:56 +00:00
|
|
|
}
|
2019-01-15 06:09:47 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn lint_needless_cloning(cx: &LateContext<'_>, root: Span, receiver: Span) {
|
2019-01-15 06:09:47 +00:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
MAP_CLONE,
|
|
|
|
root.trim_start(receiver).unwrap(),
|
2020-08-28 14:10:16 +00:00
|
|
|
"you are needlessly cloning iterator elements",
|
|
|
|
"remove the `map` call",
|
2019-01-15 06:09:47 +00:00
|
|
|
String::new(),
|
|
|
|
Applicability::MachineApplicable,
|
2021-06-03 06:41:37 +00:00
|
|
|
);
|
2019-01-15 06:09:47 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 14:12:45 +00:00
|
|
|
impl MapClone {
|
|
|
|
fn lint_explicit_closure(&self, cx: &LateContext<'_>, replace: Span, root: Span, is_copy: bool) {
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
2022-05-05 14:12:52 +00:00
|
|
|
|
2022-05-21 11:24:00 +00:00
|
|
|
let (message, sugg_method) = if is_copy && meets_msrv(self.msrv, msrvs::ITERATOR_COPIED) {
|
2022-05-05 14:12:52 +00:00
|
|
|
("you are using an explicit closure for copying elements", "copied")
|
2022-01-27 14:12:45 +00:00
|
|
|
} else {
|
2022-05-05 14:12:52 +00:00
|
|
|
("you are using an explicit closure for cloning elements", "cloned")
|
2022-01-27 14:12:45 +00:00
|
|
|
};
|
|
|
|
|
2019-04-15 21:32:39 +00:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
MAP_CLONE,
|
|
|
|
replace,
|
2022-01-27 14:12:45 +00:00
|
|
|
message,
|
|
|
|
&format!("consider calling the dedicated `{}` method", sugg_method),
|
2019-04-15 21:32:39 +00:00
|
|
|
format!(
|
2022-01-27 14:12:45 +00:00
|
|
|
"{}.{}()",
|
|
|
|
snippet_with_applicability(cx, root, "..", &mut applicability),
|
|
|
|
sugg_method,
|
2019-04-15 21:32:39 +00:00
|
|
|
),
|
|
|
|
applicability,
|
2021-06-03 06:41:37 +00:00
|
|
|
);
|
2019-04-15 21:32:39 +00:00
|
|
|
}
|
2019-01-15 06:09:47 +00:00
|
|
|
}
|