Skip to content

Commit 5af88e3

Browse files
committed
Auto merge of #6034 - rail-rain:fix_fp_in_indexing_slicing, r=flip1995
Treat refs to arrays the same as owned arrays in `indexing_slicing` and `out_of_bounds_indexing` Fixes #6021 ...and remove `walk_ptrs_ty` in favour of `peel_refs`, which came in 2019. --- changelog: Fix a false positive in `indexing_slicing` and `out_of_bounds_indexing` where references to arrays weren't treated as arrays.
2 parents 06f1902 + ce06472 commit 5af88e3

24 files changed

+67
-99
lines changed

clippy_lints/src/bytecount.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::utils::{
2-
contains_name, get_pat_name, match_type, paths, single_segment_path, snippet_with_applicability,
3-
span_lint_and_sugg, walk_ptrs_ty,
2+
contains_name, get_pat_name, match_type, paths, single_segment_path, snippet_with_applicability, span_lint_and_sugg,
43
};
54
use if_chain::if_chain;
65
use rustc_ast::ast::UintTy;
@@ -53,7 +52,7 @@ impl<'tcx> LateLintPass<'tcx> for ByteCount {
5352
if let ExprKind::Binary(ref op, ref l, ref r) = body.value.kind;
5453
if op.node == BinOpKind::Eq;
5554
if match_type(cx,
56-
walk_ptrs_ty(cx.typeck_results().expr_ty(&filter_args[0])),
55+
cx.typeck_results().expr_ty(&filter_args[0]).peel_refs(),
5756
&paths::SLICE_ITER);
5857
then {
5958
let needle = match get_path_name(l) {
@@ -63,7 +62,7 @@ impl<'tcx> LateLintPass<'tcx> for ByteCount {
6362
_ => { return; }
6463
}
6564
};
66-
if ty::Uint(UintTy::U8) != *walk_ptrs_ty(cx.typeck_results().expr_ty(needle)).kind() {
65+
if ty::Uint(UintTy::U8) != *cx.typeck_results().expr_ty(needle).peel_refs().kind() {
6766
return;
6867
}
6968
let haystack = if let ExprKind::MethodCall(ref path, _, ref args, _) =

clippy_lints/src/duration_subsec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_span::source_map::Spanned;
77

88
use crate::consts::{constant, Constant};
99
use crate::utils::paths;
10-
use crate::utils::{match_type, snippet_with_applicability, span_lint_and_sugg, walk_ptrs_ty};
10+
use crate::utils::{match_type, snippet_with_applicability, span_lint_and_sugg};
1111

1212
declare_clippy_lint! {
1313
/// **What it does:** Checks for calculation of subsecond microseconds or milliseconds
@@ -43,7 +43,7 @@ impl<'tcx> LateLintPass<'tcx> for DurationSubsec {
4343
if_chain! {
4444
if let ExprKind::Binary(Spanned { node: BinOpKind::Div, .. }, ref left, ref right) = expr.kind;
4545
if let ExprKind::MethodCall(ref method_path, _ , ref args, _) = left.kind;
46-
if match_type(cx, walk_ptrs_ty(cx.typeck_results().expr_ty(&args[0])), &paths::DURATION);
46+
if match_type(cx, cx.typeck_results().expr_ty(&args[0]).peel_refs(), &paths::DURATION);
4747
if let Some((Constant::Int(divisor), _)) = constant(cx, cx.typeck_results(), right);
4848
then {
4949
let suggested_fn = match (method_path.ident.as_str().as_ref(), divisor) {

clippy_lints/src/entry.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::SpanlessEq;
22
use crate::utils::{get_item_name, higher, is_type_diagnostic_item, match_type, paths, snippet, snippet_opt};
3-
use crate::utils::{snippet_with_applicability, span_lint_and_then, walk_ptrs_ty};
3+
use crate::utils::{snippet_with_applicability, span_lint_and_then};
44
use if_chain::if_chain;
55
use rustc_errors::Applicability;
66
use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
@@ -106,7 +106,7 @@ fn check_cond<'a>(cx: &LateContext<'_>, check: &'a Expr<'a>) -> Option<(&'static
106106
if let ExprKind::AddrOf(BorrowKind::Ref, _, ref key) = params[1].kind;
107107
then {
108108
let map = &params[0];
109-
let obj_ty = walk_ptrs_ty(cx.typeck_results().expr_ty(map));
109+
let obj_ty = cx.typeck_results().expr_ty(map).peel_refs();
110110

111111
return if match_type(cx, obj_ty, &paths::BTREEMAP) {
112112
Some(("BTreeMap", map, key))

clippy_lints/src/fallible_impl_from.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use crate::utils::paths::{BEGIN_PANIC, BEGIN_PANIC_FMT, FROM_TRAIT};
2-
use crate::utils::{
3-
is_expn_of, is_type_diagnostic_item, match_def_path, method_chain_args, span_lint_and_then, walk_ptrs_ty,
4-
};
2+
use crate::utils::{is_expn_of, is_type_diagnostic_item, match_def_path, method_chain_args, span_lint_and_then};
53
use if_chain::if_chain;
64
use rustc_hir as hir;
75
use rustc_lint::{LateContext, LateLintPass};
@@ -96,7 +94,7 @@ fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, impl_items: &[h
9694

9795
// check for `unwrap`
9896
if let Some(arglists) = method_chain_args(expr, &["unwrap"]) {
99-
let reciever_ty = walk_ptrs_ty(self.typeck_results.expr_ty(&arglists[0][0]));
97+
let reciever_ty = self.typeck_results.expr_ty(&arglists[0][0]).peel_refs();
10098
if is_type_diagnostic_item(self.lcx, reciever_ty, sym!(option_type))
10199
|| is_type_diagnostic_item(self.lcx, reciever_ty, sym!(result_type))
102100
{

clippy_lints/src/format.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::utils::paths;
22
use crate::utils::{
33
is_expn_of, is_type_diagnostic_item, last_path_segment, match_def_path, match_function_call, snippet,
4-
span_lint_and_then, walk_ptrs_ty,
4+
span_lint_and_then,
55
};
66
use if_chain::if_chain;
77
use rustc_ast::ast::LitKind;
@@ -90,7 +90,7 @@ fn on_argumentv1_new<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, arms: &
9090
if let PatKind::Tuple(ref pats, None) = arms[0].pat.kind;
9191
if pats.len() == 1;
9292
then {
93-
let ty = walk_ptrs_ty(cx.typeck_results().pat_ty(&pats[0]));
93+
let ty = cx.typeck_results().pat_ty(&pats[0]).peel_refs();
9494
if *ty.kind() != rustc_middle::ty::Str && !is_type_diagnostic_item(cx, ty, sym!(string_type)) {
9595
return None;
9696
}

clippy_lints/src/indexing_slicing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ declare_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]
8888
impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
8989
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
9090
if let ExprKind::Index(ref array, ref index) = &expr.kind {
91-
let ty = cx.typeck_results().expr_ty(array);
91+
let ty = cx.typeck_results().expr_ty(array).peel_refs();
9292
if let Some(range) = higher::range(index) {
9393
// Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..]
9494
if let ty::Array(_, s) = ty.kind() {

clippy_lints/src/inherent_to_string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
55

66
use crate::utils::{
77
get_trait_def_id, implements_trait, is_type_diagnostic_item, paths, return_ty, span_lint_and_help,
8-
trait_ref_of_method, walk_ptrs_ty,
8+
trait_ref_of_method,
99
};
1010

1111
declare_clippy_lint! {
@@ -125,7 +125,7 @@ fn show_lint(cx: &LateContext<'_>, item: &ImplItem<'_>) {
125125
// Get the real type of 'self'
126126
let fn_def_id = cx.tcx.hir().local_def_id(item.hir_id);
127127
let self_type = cx.tcx.fn_sig(fn_def_id).input(0);
128-
let self_type = walk_ptrs_ty(self_type.skip_binder());
128+
let self_type = self_type.skip_binder().peel_refs();
129129

130130
// Emit either a warning or an error
131131
if implements_trait(cx, self_type, display_trait_id, &[]) {

clippy_lints/src/len_zero.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{get_item_name, snippet_with_applicability, span_lint, span_lint_and_sugg, walk_ptrs_ty};
1+
use crate::utils::{get_item_name, snippet_with_applicability, span_lint, span_lint_and_sugg};
22
use rustc_ast::ast::LitKind;
33
use rustc_data_structures::fx::FxHashSet;
44
use rustc_errors::Applicability;
@@ -285,7 +285,7 @@ fn has_is_empty(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
285285
})
286286
}
287287

288-
let ty = &walk_ptrs_ty(cx.typeck_results().expr_ty(expr));
288+
let ty = &cx.typeck_results().expr_ty(expr).peel_refs();
289289
match ty.kind() {
290290
ty::Dynamic(ref tt, ..) => tt.principal().map_or(false, |principal| {
291291
cx.tcx

clippy_lints/src/match_on_vec_items.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::utils::walk_ptrs_ty;
21
use crate::utils::{is_type_diagnostic_item, is_type_lang_item, snippet, span_lint_and_sugg};
32
use if_chain::if_chain;
43
use rustc_errors::Applicability;
@@ -90,12 +89,12 @@ fn is_vec_indexing<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Opti
9089

9190
fn is_vector(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
9291
let ty = cx.typeck_results().expr_ty(expr);
93-
let ty = walk_ptrs_ty(ty);
92+
let ty = ty.peel_refs();
9493
is_type_diagnostic_item(cx, ty, sym!(vec_type))
9594
}
9695

9796
fn is_full_range(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
9897
let ty = cx.typeck_results().expr_ty(expr);
99-
let ty = walk_ptrs_ty(ty);
98+
let ty = ty.peel_refs();
10099
is_type_lang_item(cx, ty, LangItem::RangeFull)
101100
}

clippy_lints/src/matches.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::utils::{
66
expr_block, get_arg_name, get_parent_expr, in_macro, indent_of, is_allowed, is_expn_of, is_refutable,
77
is_type_diagnostic_item, is_wild, match_qpath, match_type, match_var, multispan_sugg, remove_blocks, snippet,
88
snippet_block, snippet_with_applicability, span_lint_and_help, span_lint_and_note, span_lint_and_sugg,
9-
span_lint_and_then, walk_ptrs_ty,
9+
span_lint_and_then,
1010
};
1111
use if_chain::if_chain;
1212
use rustc_ast::ast::LitKind;
@@ -794,7 +794,7 @@ fn check_overlapping_arms<'tcx>(cx: &LateContext<'tcx>, ex: &'tcx Expr<'_>, arms
794794
}
795795

796796
fn check_wild_err_arm(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
797-
let ex_ty = walk_ptrs_ty(cx.typeck_results().expr_ty(ex));
797+
let ex_ty = cx.typeck_results().expr_ty(ex).peel_refs();
798798
if is_type_diagnostic_item(cx, ex_ty, sym!(result_type)) {
799799
for arm in arms {
800800
if let PatKind::TupleStruct(ref path, ref inner, _) = arm.pat.kind {

0 commit comments

Comments
 (0)