2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-12-06 11:33:31 +00:00
|
|
|
use clippy_utils::get_parent_expr;
|
2021-04-08 15:50:13 +00:00
|
|
|
use clippy_utils::source::snippet_with_context;
|
2022-01-03 20:25:00 +00:00
|
|
|
use clippy_utils::ty::{is_type_lang_item, peel_mid_ty_refs};
|
2021-01-30 17:06:34 +00:00
|
|
|
use if_chain::if_chain;
|
2022-01-04 01:35:32 +00:00
|
|
|
use rustc_ast::util::parser::PREC_PREFIX;
|
2021-01-30 17:06:34 +00:00
|
|
|
use rustc_errors::Applicability;
|
2021-04-08 15:50:13 +00:00
|
|
|
use rustc_hir::{BorrowKind, Expr, ExprKind, LangItem, Mutability};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2022-01-04 01:35:32 +00:00
|
|
|
use rustc_middle::ty::adjustment::{Adjust, AutoBorrow, AutoBorrowMutability};
|
2022-01-03 20:25:00 +00:00
|
|
|
use rustc_middle::ty::subst::GenericArg;
|
2021-01-30 17:06:34 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for redundant slicing expressions which use the full range, and
|
2021-01-30 17:06:34 +00:00
|
|
|
/// do not change the type.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// It unnecessarily adds complexity to the expression.
|
2021-01-30 17:06:34 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Known problems
|
|
|
|
/// If the type being sliced has an implementation of `Index<RangeFull>`
|
2021-01-30 17:06:34 +00:00
|
|
|
/// that actually changes anything then it can't be removed. However, this would be surprising
|
|
|
|
/// to people reading the code and should have a note with it.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2021-01-30 17:06:34 +00:00
|
|
|
/// ```ignore
|
|
|
|
/// fn get_slice(x: &[u32]) -> &[u32] {
|
|
|
|
/// &x[..]
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
|
|
|
/// ```ignore
|
|
|
|
/// fn get_slice(x: &[u32]) -> &[u32] {
|
|
|
|
/// x
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "1.51.0"]
|
2021-01-30 17:06:34 +00:00
|
|
|
pub REDUNDANT_SLICING,
|
|
|
|
complexity,
|
|
|
|
"redundant slicing of the whole range of a type"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(RedundantSlicing => [REDUNDANT_SLICING]);
|
|
|
|
|
2022-01-13 12:18:19 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for RedundantSlicing {
|
2021-01-30 17:06:34 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2021-12-06 11:33:31 +00:00
|
|
|
if expr.span.from_expansion() {
|
2021-01-30 17:06:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-08 15:50:13 +00:00
|
|
|
let ctxt = expr.span.ctxt();
|
2021-01-30 17:06:34 +00:00
|
|
|
if_chain! {
|
2021-04-08 15:50:13 +00:00
|
|
|
if let ExprKind::AddrOf(BorrowKind::Ref, mutability, addressee) = expr.kind;
|
|
|
|
if addressee.span.ctxt() == ctxt;
|
2021-01-30 17:06:34 +00:00
|
|
|
if let ExprKind::Index(indexed, range) = addressee.kind;
|
|
|
|
if is_type_lang_item(cx, cx.typeck_results().expr_ty_adjusted(range), LangItem::RangeFull);
|
|
|
|
then {
|
2022-01-03 20:25:00 +00:00
|
|
|
let (expr_ty, expr_ref_count) = peel_mid_ty_refs(cx.typeck_results().expr_ty(expr));
|
|
|
|
let (indexed_ty, indexed_ref_count) = peel_mid_ty_refs(cx.typeck_results().expr_ty(indexed));
|
2022-01-04 01:35:32 +00:00
|
|
|
let parent_expr = get_parent_expr(cx, expr);
|
|
|
|
let needs_parens_for_prefix = parent_expr.map_or(false, |parent| {
|
|
|
|
parent.precedence().order() > PREC_PREFIX
|
|
|
|
});
|
2021-01-30 17:06:34 +00:00
|
|
|
let mut app = Applicability::MachineApplicable;
|
2021-04-08 15:50:13 +00:00
|
|
|
|
2022-01-03 20:25:00 +00:00
|
|
|
let (help, sugg) = if expr_ty == indexed_ty {
|
|
|
|
if expr_ref_count > indexed_ref_count {
|
2022-01-04 01:35:32 +00:00
|
|
|
// Indexing takes self by reference and can't return a reference to that
|
|
|
|
// reference as it's a local variable. The only way this could happen is if
|
|
|
|
// `self` contains a reference to the `Self` type. If this occurs then the
|
|
|
|
// lint no longer applies as it's essentially a field access, which is not
|
|
|
|
// redundant.
|
2022-01-03 20:25:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-01-04 01:35:32 +00:00
|
|
|
let deref_count = indexed_ref_count - expr_ref_count;
|
2022-01-03 20:25:00 +00:00
|
|
|
|
|
|
|
let (reborrow_str, help_str) = if mutability == Mutability::Mut {
|
|
|
|
// The slice was used to reborrow the mutable reference.
|
|
|
|
("&mut *", "reborrow the original value instead")
|
|
|
|
} else if matches!(
|
2022-01-04 01:35:32 +00:00
|
|
|
parent_expr,
|
2022-01-03 20:25:00 +00:00
|
|
|
Some(Expr {
|
|
|
|
kind: ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, _),
|
|
|
|
..
|
|
|
|
})
|
2022-01-04 01:35:32 +00:00
|
|
|
) || cx.typeck_results().expr_adjustments(expr).first().map_or(false, |a| {
|
|
|
|
matches!(a.kind, Adjust::Borrow(AutoBorrow::Ref(_, AutoBorrowMutability::Mut { .. })))
|
|
|
|
}) {
|
2022-01-03 20:25:00 +00:00
|
|
|
// The slice was used to make a temporary reference.
|
|
|
|
("&*", "reborrow the original value instead")
|
2022-01-04 01:35:32 +00:00
|
|
|
} else if deref_count != 0 {
|
2022-01-03 20:25:00 +00:00
|
|
|
("", "dereference the original value instead")
|
|
|
|
} else {
|
|
|
|
("", "use the original value instead")
|
|
|
|
};
|
|
|
|
|
|
|
|
let snip = snippet_with_context(cx, indexed.span, ctxt, "..", &mut app).0;
|
2022-01-04 01:35:32 +00:00
|
|
|
let sugg = if (deref_count != 0 || !reborrow_str.is_empty()) && needs_parens_for_prefix {
|
|
|
|
format!("({}{}{})", reborrow_str, "*".repeat(deref_count), snip)
|
|
|
|
} else {
|
|
|
|
format!("{}{}{}", reborrow_str, "*".repeat(deref_count), snip)
|
|
|
|
};
|
|
|
|
|
|
|
|
(help_str, sugg)
|
2022-01-03 20:25:00 +00:00
|
|
|
} else if let Some(target_id) = cx.tcx.lang_items().deref_target() {
|
|
|
|
if let Ok(deref_ty) = cx.tcx.try_normalize_erasing_regions(
|
|
|
|
cx.param_env,
|
|
|
|
cx.tcx.mk_projection(target_id, cx.tcx.mk_substs([GenericArg::from(indexed_ty)].into_iter())),
|
|
|
|
) {
|
|
|
|
if deref_ty == expr_ty {
|
|
|
|
let snip = snippet_with_context(cx, indexed.span, ctxt, "..", &mut app).0;
|
2022-01-04 01:35:32 +00:00
|
|
|
let sugg = if needs_parens_for_prefix {
|
|
|
|
format!("(&{}{}*{})", mutability.prefix_str(), "*".repeat(indexed_ref_count), snip)
|
|
|
|
} else {
|
|
|
|
format!("&{}{}*{}", mutability.prefix_str(), "*".repeat(indexed_ref_count), snip)
|
|
|
|
};
|
|
|
|
("dereference the original value instead", sugg)
|
2022-01-03 20:25:00 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2021-04-08 15:50:13 +00:00
|
|
|
} else {
|
2022-01-03 20:25:00 +00:00
|
|
|
return;
|
2021-04-08 15:50:13 +00:00
|
|
|
};
|
2021-01-30 17:06:34 +00:00
|
|
|
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
REDUNDANT_SLICING,
|
|
|
|
expr.span,
|
|
|
|
"redundant slicing of the whole range",
|
2022-01-03 20:25:00 +00:00
|
|
|
help,
|
|
|
|
sugg,
|
2021-01-30 17:06:34 +00:00
|
|
|
app,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|