Skip to content

Commit 97d6049

Browse files
committed
Mark range expr with desugaring
1 parent 6a0aa12 commit 97d6049

18 files changed

+80
-44
lines changed

clippy_lints/src/loops/manual_memcpy.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub(super) fn check<'tcx>(
2828
start: Some(start),
2929
end: Some(end),
3030
limits,
31+
span: _,
3132
}) = higher::Range::hir(arg)
3233
// the var must be a single name
3334
&& let PatKind::Binding(_, canonical_id, _, _) = pat.kind

clippy_lints/src/loops/manual_slice_fill.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub(super) fn check<'tcx>(
3131
start: Some(start),
3232
end: Some(end),
3333
limits: RangeLimits::HalfOpen,
34+
span: _,
3435
}) = higher::Range::hir(arg)
3536
&& let ExprKind::Lit(Spanned {
3637
node: LitKind::Int(Pu128(0), _),

clippy_lints/src/loops/needless_range_loop.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub(super) fn check<'tcx>(
3030
start: Some(start),
3131
ref end,
3232
limits,
33+
span,
3334
}) = higher::Range::hir(arg)
3435
// the var must be a single name
3536
&& let PatKind::Binding(_, canonical_id, ident, _) = pat.kind
@@ -149,15 +150,15 @@ pub(super) fn check<'tcx>(
149150
span_lint_and_then(
150151
cx,
151152
NEEDLESS_RANGE_LOOP,
152-
arg.span,
153+
span,
153154
format!("the loop variable `{}` is used to index `{indexed}`", ident.name),
154155
|diag| {
155156
diag.multipart_suggestion(
156157
"consider using an iterator and enumerate()",
157158
vec![
158159
(pat.span, format!("({}, <item>)", ident.name)),
159160
(
160-
arg.span,
161+
span,
161162
format!("{indexed}.{method}().enumerate(){method_1}{method_2}"),
162163
),
163164
],
@@ -175,12 +176,12 @@ pub(super) fn check<'tcx>(
175176
span_lint_and_then(
176177
cx,
177178
NEEDLESS_RANGE_LOOP,
178-
arg.span,
179+
span,
179180
format!("the loop variable `{}` is only used to index `{indexed}`", ident.name),
180181
|diag| {
181182
diag.multipart_suggestion(
182183
"consider using an iterator",
183-
vec![(pat.span, "<item>".to_string()), (arg.span, repl)],
184+
vec![(pat.span, "<item>".to_string()), (span, repl)],
184185
Applicability::HasPlaceholders,
185186
);
186187
},

clippy_lints/src/manual_is_ascii_check.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualIsAsciiCheck {
110110
start: Some(start),
111111
end: Some(end),
112112
limits: RangeLimits::Closed,
113+
span: _,
113114
}) = higher::Range::hir(receiver)
114115
&& !matches!(cx.typeck_results().expr_ty(arg).peel_refs().kind(), ty::Param(_))
115116
{

clippy_lints/src/methods/iter_next_slice.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, cal
3030
start: Some(start_expr),
3131
end: None,
3232
limits: ast::RangeLimits::HalfOpen,
33+
span: _,
3334
}) = higher::Range::hir(index_expr)
3435
&& let hir::ExprKind::Lit(start_lit) = &start_expr.kind
3536
&& let ast::LitKind::Int(start_idx, _) = start_lit.node

clippy_lints/src/methods/map_with_unused_argument_over_ranges.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub(super) fn check(
6363
receiver: &Expr<'_>,
6464
arg: &Expr<'_>,
6565
msrv: Msrv,
66-
method_call_span: Span,
66+
method_name_span: Span,
6767
) {
6868
let mut applicability = Applicability::MaybeIncorrect;
6969
if let Some(range) = higher::Range::hir(receiver)
@@ -105,7 +105,7 @@ pub(super) fn check(
105105
// collate all our parts here and then remove those that are empty.
106106
let mut parts = vec![
107107
(
108-
receiver.span.to(method_call_span),
108+
ex.span.with_hi(method_name_span.hi()),
109109
format!("{exec_context}::iter::{method_to_use_name}"),
110110
),
111111
new_span,

clippy_lints/src/methods/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4854,8 +4854,8 @@ impl_lint_pass!(Methods => [
48544854
/// come from expansion.
48554855
pub fn method_call<'tcx>(recv: &'tcx Expr<'tcx>) -> Option<(Symbol, &'tcx Expr<'tcx>, &'tcx [Expr<'tcx>], Span, Span)> {
48564856
if let ExprKind::MethodCall(path, receiver, args, call_span) = recv.kind
4857-
&& !args.iter().any(|e| e.span.from_expansion())
4858-
&& !receiver.span.from_expansion()
4857+
&& !args.iter().any(|e| e.range_span().unwrap_or(e.span).from_expansion())
4858+
&& !receiver.range_span().unwrap_or(receiver.span).from_expansion()
48594859
{
48604860
Some((path.ident.name, receiver, args, path.ident.span, call_span))
48614861
} else {

clippy_lints/src/no_effect.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl NoEffect {
122122
return true;
123123
}
124124

125-
if expr.span.from_expansion() {
125+
if expr.range_span().unwrap_or(expr.span).from_expansion() {
126126
return false;
127127
}
128128
let expr = peel_blocks(expr);
@@ -273,7 +273,7 @@ fn check_unnecessary_operation(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
273273
if let StmtKind::Semi(expr) = stmt.kind
274274
&& !stmt.span.in_external_macro(cx.sess().source_map())
275275
&& let ctxt = stmt.span.ctxt()
276-
&& expr.span.ctxt() == ctxt
276+
&& expr.range_span().unwrap_or(expr.span).ctxt() == ctxt
277277
&& let Some(reduced) = reduce_expression(cx, expr)
278278
&& reduced.iter().all(|e| e.span.ctxt() == ctxt)
279279
{
@@ -330,7 +330,7 @@ fn check_unnecessary_operation(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
330330
}
331331

332332
fn reduce_expression<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<Vec<&'a Expr<'a>>> {
333-
if expr.span.from_expansion() {
333+
if expr.range_span().unwrap_or(expr.span).from_expansion() {
334334
return None;
335335
}
336336
match expr.kind {

clippy_lints/src/ranges.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -501,17 +501,18 @@ fn check_range_switch<'tcx>(
501501
msg: &'static str,
502502
operator: &str,
503503
) {
504-
if expr.span.can_be_used_for_suggestions()
505-
&& let Some(higher::Range {
504+
if let Some(range) = higher::Range::hir(expr)
505+
&& let higher::Range {
506506
start,
507507
end: Some(end),
508508
limits,
509-
}) = higher::Range::hir(expr)
509+
span,
510+
} = range
511+
&& span.can_be_used_for_suggestions()
510512
&& limits == kind
511513
&& let Some(y) = predicate(cx, end)
512514
&& can_switch_ranges(cx, expr, kind, cx.typeck_results().expr_ty(y))
513515
{
514-
let span = expr.span;
515516
span_lint_and_then(cx, lint, span, msg, |diag| {
516517
let mut app = Applicability::MachineApplicable;
517518
let start = start.map_or(String::new(), |x| {
@@ -567,6 +568,7 @@ fn check_reversed_empty_range(cx: &LateContext<'_>, expr: &Expr<'_>) {
567568
start: Some(start),
568569
end: Some(end),
569570
limits,
571+
span,
570572
}) = higher::Range::hir(expr)
571573
&& let ty = cx.typeck_results().expr_ty(start)
572574
&& let ty::Int(_) | ty::Uint(_) = ty.kind()
@@ -582,7 +584,7 @@ fn check_reversed_empty_range(cx: &LateContext<'_>, expr: &Expr<'_>) {
582584
span_lint(
583585
cx,
584586
REVERSED_EMPTY_RANGES,
585-
expr.span,
587+
span,
586588
"this range is reversed and using it to index a slice will panic at run-time",
587589
);
588590
}
@@ -591,7 +593,7 @@ fn check_reversed_empty_range(cx: &LateContext<'_>, expr: &Expr<'_>) {
591593
span_lint_and_then(
592594
cx,
593595
REVERSED_EMPTY_RANGES,
594-
expr.span,
596+
span,
595597
"this range is empty so it will yield no values",
596598
|diag| {
597599
if ordering != Ordering::Equal {
@@ -603,7 +605,7 @@ fn check_reversed_empty_range(cx: &LateContext<'_>, expr: &Expr<'_>) {
603605
};
604606

605607
diag.span_suggestion(
606-
expr.span,
608+
span,
607609
"consider using the following if you are attempting to iterate over this \
608610
range in reverse",
609611
format!("({end_snippet}{dots}{start_snippet}).rev()"),

clippy_lints/src/unnecessary_struct_initialization.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use clippy_utils::{get_parent_expr, is_mutable};
66
use rustc_hir::{Expr, ExprField, ExprKind, Path, QPath, StructTailExpr, UnOp};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_session::declare_lint_pass;
9+
use rustc_span::{DesugaringKind, Ident};
910

1011
declare_clippy_lint! {
1112
/// ### What it does
@@ -52,7 +53,8 @@ impl LateLintPass<'_> for UnnecessaryStruct {
5253
return;
5354
};
5455

55-
if expr.span.from_expansion() {
56+
let expr_span = expr.range_span().unwrap_or(expr.span);
57+
if expr_span.from_expansion() {
5658
// Prevent lint from hitting inside macro code
5759
return;
5860
}
@@ -80,7 +82,7 @@ impl LateLintPass<'_> for UnnecessaryStruct {
8082
span_lint_and_sugg(
8183
cx,
8284
UNNECESSARY_STRUCT_INITIALIZATION,
83-
expr.span,
85+
expr_span,
8486
"unnecessary struct building",
8587
"replace with",
8688
snippet(cx, sugg, "..").into_owned(),
@@ -130,7 +132,7 @@ fn same_path_in_all_fields<'tcx>(
130132
// expression type matches
131133
&& ty == cx.typeck_results().expr_ty(src_expr)
132134
// field name matches
133-
&& f.ident == ident
135+
&& ident_without_range_desugaring(f.ident) == ident
134136
// assigned from a path expression
135137
&& let ExprKind::Path(QPath::Resolved(None, src_path)) = src_expr.kind
136138
{
@@ -197,3 +199,14 @@ fn path_matches_base(path: &Path<'_>, base: &Expr<'_>) -> bool {
197199
};
198200
path.res == base_path.res
199201
}
202+
203+
fn ident_without_range_desugaring(ident: Ident) -> Ident {
204+
if ident.span.desugaring_kind() == Some(DesugaringKind::RangeExpr) {
205+
Ident {
206+
span: ident.span.parent_callsite().unwrap(),
207+
..ident
208+
}
209+
} else {
210+
ident
211+
}
212+
}

0 commit comments

Comments
 (0)