Skip to content

Commit 878a87d

Browse files
authored
Rollup merge of rust-lang#114434 - Nilstrieb:indexing-spans, r=est31
Improve spans for indexing expressions fixes rust-lang#114388 Indexing is similar to method calls in having an arbitrary left-hand-side and then something on the right, which is the main part of the expression. Method calls already have a span for that right part, but indexing does not. This means that long method chains that use indexing have really bad spans, especially when the indexing panics and that span in coverted into a panic location. This does the same thing as method calls for the AST and HIR, storing an extra span which is then put into the `fn_span` field in THIR. r? compiler-errors
2 parents 1589759 + ed0dfed commit 878a87d

33 files changed

+42
-42
lines changed

clippy_lints/src/dereference.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ fn in_postfix_position<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) -> boo
800800
&& parent.span.ctxt() == e.span.ctxt()
801801
{
802802
match parent.kind {
803-
ExprKind::Call(child, _) | ExprKind::MethodCall(_, child, _, _) | ExprKind::Index(child, _)
803+
ExprKind::Call(child, _) | ExprKind::MethodCall(_, child, _, _) | ExprKind::Index(child, _, _)
804804
if child.hir_id == e.hir_id => true,
805805
ExprKind::Field(_, _) | ExprKind::Match(_, _, MatchSource::TryDesugar | MatchSource::AwaitDesugar) => true,
806806
_ => false,

clippy_lints/src/functions/must_use.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ fn is_mutated_static(e: &hir::Expr<'_>) -> bool {
221221
match e.kind {
222222
Path(QPath::Resolved(_, path)) => !matches!(path.res, Res::Local(_)),
223223
Path(_) => true,
224-
Field(inner, _) | Index(inner, _) => is_mutated_static(inner),
224+
Field(inner, _) | Index(inner, _, _) => is_mutated_static(inner),
225225
_ => false,
226226
}
227227
}

clippy_lints/src/index_refutable_slice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ impl<'a, 'tcx> Visitor<'tcx> for SliceIndexLintingVisitor<'a, 'tcx> {
254254
// Checking for slice indexing
255255
let parent_id = map.parent_id(expr.hir_id);
256256
if let Some(hir::Node::Expr(parent_expr)) = map.find(parent_id);
257-
if let hir::ExprKind::Index(_, index_expr) = parent_expr.kind;
257+
if let hir::ExprKind::Index(_, index_expr, _) = parent_expr.kind;
258258
if let Some(Constant::Int(index_value)) = constant(cx, cx.typeck_results(), index_expr);
259259
if let Ok(index_value) = index_value.try_into();
260260
if index_value < max_suggested_slice;

clippy_lints/src/indexing_slicing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
103103
return;
104104
}
105105

106-
if let ExprKind::Index(array, index) = &expr.kind {
106+
if let ExprKind::Index(array, index, _) = &expr.kind {
107107
let note = "the suggestion might not be applicable in constant blocks";
108108
let ty = cx.typeck_results().expr_ty(array).peel_refs();
109109
if let Some(range) = higher::Range::hir(index) {

clippy_lints/src/loops/manual_memcpy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ pub(super) fn check<'tcx>(
6060
o.and_then(|(lhs, rhs)| {
6161
let rhs = fetch_cloned_expr(rhs);
6262
if_chain! {
63-
if let ExprKind::Index(base_left, idx_left) = lhs.kind;
64-
if let ExprKind::Index(base_right, idx_right) = rhs.kind;
63+
if let ExprKind::Index(base_left, idx_left, _) = lhs.kind;
64+
if let ExprKind::Index(base_right, idx_right, _) = rhs.kind;
6565
if let Some(ty) = get_slice_like_element_ty(cx, cx.typeck_results().expr_ty(base_left));
6666
if get_slice_like_element_ty(cx, cx.typeck_results().expr_ty(base_right)).is_some();
6767
if let Some((start_left, offset_left)) = get_details_from_idx(cx, idx_left, &starts);

clippy_lints/src/loops/needless_range_loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ impl<'a, 'tcx> Visitor<'tcx> for VarVisitor<'a, 'tcx> {
319319

320320
if_chain! {
321321
// an index op
322-
if let ExprKind::Index(seqexpr, idx) = expr.kind;
322+
if let ExprKind::Index(seqexpr, idx, _) = expr.kind;
323323
if !self.check(idx, seqexpr, expr);
324324
then {
325325
return;

clippy_lints/src/loops/never_loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ fn never_loop_expr<'tcx>(
162162
ExprKind::Binary(_, e1, e2)
163163
| ExprKind::Assign(e1, e2, _)
164164
| ExprKind::AssignOp(_, e1, e2)
165-
| ExprKind::Index(e1, e2) => never_loop_expr_all(cx, &mut [e1, e2].iter().copied(), ignore_ids, main_loop_id),
165+
| ExprKind::Index(e1, e2, _) => never_loop_expr_all(cx, &mut [e1, e2].iter().copied(), ignore_ids, main_loop_id),
166166
ExprKind::Loop(b, _, _, _) => {
167167
// Break can come from the inner loop so remove them.
168168
absorb_break(never_loop_block(cx, b, ignore_ids, main_loop_id))

clippy_lints/src/loops/while_let_on_iterator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ fn try_parse_iter_expr(cx: &LateContext<'_>, mut e: &Expr<'_>) -> Option<IterExp
113113

114114
// Shouldn't have side effects, but there's no way to trace which field is used. So forget which fields have
115115
// already been seen.
116-
ExprKind::Index(base, idx) if !idx.can_have_side_effects() => {
116+
ExprKind::Index(base, idx, _) if !idx.can_have_side_effects() => {
117117
can_move = false;
118118
fields.clear();
119119
e = base;

clippy_lints/src/manual_strip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ fn find_stripping<'tcx>(
204204
if_chain! {
205205
if is_ref_str(self.cx, ex);
206206
let unref = peel_ref(ex);
207-
if let ExprKind::Index(indexed, index) = &unref.kind;
207+
if let ExprKind::Index(indexed, index, _) = &unref.kind;
208208
if let Some(higher::Range { start, end, .. }) = higher::Range::hir(index);
209209
if let ExprKind::Path(path) = &indexed.kind;
210210
if self.cx.qpath_res(path, ex.hir_id) == self.target;

clippy_lints/src/matches/match_on_vec_items.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use super::MATCH_ON_VEC_ITEMS;
1212
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, scrutinee: &'tcx Expr<'_>) {
1313
if_chain! {
1414
if let Some(idx_expr) = is_vec_indexing(cx, scrutinee);
15-
if let ExprKind::Index(vec, idx) = idx_expr.kind;
15+
if let ExprKind::Index(vec, idx, _) = idx_expr.kind;
1616

1717
then {
1818
// FIXME: could be improved to suggest surrounding every pattern with Some(_),
@@ -36,7 +36,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, scrutinee: &'tcx Expr<'_>) {
3636

3737
fn is_vec_indexing<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
3838
if_chain! {
39-
if let ExprKind::Index(array, index) = expr.kind;
39+
if let ExprKind::Index(array, index, _) = expr.kind;
4040
if is_vector(cx, array);
4141
if !is_full_range(cx, index);
4242

0 commit comments

Comments
 (0)