mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 13:13:34 +00:00
Auto merge of #7638 - xFrednet:7569-avoid-indexing-in-clippy, r=Manishearth
Avoid slice indexing in Clippy (down with the ICEs) While working on #7569 I got about 23 lint reports where we can avoid slice indexing by destructing the slice early. This is a preparation PR to avoid fixing them in the lint PR. (The implementation already takes about 300 lines without tests 😅). Either way, this should hopefully be easy to review 🙃 --- changelog: none
This commit is contained in:
commit
051286d7ec
18 changed files with 58 additions and 58 deletions
|
@ -61,8 +61,8 @@ impl<'a, 'tcx> Visitor<'tcx> for ExVisitor<'a, 'tcx> {
|
|||
// do not lint if the closure is called using an iterator (see #1141)
|
||||
if_chain! {
|
||||
if let Some(parent) = get_parent_expr(self.cx, expr);
|
||||
if let ExprKind::MethodCall(_, _, args, _) = parent.kind;
|
||||
let caller = self.cx.typeck_results().expr_ty(&args[0]);
|
||||
if let ExprKind::MethodCall(_, _, [self_arg, ..], _) = &parent.kind;
|
||||
let caller = self.cx.typeck_results().expr_ty(self_arg);
|
||||
if let Some(iter_id) = self.cx.tcx.get_diagnostic_item(sym::Iterator);
|
||||
if implements_trait(self.cx, caller, iter_id, &[]);
|
||||
then {
|
||||
|
|
|
@ -19,7 +19,7 @@ pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
|||
cx.typeck_results().expr_ty(expr),
|
||||
);
|
||||
lint_cast_ptr_alignment(cx, expr, cast_from, cast_to);
|
||||
} else if let ExprKind::MethodCall(method_path, _, args, _) = expr.kind {
|
||||
} else if let ExprKind::MethodCall(method_path, _, [self_arg, ..], _) = &expr.kind {
|
||||
if_chain! {
|
||||
if method_path.ident.name == sym!(cast);
|
||||
if let Some(generic_args) = method_path.args;
|
||||
|
@ -28,7 +28,7 @@ pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
|||
if !is_hir_ty_cfg_dependant(cx, cast_to);
|
||||
then {
|
||||
let (cast_from, cast_to) =
|
||||
(cx.typeck_results().expr_ty(&args[0]), cx.typeck_results().expr_ty(expr));
|
||||
(cx.typeck_results().expr_ty(self_arg), cx.typeck_results().expr_ty(expr));
|
||||
lint_cast_ptr_alignment(cx, expr, cast_from, cast_to);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -362,22 +362,22 @@ fn detect_hypot(cx: &LateContext<'_>, args: &[Expr<'_>]) -> Option<String> {
|
|||
if_chain! {
|
||||
if let ExprKind::MethodCall(
|
||||
PathSegment { ident: lmethod_name, .. },
|
||||
ref _lspan,
|
||||
largs,
|
||||
_lspan,
|
||||
[largs_0, largs_1, ..],
|
||||
_
|
||||
) = add_lhs.kind;
|
||||
) = &add_lhs.kind;
|
||||
if let ExprKind::MethodCall(
|
||||
PathSegment { ident: rmethod_name, .. },
|
||||
ref _rspan,
|
||||
rargs,
|
||||
_rspan,
|
||||
[rargs_0, rargs_1, ..],
|
||||
_
|
||||
) = add_rhs.kind;
|
||||
) = &add_rhs.kind;
|
||||
if lmethod_name.as_str() == "powi" && rmethod_name.as_str() == "powi";
|
||||
if let Some((lvalue, _)) = constant(cx, cx.typeck_results(), &largs[1]);
|
||||
if let Some((rvalue, _)) = constant(cx, cx.typeck_results(), &rargs[1]);
|
||||
if let Some((lvalue, _)) = constant(cx, cx.typeck_results(), largs_1);
|
||||
if let Some((rvalue, _)) = constant(cx, cx.typeck_results(), rargs_1);
|
||||
if Int(2) == lvalue && Int(2) == rvalue;
|
||||
then {
|
||||
return Some(format!("{}.hypot({})", Sugg::hir(cx, &largs[0], ".."), Sugg::hir(cx, &rargs[0], "..")));
|
||||
return Some(format!("{}.hypot({})", Sugg::hir(cx, largs_0, ".."), Sugg::hir(cx, rargs_0, "..")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -407,8 +407,8 @@ fn check_expm1(cx: &LateContext<'_>, expr: &Expr<'_>) {
|
|||
if cx.typeck_results().expr_ty(lhs).is_floating_point();
|
||||
if let Some((value, _)) = constant(cx, cx.typeck_results(), rhs);
|
||||
if F32(1.0) == value || F64(1.0) == value;
|
||||
if let ExprKind::MethodCall(path, _, method_args, _) = lhs.kind;
|
||||
if cx.typeck_results().expr_ty(&method_args[0]).is_floating_point();
|
||||
if let ExprKind::MethodCall(path, _, [self_arg, ..], _) = &lhs.kind;
|
||||
if cx.typeck_results().expr_ty(self_arg).is_floating_point();
|
||||
if path.ident.name.as_str() == "exp";
|
||||
then {
|
||||
span_lint_and_sugg(
|
||||
|
@ -419,7 +419,7 @@ fn check_expm1(cx: &LateContext<'_>, expr: &Expr<'_>) {
|
|||
"consider using",
|
||||
format!(
|
||||
"{}.exp_m1()",
|
||||
Sugg::hir(cx, &method_args[0], "..")
|
||||
Sugg::hir(cx, self_arg, "..")
|
||||
),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
|
@ -617,8 +617,8 @@ fn check_log_division(cx: &LateContext<'_>, expr: &Expr<'_>) {
|
|||
rhs,
|
||||
) = &expr.kind;
|
||||
if are_same_base_logs(cx, lhs, rhs);
|
||||
if let ExprKind::MethodCall(_, _, largs, _) = lhs.kind;
|
||||
if let ExprKind::MethodCall(_, _, rargs, _) = rhs.kind;
|
||||
if let ExprKind::MethodCall(_, _, [largs_self, ..], _) = &lhs.kind;
|
||||
if let ExprKind::MethodCall(_, _, [rargs_self, ..], _) = &rhs.kind;
|
||||
then {
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
|
@ -626,7 +626,7 @@ fn check_log_division(cx: &LateContext<'_>, expr: &Expr<'_>) {
|
|||
expr.span,
|
||||
"log base can be expressed more clearly",
|
||||
"consider using",
|
||||
format!("{}.log({})", Sugg::hir(cx, &largs[0], ".."), Sugg::hir(cx, &rargs[0], ".."),),
|
||||
format!("{}.log({})", Sugg::hir(cx, largs_self, ".."), Sugg::hir(cx, rargs_self, ".."),),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
||||
|
|
|
@ -138,12 +138,12 @@ impl<'tcx, 'l> ArmVisitor<'tcx, 'l> {
|
|||
|
||||
fn is_mutex_lock_call<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
|
||||
if_chain! {
|
||||
if let ExprKind::MethodCall(path, _span, args, _) = &expr.kind;
|
||||
if let ExprKind::MethodCall(path, _span, [self_arg, ..], _) = &expr.kind;
|
||||
if path.ident.as_str() == "lock";
|
||||
let ty = cx.typeck_results().expr_ty(&args[0]);
|
||||
let ty = cx.typeck_results().expr_ty(self_arg);
|
||||
if is_type_diagnostic_item(cx, ty, sym!(mutex_type));
|
||||
then {
|
||||
Some(&args[0])
|
||||
Some(self_arg)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
|
|
@ -46,10 +46,10 @@ impl<'tcx> LateLintPass<'tcx> for OkIfLet {
|
|||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
if_chain! { //begin checking variables
|
||||
if let Some(higher::IfLet { let_pat, let_expr, .. }) = higher::IfLet::hir(cx, expr);
|
||||
if let ExprKind::MethodCall(_, ok_span, result_types, _) = let_expr.kind; //check is expr.ok() has type Result<T,E>.ok(, _)
|
||||
if let ExprKind::MethodCall(_, ok_span, [ref result_types_0, ..], _) = let_expr.kind; //check is expr.ok() has type Result<T,E>.ok(, _)
|
||||
if let PatKind::TupleStruct(QPath::Resolved(_, x), y, _) = let_pat.kind; //get operation
|
||||
if method_chain_args(let_expr, &["ok"]).is_some(); //test to see if using ok() methoduse std::marker::Sized;
|
||||
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&result_types[0]), sym::result_type);
|
||||
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(result_types_0), sym::result_type);
|
||||
if rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_path(x, false)) == "Some";
|
||||
|
||||
then {
|
||||
|
|
|
@ -301,10 +301,10 @@ impl<'a, 'tcx> Visitor<'tcx> for VarVisitor<'a, 'tcx> {
|
|||
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
|
||||
if_chain! {
|
||||
// a range index op
|
||||
if let ExprKind::MethodCall(meth, _, args, _) = expr.kind;
|
||||
if let ExprKind::MethodCall(meth, _, [args_0, args_1, ..], _) = &expr.kind;
|
||||
if (meth.ident.name == sym::index && match_trait_method(self.cx, expr, &paths::INDEX))
|
||||
|| (meth.ident.name == sym::index_mut && match_trait_method(self.cx, expr, &paths::INDEX_MUT));
|
||||
if !self.check(&args[1], &args[0], expr);
|
||||
if !self.check(args_1, args_0, expr);
|
||||
then { return }
|
||||
}
|
||||
|
||||
|
|
|
@ -28,11 +28,11 @@ declare_lint_pass!(MemForget => [MEM_FORGET]);
|
|||
|
||||
impl<'tcx> LateLintPass<'tcx> for MemForget {
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
|
||||
if let ExprKind::Call(path_expr, args) = e.kind {
|
||||
if let ExprKind::Call(path_expr, [ref first_arg, ..]) = e.kind {
|
||||
if let ExprKind::Path(ref qpath) = path_expr.kind {
|
||||
if let Some(def_id) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id() {
|
||||
if match_def_path(cx, def_id, &paths::MEM_FORGET) {
|
||||
let forgot_ty = cx.typeck_results().expr_ty(&args[0]);
|
||||
let forgot_ty = cx.typeck_results().expr_ty(first_arg);
|
||||
|
||||
if forgot_ty.ty_adt_def().map_or(false, |def| def.has_dtor(cx.tcx)) {
|
||||
span_lint(cx, MEM_FORGET, e.span, "usage of `mem::forget` on `Drop` type");
|
||||
|
|
|
@ -96,9 +96,9 @@ pub(super) fn check<'tcx>(
|
|||
(&paths::RESULT, true, &["or", "unwrap_or"], "else"),
|
||||
];
|
||||
|
||||
if let hir::ExprKind::MethodCall(path, _, args, _) = &arg.kind {
|
||||
if let hir::ExprKind::MethodCall(path, _, [self_arg, ..], _) = &arg.kind {
|
||||
if path.ident.name == sym::len {
|
||||
let ty = cx.typeck_results().expr_ty(&args[0]).peel_refs();
|
||||
let ty = cx.typeck_results().expr_ty(self_arg).peel_refs();
|
||||
|
||||
match ty.kind() {
|
||||
ty::Slice(_) | ty::Array(_, _) | ty::Str => return,
|
||||
|
|
|
@ -24,9 +24,9 @@ pub(super) fn derefs_to_slice<'tcx>(
|
|||
}
|
||||
}
|
||||
|
||||
if let hir::ExprKind::MethodCall(path, _, args, _) = expr.kind {
|
||||
if path.ident.name == sym::iter && may_slice(cx, cx.typeck_results().expr_ty(&args[0])) {
|
||||
Some(&args[0])
|
||||
if let hir::ExprKind::MethodCall(path, _, [self_arg, ..], _) = &expr.kind {
|
||||
if path.ident.name == sym::iter && may_slice(cx, cx.typeck_results().expr_ty(self_arg)) {
|
||||
Some(self_arg)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
|
|
@ -513,12 +513,12 @@ fn is_signum(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
|
|||
}
|
||||
|
||||
if_chain! {
|
||||
if let ExprKind::MethodCall(method_name, _, expressions, _) = expr.kind;
|
||||
if let ExprKind::MethodCall(method_name, _, [ref self_arg, ..], _) = expr.kind;
|
||||
if sym!(signum) == method_name.ident.name;
|
||||
// Check that the receiver of the signum() is a float (expressions[0] is the receiver of
|
||||
// the method call)
|
||||
then {
|
||||
return is_float(cx, &expressions[0]);
|
||||
return is_float(cx, self_arg);
|
||||
}
|
||||
}
|
||||
false
|
||||
|
|
|
@ -47,9 +47,9 @@ declare_lint_pass!(MutMutexLock => [MUT_MUTEX_LOCK]);
|
|||
impl<'tcx> LateLintPass<'tcx> for MutMutexLock {
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, ex: &'tcx Expr<'tcx>) {
|
||||
if_chain! {
|
||||
if let ExprKind::MethodCall(path, method_span, args, _) = &ex.kind;
|
||||
if let ExprKind::MethodCall(path, method_span, [self_arg, ..], _) = &ex.kind;
|
||||
if path.ident.name == sym!(lock);
|
||||
let ty = cx.typeck_results().expr_ty(&args[0]);
|
||||
let ty = cx.typeck_results().expr_ty(self_arg);
|
||||
if let ty::Ref(_, inner_ty, Mutability::Mut) = ty.kind();
|
||||
if is_type_diagnostic_item(cx, inner_ty, sym!(mutex_type));
|
||||
then {
|
||||
|
|
|
@ -31,11 +31,11 @@ declare_lint_pass!(OpenOptions => [NONSENSICAL_OPEN_OPTIONS]);
|
|||
|
||||
impl<'tcx> LateLintPass<'tcx> for OpenOptions {
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
|
||||
if let ExprKind::MethodCall(path, _, arguments, _) = e.kind {
|
||||
let obj_ty = cx.typeck_results().expr_ty(&arguments[0]).peel_refs();
|
||||
if let ExprKind::MethodCall(path, _, [self_arg, ..], _) = &e.kind {
|
||||
let obj_ty = cx.typeck_results().expr_ty(self_arg).peel_refs();
|
||||
if path.ident.name == sym!(open) && match_type(cx, obj_ty, &paths::OPEN_OPTIONS) {
|
||||
let mut options = Vec::new();
|
||||
get_open_options(cx, &arguments[0], &mut options);
|
||||
get_open_options(cx, self_arg, &mut options);
|
||||
check_open_options(cx, &options, e.span);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -92,13 +92,13 @@ fn expr_as_ptr_offset_call<'tcx>(
|
|||
cx: &LateContext<'tcx>,
|
||||
expr: &'tcx Expr<'_>,
|
||||
) -> Option<(&'tcx Expr<'tcx>, &'tcx Expr<'tcx>, Method)> {
|
||||
if let ExprKind::MethodCall(path_segment, _, args, _) = expr.kind {
|
||||
if is_expr_ty_raw_ptr(cx, &args[0]) {
|
||||
if let ExprKind::MethodCall(path_segment, _, [arg_0, arg_1, ..], _) = &expr.kind {
|
||||
if is_expr_ty_raw_ptr(cx, arg_0) {
|
||||
if path_segment.ident.name == sym::offset {
|
||||
return Some((&args[0], &args[1], Method::Offset));
|
||||
return Some((arg_0, arg_1, Method::Offset));
|
||||
}
|
||||
if path_segment.ident.name == sym!(wrapping_offset) {
|
||||
return Some((&args[0], &args[1], Method::WrappingOffset));
|
||||
return Some((arg_0, arg_1, Method::WrappingOffset));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -345,9 +345,9 @@ declare_lint_pass!(StrToString => [STR_TO_STRING]);
|
|||
impl LateLintPass<'_> for StrToString {
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
|
||||
if_chain! {
|
||||
if let ExprKind::MethodCall(path, _, args, _) = &expr.kind;
|
||||
if let ExprKind::MethodCall(path, _, [self_arg, ..], _) = &expr.kind;
|
||||
if path.ident.name == sym!(to_string);
|
||||
let ty = cx.typeck_results().expr_ty(&args[0]);
|
||||
let ty = cx.typeck_results().expr_ty(self_arg);
|
||||
if let ty::Ref(_, ty, ..) = ty.kind();
|
||||
if *ty.kind() == ty::Str;
|
||||
then {
|
||||
|
@ -394,9 +394,9 @@ declare_lint_pass!(StringToString => [STRING_TO_STRING]);
|
|||
impl LateLintPass<'_> for StringToString {
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
|
||||
if_chain! {
|
||||
if let ExprKind::MethodCall(path, _, args, _) = &expr.kind;
|
||||
if let ExprKind::MethodCall(path, _, [self_arg, ..], _) = &expr.kind;
|
||||
if path.ident.name == sym!(to_string);
|
||||
let ty = cx.typeck_results().expr_ty(&args[0]);
|
||||
let ty = cx.typeck_results().expr_ty(self_arg);
|
||||
if is_type_diagnostic_item(cx, ty, sym::string_type);
|
||||
then {
|
||||
span_lint_and_help(
|
||||
|
|
|
@ -92,11 +92,11 @@ impl LateLintPass<'_> for ToStringInDisplay {
|
|||
if_chain! {
|
||||
if self.in_display_impl;
|
||||
if let Some(self_hir_id) = self.self_hir_id;
|
||||
if let ExprKind::MethodCall(path, _, args, _) = expr.kind;
|
||||
if let ExprKind::MethodCall(path, _, [ref self_arg, ..], _) = expr.kind;
|
||||
if path.ident.name == sym!(to_string);
|
||||
if let Some(expr_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
|
||||
if is_diag_trait_item(cx, expr_def_id, sym::ToString);
|
||||
if path_to_local_id(&args[0], self_hir_id);
|
||||
if path_to_local_id(self_arg, self_hir_id);
|
||||
then {
|
||||
span_lint(
|
||||
cx,
|
||||
|
|
|
@ -37,8 +37,8 @@ declare_lint_pass!(UndroppedManuallyDrops => [UNDROPPED_MANUALLY_DROPS]);
|
|||
|
||||
impl LateLintPass<'tcx> for UndroppedManuallyDrops {
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
if let Some(args) = match_function_call(cx, expr, &paths::DROP) {
|
||||
let ty = cx.typeck_results().expr_ty(&args[0]);
|
||||
if let Some([arg_0, ..]) = match_function_call(cx, expr, &paths::DROP) {
|
||||
let ty = cx.typeck_results().expr_ty(arg_0);
|
||||
if is_type_lang_item(cx, ty, lang_items::LangItem::ManuallyDrop) {
|
||||
span_lint_and_help(
|
||||
cx,
|
||||
|
|
|
@ -45,20 +45,20 @@ impl<'tcx> LateLintPass<'tcx> for UnusedIoAmount {
|
|||
|
||||
match expr.kind {
|
||||
hir::ExprKind::Match(res, _, _) if is_try(cx, expr).is_some() => {
|
||||
if let hir::ExprKind::Call(func, args) = res.kind {
|
||||
if let hir::ExprKind::Call(func, [ref arg_0, ..]) = res.kind {
|
||||
if matches!(
|
||||
func.kind,
|
||||
hir::ExprKind::Path(hir::QPath::LangItem(hir::LangItem::TryTraitBranch, _))
|
||||
) {
|
||||
check_map_error(cx, &args[0], expr);
|
||||
check_map_error(cx, arg_0, expr);
|
||||
}
|
||||
} else {
|
||||
check_map_error(cx, res, expr);
|
||||
}
|
||||
},
|
||||
hir::ExprKind::MethodCall(path, _, args, _) => match &*path.ident.as_str() {
|
||||
hir::ExprKind::MethodCall(path, _, [ref arg_0, ..], _) => match &*path.ident.as_str() {
|
||||
"expect" | "unwrap" | "unwrap_or" | "unwrap_or_else" => {
|
||||
check_map_error(cx, &args[0], expr);
|
||||
check_map_error(cx, arg_0, expr);
|
||||
},
|
||||
_ => (),
|
||||
},
|
||||
|
|
|
@ -504,10 +504,10 @@ impl<'tcx> LateLintPass<'tcx> for CompilerLintFunctions {
|
|||
}
|
||||
|
||||
if_chain! {
|
||||
if let ExprKind::MethodCall(path, _, args, _) = expr.kind;
|
||||
if let ExprKind::MethodCall(path, _, [self_arg, ..], _) = &expr.kind;
|
||||
let fn_name = path.ident;
|
||||
if let Some(sugg) = self.map.get(&*fn_name.as_str());
|
||||
let ty = cx.typeck_results().expr_ty(&args[0]).peel_refs();
|
||||
let ty = cx.typeck_results().expr_ty(self_arg).peel_refs();
|
||||
if match_type(cx, ty, &paths::EARLY_CONTEXT)
|
||||
|| match_type(cx, ty, &paths::LATE_CONTEXT);
|
||||
then {
|
||||
|
|
Loading…
Reference in a new issue