Skip to content

Commit 76f16ed

Browse files
committed
Remove span from hir::Stmt.
1 parent 0bf0ea2 commit 76f16ed

File tree

18 files changed

+50
-40
lines changed

18 files changed

+50
-40
lines changed

src/librustc_ast_lowering/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,7 +2385,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23852385
hir::Stmt {
23862386
hir_id: self.lower_node_id(s.id, s.span),
23872387
kind: hir::StmtKind::Local(self.arena.alloc(l)),
2388-
span: s.span,
23892388
}
23902389
});
23912390
return ids;
@@ -2402,7 +2401,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24022401
.map(|id| self.lower_node_id(id, s.span))
24032402
.unwrap_or_else(|| self.next_id(s.span));
24042403

2405-
hir::Stmt { hir_id, kind: hir::StmtKind::Item(item_id), span: s.span }
2404+
hir::Stmt { hir_id, kind: hir::StmtKind::Item(item_id) }
24062405
})
24072406
.collect();
24082407
}
@@ -2411,7 +2410,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24112410
StmtKind::Empty => return smallvec![],
24122411
StmtKind::MacCall(..) => panic!("shouldn't exist here"),
24132412
};
2414-
smallvec![hir::Stmt { hir_id: self.lower_node_id(s.id, s.span), kind, span: s.span }]
2413+
smallvec![hir::Stmt { hir_id: self.lower_node_id(s.id, s.span), kind }]
24152414
}
24162415

24172416
fn lower_block_check_mode(&mut self, b: &BlockCheckMode) -> hir::BlockCheckMode {
@@ -2446,7 +2445,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24462445
// Helper methods for building HIR.
24472446

24482447
fn stmt(&mut self, span: Span, kind: hir::StmtKind<'hir>) -> hir::Stmt<'hir> {
2449-
hir::Stmt { span, kind, hir_id: self.next_id(span) }
2448+
hir::Stmt { kind, hir_id: self.next_id(span) }
24502449
}
24512450

24522451
fn stmt_expr(&mut self, span: Span, expr: hir::Expr<'hir>) -> hir::Stmt<'hir> {

src/librustc_hir/hir.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,6 @@ impl UnOp {
11031103
pub struct Stmt<'hir> {
11041104
pub hir_id: HirId,
11051105
pub kind: StmtKind<'hir>,
1106-
pub span: Span,
11071106
}
11081107

11091108
/// The contents of a statement.

src/librustc_hir_pretty/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,8 @@ impl<'a> State<'a> {
10401040
}
10411041

10421042
pub fn print_stmt(&mut self, st: &hir::Stmt<'_>) {
1043-
self.maybe_print_comment(st.span.lo());
1043+
let span = self.span(st.hir_id);
1044+
self.maybe_print_comment(span.lo());
10441045
match st.kind {
10451046
hir::StmtKind::Local(ref loc) => {
10461047
self.print_local(loc.init.as_deref(), |this| this.print_local_decl(&loc));
@@ -1059,7 +1060,7 @@ impl<'a> State<'a> {
10591060
if stmt_ends_with_semi(&st.kind) {
10601061
self.s.word(";");
10611062
}
1062-
self.maybe_print_trailing_comment(st.span, None)
1063+
self.maybe_print_trailing_comment(span, None)
10631064
}
10641065

10651066
pub fn print_block(&mut self, blk: &hir::Block<'_>) {

src/librustc_lint/unused.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
4747
}
4848

4949
let ty = cx.tables.expr_ty(&expr);
50-
let type_permits_lack_of_use = check_must_use_ty(cx, ty, &expr, s.span, "", "", 1);
50+
let span = cx.tcx.hir().span(s.hir_id);
51+
let type_permits_lack_of_use = check_must_use_ty(cx, ty, &expr, span, "", "", 1);
5152

5253
let mut fn_warned = false;
5354
let mut op_warned = false;
@@ -69,7 +70,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
6970
_ => None,
7071
};
7172
if let Some(def_id) = maybe_def_id {
72-
fn_warned = check_must_use_def(cx, def_id, s.span, "return value of ", "");
73+
fn_warned = check_must_use_def(cx, def_id, span, "return value of ", "");
7374
} else if type_permits_lack_of_use {
7475
// We don't warn about unused unit or uninhabited types.
7576
// (See https://github.com/rust-lang/rust/issues/43806 for details.)
@@ -111,7 +112,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
111112
}
112113

113114
if !(type_permits_lack_of_use || fn_warned || op_warned) {
114-
cx.struct_span_lint(UNUSED_RESULTS, s.span, |lint| lint.build("unused result").emit());
115+
cx.struct_span_lint(UNUSED_RESULTS, span, |lint| lint.build("unused result").emit());
115116
}
116117

117118
// Returns whether an error has been emitted (and thus another does not need to be later).
@@ -255,7 +256,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PathStatements {
255256
fn check_stmt(&mut self, cx: &LateContext<'_, '_>, s: &hir::Stmt<'_>) {
256257
if let hir::StmtKind::Semi(ref expr) = s.kind {
257258
if let hir::ExprKind::Path(_) = expr.kind {
258-
cx.struct_span_lint(PATH_STATEMENTS, s.span, |lint| {
259+
let span = cx.tcx.hir().span(s.hir_id);
260+
cx.struct_span_lint(PATH_STATEMENTS, span, |lint| {
259261
lint.build("path statement with no effect").emit()
260262
});
261263
}

src/librustc_middle/hir/map/collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
444444
}
445445

446446
fn visit_stmt(&mut self, stmt: &'hir Stmt<'hir>) {
447-
self.insert(stmt.span, stmt.hir_id, Node::Stmt(stmt));
447+
self.insert(DUMMY_SP, stmt.hir_id, Node::Stmt(stmt));
448448

449449
self.with_parent(stmt.hir_id, |this| {
450450
intravisit::walk_stmt(this, stmt);

src/librustc_middle/middle/region.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl Scope {
184184
// (This is the special case alluded to in the
185185
// doc-comment for this method)
186186

187-
let stmt_span = blk.stmts[first_statement_index.index()].span;
187+
let stmt_span = tcx.hir().span(blk.stmts[first_statement_index.index()].hir_id);
188188

189189
// To avoid issues with macro-generated spans, the span
190190
// of the statement must be nested in that of the block.

src/librustc_passes/check_attr.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,14 @@ impl CheckAttrVisitor<'tcx> {
357357
if let hir::StmtKind::Local(ref l) = stmt.kind {
358358
for attr in l.attrs.iter() {
359359
if attr.check_name(sym::inline) {
360-
self.check_inline(l.hir_id, attr, &stmt.span, Target::Statement);
360+
let span = self.tcx.hir().span(stmt.hir_id);
361+
self.check_inline(l.hir_id, attr, &span, Target::Statement);
361362
}
362363
if attr.check_name(sym::repr) {
364+
let span = self.tcx.hir().span(stmt.hir_id);
363365
self.emit_repr_error(
364366
attr.span,
365-
stmt.span,
367+
span,
366368
"attribute should not be applied to a statement",
367369
"not a struct, enum, or union",
368370
);

src/librustc_trait_selection/traits/error_reporting/suggestions.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
922922
// no return, suggest removal of semicolon on last statement.
923923
// Once that is added, close #54771.
924924
if let Some(ref stmt) = blk.stmts.last() {
925-
let sp = self.tcx.sess.source_map().end_point(stmt.span);
925+
let span = self.tcx.hir().span(stmt.hir_id);
926+
let sp = self.tcx.sess.source_map().end_point(span);
926927
err.span_label(sp, "consider removing this semicolon");
927928
}
928929
}

src/librustc_typeck/check/_match.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
300300
} else if let Some(stmt) = block.stmts.last() {
301301
// possibly incorrect trailing `;` in the else arm
302302
remove_semicolon = self.could_remove_semicolon(block, then_ty);
303-
stmt.span
303+
self.tcx.hir().span(stmt.hir_id)
304304
} else {
305305
// empty block; point at its entirety
306306
// Avoid overlapping spans that aren't as readable:
@@ -347,7 +347,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
347347
} else if let Some(stmt) = block.stmts.last() {
348348
// possibly incorrect trailing `;` in the else arm
349349
remove_semicolon = remove_semicolon.or(self.could_remove_semicolon(block, else_ty));
350-
stmt.span
350+
self.tcx.hir().span(stmt.hir_id)
351351
} else {
352352
// empty block; point at its entirety
353353
outer_sp = None; // same as in `error_sp`; cleanup output

src/librustc_typeck/check/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4618,7 +4618,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
46184618
hir::StmtKind::Local(..) | hir::StmtKind::Expr(..) | hir::StmtKind::Semi(..) => {}
46194619
}
46204620

4621-
self.warn_if_unreachable(stmt.hir_id, stmt.span, "statement");
4621+
let span = self.tcx.hir().span(stmt.hir_id);
4622+
self.warn_if_unreachable(stmt.hir_id, span, "statement");
46224623

46234624
// Hide the outer diverging and `has_errors` flags.
46244625
let old_diverges = self.diverges.replace(Diverges::Maybe);
@@ -5406,7 +5407,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
54065407
{
54075408
return None;
54085409
}
5409-
let original_span = original_sp(last_stmt.span, blk.span);
5410+
let last_stmt_span = self.tcx.hir().span(last_stmt.hir_id);
5411+
let original_span = original_sp(last_stmt_span, blk.span);
54105412
Some(original_span.with_lo(original_span.hi() - BytePos(1)))
54115413
}
54125414

src/tools/clippy/clippy_lints/src/blocks_in_if_conditions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlocksInIfConditions {
107107
);
108108
}
109109
} else {
110-
let span = block.expr.as_ref().map_or_else(|| block.stmts[0].span, |e| e.span);
110+
let span = block.expr.as_ref().map_or_else(|| cx.tcx.hir().span(block.stmts[0].hir_id), |e| e.span);
111111
if span.from_expansion() || differing_macro_contexts(expr.span, span) {
112112
return;
113113
}

src/tools/clippy/clippy_lints/src/let_if_seq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetIfSeq {
7171
if let Some(value) = check_assign(cx, canonical_id, &*then);
7272
if !used_in_expr(cx, canonical_id, value);
7373
then {
74-
let span = stmt.span.to(if_.span);
74+
let span = cx.tcx.hir().span(stmt.hir_id).to(if_.span);
7575

7676
let has_interior_mutability = !cx.tables.node_type(canonical_id).is_freeze(
7777
cx.tcx,

src/tools/clippy/clippy_lints/src/map_unit_fn.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ fn reduce_unit_expression<'a>(cx: &LateContext<'_, '_>, expr: &'a hir::Expr<'_>)
142142
match inner_stmt.kind {
143143
hir::StmtKind::Local(ref local) => Some(local.span),
144144
hir::StmtKind::Expr(ref e) => Some(e.span),
145-
hir::StmtKind::Semi(..) => Some(inner_stmt.span),
145+
hir::StmtKind::Semi(..) => Some(cx.tcx.hir().span(inner_stmt.hir_id)),
146146
hir::StmtKind::Item(..) => None,
147147
}
148148
},
@@ -214,6 +214,7 @@ fn lint_map_unit_fn(cx: &LateContext<'_, '_>, stmt: &hir::Stmt<'_>, expr: &hir::
214214
};
215215
let fn_arg = &map_args[1];
216216

217+
let stmt_span = cx.tcx.hir().span(stmt.hir_id);
217218
if is_unit_function(cx, fn_arg) {
218219
let msg = suggestion_msg("function", map_type);
219220
let suggestion = format!(
@@ -225,7 +226,7 @@ fn lint_map_unit_fn(cx: &LateContext<'_, '_>, stmt: &hir::Stmt<'_>, expr: &hir::
225226
);
226227

227228
span_lint_and_then(cx, lint, expr.span, &msg, |diag| {
228-
diag.span_suggestion(stmt.span, "try this", suggestion, Applicability::MachineApplicable);
229+
diag.span_suggestion(stmt_span, "try this", suggestion, Applicability::MachineApplicable);
229230
});
230231
} else if let Some((binding, closure_expr)) = unit_closure(cx, fn_arg) {
231232
let msg = suggestion_msg("closure", map_type);
@@ -240,7 +241,7 @@ fn lint_map_unit_fn(cx: &LateContext<'_, '_>, stmt: &hir::Stmt<'_>, expr: &hir::
240241
snippet(cx, reduced_expr_span, "_")
241242
);
242243
diag.span_suggestion(
243-
stmt.span,
244+
stmt_span,
244245
"try this",
245246
suggestion,
246247
Applicability::MachineApplicable, // snippet
@@ -252,15 +253,15 @@ fn lint_map_unit_fn(cx: &LateContext<'_, '_>, stmt: &hir::Stmt<'_>, expr: &hir::
252253
snippet(cx, binding.pat.span, "_"),
253254
snippet(cx, var_arg.span, "_"),
254255
);
255-
diag.span_suggestion(stmt.span, "try this", suggestion, Applicability::HasPlaceholders);
256+
diag.span_suggestion(stmt_span, "try this", suggestion, Applicability::HasPlaceholders);
256257
}
257258
});
258259
}
259260
}
260261

261262
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapUnit {
262263
fn check_stmt(&mut self, cx: &LateContext<'_, '_>, stmt: &hir::Stmt<'_>) {
263-
if stmt.span.from_expansion() {
264+
if cx.tcx.hir().span(stmt.hir_id).from_expansion() {
264265
return;
265266
}
266267

src/tools/clippy/clippy_lints/src/matches.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ fn check_wild_err_arm(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[Arm<'_>])
736736
if_chain! {
737737
if matching_wild;
738738
if let ExprKind::Block(ref block, _) = arm.body.kind;
739-
if is_panic_block(block);
739+
if is_panic_block(cx, block);
740740
then {
741741
// `Err(_)` or `Err(_e)` arm with `panic!` found
742742
span_lint_and_note(cx,
@@ -872,13 +872,14 @@ fn check_wild_enum_match(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[Arm<'_
872872
}
873873

874874
// If the block contains only a `panic!` macro (as expression or statement)
875-
fn is_panic_block(block: &Block<'_>) -> bool {
875+
fn is_panic_block(cx: &LateContext<'_, '_>, block: &Block<'_>) -> bool {
876876
match (&block.expr, block.stmts.len(), block.stmts.first()) {
877877
(&Some(ref exp), 0, _) => {
878878
is_expn_of(exp.span, "panic").is_some() && is_expn_of(exp.span, "unreachable").is_none()
879879
},
880880
(&None, 1, Some(stmt)) => {
881-
is_expn_of(stmt.span, "panic").is_some() && is_expn_of(stmt.span, "unreachable").is_none()
881+
let stmt_span = cx.tcx.hir().span(stmt.hir_id);
882+
is_expn_of(stmt_span, "panic").is_some() && is_expn_of(stmt_span, "unreachable").is_none()
882883
},
883884
_ => false,
884885
}

src/tools/clippy/clippy_lints/src/misc.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints {
317317
"`ref` on an entire `let` pattern is discouraged, take a reference with `&` instead",
318318
|diag| {
319319
diag.span_suggestion(
320-
stmt.span,
320+
cx.tcx.hir().span(stmt.hir_id),
321321
"try",
322322
format!(
323323
"let {name}{tyopt} = {initref};",
@@ -338,14 +338,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints {
338338
if binop.node == BinOpKind::And || binop.node == BinOpKind::Or;
339339
if let Some(sugg) = Sugg::hir_opt(cx, a);
340340
then {
341+
let stmt_span = cx.tcx.hir().span(stmt.hir_id);
341342
span_lint_and_then(cx,
342343
SHORT_CIRCUIT_STATEMENT,
343-
stmt.span,
344+
stmt_span,
344345
"boolean short circuit operator in statement may be clearer using an explicit test",
345346
|diag| {
346347
let sugg = if binop.node == BinOpKind::Or { !sugg } else { sugg };
347348
diag.span_suggestion(
348-
stmt.span,
349+
stmt_span,
349350
"replace it with",
350351
format!(
351352
"if {} {{ {}; }}",

src/tools/clippy/clippy_lints/src/no_effect.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NoEffect {
9191
fn check_stmt(&mut self, cx: &LateContext<'a, 'tcx>, stmt: &'tcx Stmt<'_>) {
9292
if let StmtKind::Semi(ref expr) = stmt.kind {
9393
if has_no_effect(cx, expr) {
94-
span_lint(cx, NO_EFFECT, stmt.span, "statement with no effect");
94+
span_lint(cx, NO_EFFECT, cx.tcx.hir().span(stmt.hir_id), "statement with no effect");
9595
} else if let Some(reduced) = reduce_expression(cx, expr) {
9696
let mut snippet = String::new();
9797
for e in reduced {
@@ -108,7 +108,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NoEffect {
108108
span_lint_and_sugg(
109109
cx,
110110
UNNECESSARY_OPERATION,
111-
stmt.span,
111+
cx.tcx.hir().span(stmt.hir_id),
112112
"statement can be reduced",
113113
"replace it with",
114114
snippet,

src/tools/clippy/clippy_lints/src/swap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ fn check_manual_swap(cx: &LateContext<'_, '_>, block: &Block<'_>) {
133133
(true, String::new(), String::new())
134134
};
135135

136-
let span = w[0].span.to(second.span);
136+
let span = cx.tcx.hir().span(w[0].hir_id).to(second.span);
137137

138138
span_lint_and_then(
139139
cx,

src/tools/clippy/clippy_lints/src/types.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetUnitValue {
603603
fn check_stmt(&mut self, cx: &LateContext<'a, 'tcx>, stmt: &'tcx Stmt<'_>) {
604604
if let StmtKind::Local(ref local) = stmt.kind {
605605
if is_unit(cx.tables.pat_ty(&local.pat)) {
606-
if in_external_macro(cx.sess(), stmt.span) || local.pat.span.from_expansion() {
606+
let stmt_span = cx.tcx.hir().span(stmt.hir_id);
607+
if in_external_macro(cx.sess(), stmt_span) || local.pat.span.from_expansion() {
607608
return;
608609
}
609610
if higher::is_from_for_desugar(local) {
@@ -612,13 +613,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetUnitValue {
612613
span_lint_and_then(
613614
cx,
614615
LET_UNIT_VALUE,
615-
stmt.span,
616+
stmt_span,
616617
"this let-binding has unit value",
617618
|diag| {
618619
if let Some(expr) = &local.init {
619620
let snip = snippet_with_macro_callsite(cx, expr.span, "()");
620621
diag.span_suggestion(
621-
stmt.span,
622+
stmt_span,
622623
"omit the `let` binding",
623624
format!("{};", snip),
624625
Applicability::MachineApplicable, // snippet
@@ -826,7 +827,7 @@ fn lint_unit_args(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args_to_recover: &[
826827
if let Some(snip) = snippet_opt(cx, last_expr.span);
827828
then {
828829
Some((
829-
last_stmt.span,
830+
cx.tcx.hir().span(last_stmt.hir_id),
830831
snip,
831832
))
832833
}

0 commit comments

Comments
 (0)