2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-04-08 15:50:13 +00:00
|
|
|
use clippy_utils::source::snippet_with_context;
|
2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::ty::is_type_lang_item;
|
2021-04-08 15:50:13 +00:00
|
|
|
use clippy_utils::{get_parent_expr, in_macro};
|
2021-01-30 17:06:34 +00:00
|
|
|
use if_chain::if_chain;
|
|
|
|
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};
|
|
|
|
use rustc_middle::ty::TyS;
|
2021-01-30 17:06:34 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for redundant slicing expressions which use the full range, and
|
|
|
|
/// do not change the type.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** It unnecessarily adds complexity to the expression.
|
|
|
|
///
|
|
|
|
/// **Known problems:** If the type being sliced has an implementation of `Index<RangeFull>`
|
|
|
|
/// 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.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// ```ignore
|
|
|
|
/// fn get_slice(x: &[u32]) -> &[u32] {
|
|
|
|
/// &x[..]
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
|
|
|
/// ```ignore
|
|
|
|
/// fn get_slice(x: &[u32]) -> &[u32] {
|
|
|
|
/// x
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub REDUNDANT_SLICING,
|
|
|
|
complexity,
|
|
|
|
"redundant slicing of the whole range of a type"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(RedundantSlicing => [REDUNDANT_SLICING]);
|
|
|
|
|
|
|
|
impl LateLintPass<'_> for RedundantSlicing {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2021-04-08 15:50:13 +00:00
|
|
|
if in_macro(expr.span) {
|
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);
|
|
|
|
if TyS::same_type(cx.typeck_results().expr_ty(expr), cx.typeck_results().expr_ty(indexed));
|
|
|
|
then {
|
|
|
|
let mut app = Applicability::MachineApplicable;
|
2021-04-08 15:50:13 +00:00
|
|
|
let snip = snippet_with_context(cx, indexed.span, ctxt, "..", &mut app).0;
|
|
|
|
|
|
|
|
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!(
|
|
|
|
get_parent_expr(cx, expr),
|
|
|
|
Some(Expr {
|
|
|
|
kind: ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, _),
|
|
|
|
..
|
|
|
|
})
|
|
|
|
) {
|
|
|
|
// The slice was used to make a temporary reference.
|
|
|
|
("&*", "reborrow the original value instead")
|
|
|
|
} else {
|
|
|
|
("", "use the original value instead")
|
|
|
|
};
|
2021-01-30 17:06:34 +00:00
|
|
|
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
REDUNDANT_SLICING,
|
|
|
|
expr.span,
|
|
|
|
"redundant slicing of the whole range",
|
2021-04-08 15:50:13 +00:00
|
|
|
help_str,
|
|
|
|
format!("{}{}", reborrow_str, snip),
|
2021-01-30 17:06:34 +00:00
|
|
|
app,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|