2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::source::snippet_with_applicability;
|
|
|
|
use clippy_utils::ty::is_type_diagnostic_item;
|
2022-03-14 11:02:53 +00:00
|
|
|
use clippy_utils::{get_parent_expr, path_to_local_id, usage};
|
2021-03-25 18:29:11 +00:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_ast::ast;
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir as hir;
|
2022-03-14 11:02:53 +00:00
|
|
|
use rustc_hir::intravisit::{walk_expr, Visitor};
|
|
|
|
use rustc_hir::{BorrowKind, Expr, ExprKind, HirId, Mutability, Pat};
|
2021-03-25 18:29:11 +00:00
|
|
|
use rustc_lint::LateContext;
|
2022-03-14 11:02:53 +00:00
|
|
|
use rustc_middle::hir::nested_filter;
|
2021-03-25 18:29:11 +00:00
|
|
|
use rustc_middle::ty::{self, Ty};
|
|
|
|
use rustc_span::symbol::sym;
|
|
|
|
|
|
|
|
pub(super) fn derefs_to_slice<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
expr: &'tcx hir::Expr<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) -> Option<&'tcx hir::Expr<'tcx>> {
|
|
|
|
fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool {
|
|
|
|
match ty.kind() {
|
|
|
|
ty::Slice(_) => true,
|
|
|
|
ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()),
|
2021-10-02 23:51:01 +00:00
|
|
|
ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::Vec),
|
2021-04-27 14:55:11 +00:00
|
|
|
ty::Array(_, size) => size.try_eval_usize(cx.tcx, cx.param_env).is_some(),
|
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
|
|
|
ty::Ref(_, inner, _) => may_slice(cx, *inner),
|
2021-03-25 18:29:11 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 09:43:35 +00:00
|
|
|
if let hir::ExprKind::MethodCall(path, self_arg, ..) = &expr.kind {
|
2021-09-08 14:31:47 +00:00
|
|
|
if path.ident.name == sym::iter && may_slice(cx, cx.typeck_results().expr_ty(self_arg)) {
|
|
|
|
Some(self_arg)
|
2021-03-25 18:29:11 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match ty.kind() {
|
|
|
|
ty::Slice(_) => Some(expr),
|
|
|
|
ty::Adt(def, _) if def.is_box() && may_slice(cx, ty.boxed_ty()) => Some(expr),
|
|
|
|
ty::Ref(_, inner, _) => {
|
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
|
|
|
if may_slice(cx, *inner) {
|
2021-03-25 18:29:11 +00:00
|
|
|
Some(expr)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn get_hint_if_single_char_arg(
|
|
|
|
cx: &LateContext<'_>,
|
|
|
|
arg: &hir::Expr<'_>,
|
|
|
|
applicability: &mut Applicability,
|
|
|
|
) -> Option<String> {
|
|
|
|
if_chain! {
|
|
|
|
if let hir::ExprKind::Lit(lit) = &arg.kind;
|
|
|
|
if let ast::LitKind::Str(r, style) = lit.node;
|
|
|
|
let string = r.as_str();
|
|
|
|
if string.chars().count() == 1;
|
|
|
|
then {
|
2021-12-30 14:10:43 +00:00
|
|
|
let snip = snippet_with_applicability(cx, arg.span, string, applicability);
|
2021-03-25 18:29:11 +00:00
|
|
|
let ch = if let ast::StrStyle::Raw(nhash) = style {
|
|
|
|
let nhash = nhash as usize;
|
|
|
|
// for raw string: r##"a"##
|
|
|
|
&snip[(nhash + 2)..(snip.len() - 1 - nhash)]
|
|
|
|
} else {
|
|
|
|
// for regular string: "a"
|
|
|
|
&snip[1..(snip.len() - 1)]
|
|
|
|
};
|
2021-12-06 11:33:31 +00:00
|
|
|
|
|
|
|
let hint = format!("'{}'", match ch {
|
|
|
|
"'" => "\\'" ,
|
|
|
|
r"\" => "\\\\",
|
|
|
|
_ => ch,
|
|
|
|
});
|
|
|
|
|
2021-03-25 18:29:11 +00:00
|
|
|
Some(hint)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-14 11:02:53 +00:00
|
|
|
|
|
|
|
/// The core logic of `check_for_loop_iter` in `unnecessary_iter_cloned.rs`, this function wraps a
|
|
|
|
/// use of `CloneOrCopyVisitor`.
|
|
|
|
pub(super) fn clone_or_copy_needed<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
pat: &Pat<'tcx>,
|
|
|
|
body: &'tcx Expr<'tcx>,
|
|
|
|
) -> (bool, Vec<&'tcx Expr<'tcx>>) {
|
|
|
|
let mut visitor = CloneOrCopyVisitor {
|
|
|
|
cx,
|
|
|
|
binding_hir_ids: pat_bindings(pat),
|
|
|
|
clone_or_copy_needed: false,
|
|
|
|
addr_of_exprs: Vec::new(),
|
|
|
|
};
|
|
|
|
visitor.visit_expr(body);
|
|
|
|
(visitor.clone_or_copy_needed, visitor.addr_of_exprs)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a vector of all `HirId`s bound by the pattern.
|
|
|
|
fn pat_bindings(pat: &Pat<'_>) -> Vec<HirId> {
|
|
|
|
let mut collector = usage::ParamBindingIdCollector {
|
|
|
|
binding_hir_ids: Vec::new(),
|
|
|
|
};
|
|
|
|
collector.visit_pat(pat);
|
|
|
|
collector.binding_hir_ids
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `clone_or_copy_needed` will be false when `CloneOrCopyVisitor` is done visiting if the only
|
|
|
|
/// operations performed on `binding_hir_ids` are:
|
|
|
|
/// * to take non-mutable references to them
|
|
|
|
/// * to use them as non-mutable `&self` in method calls
|
|
|
|
/// If any of `binding_hir_ids` is used in any other way, then `clone_or_copy_needed` will be true
|
|
|
|
/// when `CloneOrCopyVisitor` is done visiting.
|
|
|
|
struct CloneOrCopyVisitor<'cx, 'tcx> {
|
|
|
|
cx: &'cx LateContext<'tcx>,
|
|
|
|
binding_hir_ids: Vec<HirId>,
|
|
|
|
clone_or_copy_needed: bool,
|
|
|
|
addr_of_exprs: Vec<&'tcx Expr<'tcx>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'cx, 'tcx> Visitor<'tcx> for CloneOrCopyVisitor<'cx, 'tcx> {
|
|
|
|
type NestedFilter = nested_filter::OnlyBodies;
|
|
|
|
|
|
|
|
fn nested_visit_map(&mut self) -> Self::Map {
|
|
|
|
self.cx.tcx.hir()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
|
|
|
|
walk_expr(self, expr);
|
|
|
|
if self.is_binding(expr) {
|
|
|
|
if let Some(parent) = get_parent_expr(self.cx, expr) {
|
|
|
|
match parent.kind {
|
|
|
|
ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, _) => {
|
|
|
|
self.addr_of_exprs.push(parent);
|
|
|
|
return;
|
|
|
|
},
|
2022-09-01 09:43:35 +00:00
|
|
|
ExprKind::MethodCall(.., args, _) => {
|
2022-03-14 11:02:53 +00:00
|
|
|
if_chain! {
|
2022-09-01 09:43:35 +00:00
|
|
|
if args.iter().all(|arg| !self.is_binding(arg));
|
2022-03-14 11:02:53 +00:00
|
|
|
if let Some(method_def_id) = self.cx.typeck_results().type_dependent_def_id(parent.hir_id);
|
|
|
|
let method_ty = self.cx.tcx.type_of(method_def_id);
|
|
|
|
let self_ty = method_ty.fn_sig(self.cx.tcx).input(0).skip_binder();
|
|
|
|
if matches!(self_ty.kind(), ty::Ref(_, _, Mutability::Not));
|
|
|
|
then {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.clone_or_copy_needed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'cx, 'tcx> CloneOrCopyVisitor<'cx, 'tcx> {
|
|
|
|
fn is_binding(&self, expr: &Expr<'tcx>) -> bool {
|
|
|
|
self.binding_hir_ids
|
|
|
|
.iter()
|
|
|
|
.any(|hir_id| path_to_local_id(expr, *hir_id))
|
|
|
|
}
|
|
|
|
}
|