Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Spans from HIR #72015

Closed
wants to merge 41 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
596450e
Collect spans during lowering.
cjgillot May 1, 2020
1b6618d
Introduce a query for HIR spans.
cjgillot May 1, 2020
166bb39
Don't pass spans in hir::map::blocks.
cjgillot May 2, 2020
46b2dc2
Stop passing the Span in HIR visiting.
cjgillot May 2, 2020
9c1039a
Pass HirId in save_analysis.
cjgillot Jun 7, 2020
7b5f1a3
Remove Span parameter from HIR collector.
cjgillot Jun 3, 2020
07e9c5c
Fix fulldeps tests.
cjgillot Dec 13, 2020
4cff471
Fix clippy.
cjgillot Dec 13, 2020
173048d
Remove span from hir::Param.
cjgillot May 1, 2020
56a6530
Remove span from hir::Variant.
cjgillot May 1, 2020
272a1f2
Remove span from hir::StructField.
cjgillot May 1, 2020
7b8595f
Remove span from hir::Stmt.
cjgillot Mar 6, 2021
a295ae3
Remove span from hir::Block.
cjgillot May 1, 2020
32716ae
Remove span from hir::MacroDef.
cjgillot May 2, 2020
6ec2356
Remove span from hir::GenericParam.
cjgillot May 2, 2020
4f527e9
Remove span from hir::Arm.
cjgillot May 2, 2020
1fcff64
Remove span from hir::FieldPat.
cjgillot May 2, 2020
3cf2fba
Remove span from hir::Local.
cjgillot May 2, 2020
f51da1e
Remove span from hir::Pat.
cjgillot Mar 6, 2021
ec56b9f
Remove span from hir::Field.
cjgillot May 2, 2020
ec5cd74
Remove GenericArg::span.
cjgillot May 3, 2020
66e1a16
Remove Span from hir::TypeBinding.
cjgillot May 8, 2020
a29261d
Remove Span from hir::ConstArg.
cjgillot Jun 1, 2020
56871a5
Remove Span from hir::TraitItemRef.
cjgillot Jun 1, 2020
346176f
Remove Span from hir::ImplItemRef.
cjgillot Jun 1, 2020
6459750
Remove span from hir::ForeignItemRef.
cjgillot Dec 11, 2020
fcdfaf0
Fix fulldeps tests.
cjgillot May 9, 2020
e6d9d4b
Fortify find_entry.
cjgillot Dec 13, 2020
125796b
Remove Span from hir::CrateItem.
cjgillot Dec 10, 2020
7e26fd6
Pass HirId in rustc_typeck::check.
cjgillot May 2, 2020
bdb83e2
Pass HirId in rustc_lint.
cjgillot May 2, 2020
05e913a
Pass HirId in rustc_passes::stability.
cjgillot May 2, 2020
2035593
Pass HirId in rustc_incremental::persist::dirty_clean.
cjgillot Dec 11, 2020
cf99aea
Pass HirId in rustc_passes::check_attr.
cjgillot Dec 11, 2020
85396f8
Pass HirId in rustc_passes::entry.
cjgillot Dec 11, 2020
042bf08
Remove span from hir::Item.
cjgillot Dec 22, 2020
d632f42
Remove span from hir::ForeignItem.
cjgillot Dec 11, 2020
0d9d1e0
Remove Span from hir::Ty.
cjgillot May 8, 2020
e281f67
Remove Span from hir::Expr.
cjgillot May 3, 2020
f065656
Fix fulldeps tests.
cjgillot May 9, 2020
bb40d08
Fix suggestions.
cjgillot Mar 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove span from hir::Stmt.
cjgillot committed Mar 13, 2021
commit 7b8595fad289e0f57e819085bcb0c8694517ed8f
14 changes: 4 additions & 10 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
@@ -2483,13 +2483,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
.collect();
let hir_id = self.lower_node_id(s.id, s.span);
self.alias_attrs(hir_id, l.hir_id);
ids.push({
hir::Stmt {
hir_id,
kind: hir::StmtKind::Local(self.arena.alloc(l)),
span: s.span,
}
});
ids.push(hir::Stmt { hir_id, kind: hir::StmtKind::Local(self.arena.alloc(l)) });
return ids;
}
StmtKind::Item(ref it) => {
@@ -2504,7 +2498,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
.map(|id| self.lower_node_id(id, s.span))
.unwrap_or_else(|| self.next_id(s.span));

hir::Stmt { hir_id, kind: hir::StmtKind::Item(item_id), span: s.span }
hir::Stmt { hir_id, kind: hir::StmtKind::Item(item_id) }
})
.collect();
}
@@ -2523,7 +2517,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
StmtKind::Empty => return smallvec![],
StmtKind::MacCall(..) => panic!("shouldn't exist here"),
};
smallvec![hir::Stmt { hir_id, kind, span: s.span }]
smallvec![hir::Stmt { hir_id, kind }]
}

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

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

fn stmt_expr(&mut self, span: Span, expr: hir::Expr<'hir>) -> hir::Stmt<'hir> {
1 change: 0 additions & 1 deletion compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
@@ -1151,7 +1151,6 @@ impl UnOp {
pub struct Stmt<'hir> {
pub hir_id: HirId,
pub kind: StmtKind<'hir>,
pub span: Span,
}

/// The contents of a statement.
5 changes: 3 additions & 2 deletions compiler/rustc_hir_pretty/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1041,7 +1041,8 @@ impl<'a> State<'a> {
}

pub fn print_stmt(&mut self, st: &hir::Stmt<'_>) {
self.maybe_print_comment(st.span.lo());
let span = self.span(st.hir_id);
self.maybe_print_comment(span.lo());
match st.kind {
hir::StmtKind::Local(ref loc) => {
self.print_local(loc.init.as_deref(), |this| this.print_local_decl(&loc));
@@ -1060,7 +1061,7 @@ impl<'a> State<'a> {
if stmt_ends_with_semi(&st.kind) {
self.s.word(";");
}
self.maybe_print_trailing_comment(st.span, None)
self.maybe_print_trailing_comment(span, None)
}

pub fn print_block(&mut self, blk: &hir::Block<'_>) {
14 changes: 8 additions & 6 deletions compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
@@ -101,8 +101,9 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
return;
}

let span = cx.tcx.hir().span(s.hir_id);
let ty = cx.typeck_results().expr_ty(&expr);
let type_permits_lack_of_use = check_must_use_ty(cx, ty, &expr, s.span, "", "", 1);
let type_permits_lack_of_use = check_must_use_ty(cx, ty, &expr, span, "", "", 1);

let mut fn_warned = false;
let mut op_warned = false;
@@ -124,7 +125,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
_ => None,
};
if let Some(def_id) = maybe_def_id {
fn_warned = check_must_use_def(cx, def_id, s.span, "return value of ", "");
fn_warned = check_must_use_def(cx, def_id, span, "return value of ", "");
} else if type_permits_lack_of_use {
// We don't warn about unused unit or uninhabited types.
// (See https://github.com/rust-lang/rust/issues/43806 for details.)
@@ -166,7 +167,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
}

if !(type_permits_lack_of_use || fn_warned || op_warned) {
cx.struct_span_lint(UNUSED_RESULTS, s.span, |lint| lint.build("unused result").emit());
cx.struct_span_lint(UNUSED_RESULTS, span, |lint| lint.build("unused result").emit());
}

// Returns whether an error has been emitted (and thus another does not need to be later).
@@ -349,19 +350,20 @@ impl<'tcx> LateLintPass<'tcx> for PathStatements {
fn check_stmt(&mut self, cx: &LateContext<'_>, s: &hir::Stmt<'_>) {
if let hir::StmtKind::Semi(expr) = s.kind {
if let hir::ExprKind::Path(_) = expr.kind {
cx.struct_span_lint(PATH_STATEMENTS, s.span, |lint| {
let span = cx.tcx.hir().span(s.hir_id);
cx.struct_span_lint(PATH_STATEMENTS, span, |lint| {
let ty = cx.typeck_results().expr_ty(expr);
if ty.needs_drop(cx.tcx, cx.param_env) {
let mut lint = lint.build("path statement drops value");
if let Ok(snippet) = cx.sess().source_map().span_to_snippet(expr.span) {
lint.span_suggestion(
s.span,
span,
"use `drop` to clarify the intent",
format!("drop({});", snippet),
Applicability::MachineApplicable,
);
} else {
lint.span_help(s.span, "use `drop` to clarify the intent");
lint.span_help(span, "use `drop` to clarify the intent");
}
lint.emit()
} else {
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/middle/region.rs
Original file line number Diff line number Diff line change
@@ -184,7 +184,7 @@ impl Scope {
// (This is the special case alluded to in the
// doc-comment for this method)

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

// To avoid issues with macro-generated spans, the span
// of the statement must be nested in that of the block.
3 changes: 2 additions & 1 deletion compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
@@ -1260,7 +1260,8 @@ impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt<'tcx>) {
// When checking statements ignore expressions, they will be checked later.
if let hir::StmtKind::Local(ref l) = stmt.kind {
self.check_attributes(l.hir_id, &stmt.span, Target::Statement, None);
let stmt_span = self.tcx.hir().span(stmt.hir_id);
self.check_attributes(l.hir_id, &stmt_span, Target::Statement, None);
}
intravisit::walk_stmt(self, stmt)
}
4 changes: 2 additions & 2 deletions compiler/rustc_passes/src/naked_functions.rs
Original file line number Diff line number Diff line change
@@ -310,10 +310,10 @@ impl<'tcx> Visitor<'tcx> for CheckInlineAssembly<'tcx> {
match stmt.kind {
StmtKind::Item(..) => {}
StmtKind::Local(..) => {
self.items.push((ItemKind::NonAsm, stmt.span));
self.items.push((ItemKind::NonAsm, self.tcx.hir().span(stmt.hir_id)));
}
StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => {
self.check_expr(expr, stmt.span);
self.check_expr(expr, self.tcx.hir().span(stmt.hir_id));
}
}
}
Original file line number Diff line number Diff line change
@@ -920,7 +920,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
// Once that is added, close #54771.
if let Some(ref stmt) = blk.stmts.last() {
if let hir::StmtKind::Semi(_) = stmt.kind {
let sp = self.tcx.sess.source_map().end_point(stmt.span);
let stmt_span = self.tcx.hir().span(stmt.hir_id);
let sp = self.tcx.sess.source_map().end_point(stmt_span);
err.span_label(sp, "consider removing this semicolon");
}
}
3 changes: 2 additions & 1 deletion compiler/rustc_typeck/src/check/_match.rs
Original file line number Diff line number Diff line change
@@ -577,8 +577,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if let Some(expr) = &block.expr {
(expr.span, None)
} else if let Some(stmt) = block.stmts.last() {
let span = self.tcx.hir().span(stmt.hir_id);
// possibly incorrect trailing `;` in the else arm
(stmt.span, expected_ty.and_then(|ty| self.could_remove_semicolon(block, ty)))
(span, expected_ty.and_then(|ty| self.could_remove_semicolon(block, ty)))
} else {
// empty block; point at its entirety
(block.span, None)
3 changes: 2 additions & 1 deletion compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
@@ -1136,7 +1136,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
{
return None;
}
let original_span = original_sp(last_stmt.span, blk.span);
let last_stmt_span = self.tcx.hir().span(last_stmt.hir_id);
let original_span = original_sp(last_stmt_span, blk.span);
Some((original_span.with_lo(original_span.hi() - BytePos(1)), needs_box))
}

5 changes: 3 additions & 2 deletions compiler/rustc_typeck/src/check/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
@@ -547,7 +547,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
hir::StmtKind::Local(..) | hir::StmtKind::Expr(..) | hir::StmtKind::Semi(..) => {}
}

self.warn_if_unreachable(stmt.hir_id, stmt.span, "statement");
let stmt_span = self.tcx.hir().span(stmt.hir_id);
self.warn_if_unreachable(stmt.hir_id, stmt_span, "statement");

// Hide the outer diverging and `has_errors` flags.
let old_diverges = self.diverges.replace(Diverges::Maybe);
@@ -572,7 +573,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// in order to capture the fact that this `match` is the last statement in its
// function. This is done for better suggestions to remove the `;`.
let expectation = match expr.kind {
hir::ExprKind::Match(..) if is_last => IsLast(stmt.span),
hir::ExprKind::Match(..) if is_last => IsLast(self.tcx.hir().span(stmt.hir_id)),
_ => NoExpectation,
};
self.check_expr_with_expectation(expr, expectation);
Original file line number Diff line number Diff line change
@@ -122,7 +122,7 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInIfConditions {
);
}
} else {
let span = block.expr.as_ref().map_or_else(|| block.stmts[0].span, |e| e.span);
let span = block.expr.as_ref().map_or_else(|| cx.tcx.hir().span(block.stmts[0].hir_id), |e| e.span);
if span.from_expansion() || differing_macro_contexts(expr.span, span) {
return;
}
3 changes: 2 additions & 1 deletion src/tools/clippy/clippy_lints/src/default.rs
Original file line number Diff line number Diff line change
@@ -209,10 +209,11 @@ impl LateLintPass<'_> for Default {
};

// span lint once per statement that binds default
let first_assign_span = cx.tcx.hir().span(first_assign.unwrap().hir_id);
span_lint_and_note(
cx,
FIELD_REASSIGN_WITH_DEFAULT,
first_assign.unwrap().span,
first_assign_span,
"field assignment outside of initializer for an instance created with Default::default()",
Some(local.span),
&format!(
3 changes: 2 additions & 1 deletion src/tools/clippy/clippy_lints/src/eq_op.rs
Original file line number Diff line number Diff line change
@@ -69,7 +69,8 @@ impl<'tcx> LateLintPass<'tcx> for EqOp {
for stmt in block.stmts {
for amn in &ASSERT_MACRO_NAMES {
if_chain! {
if is_expn_of(stmt.span, amn).is_some();
let stmt_span = cx.tcx.hir().span(stmt.hir_id);
if is_expn_of(stmt_span, amn).is_some();
if let StmtKind::Semi(ref matchexpr) = stmt.kind;
if let Some(macro_args) = higher::extract_assert_macro_args(matchexpr);
if macro_args.len() == 2;
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_lints/src/let_if_seq.rs
Original file line number Diff line number Diff line change
@@ -69,7 +69,7 @@ impl<'tcx> LateLintPass<'tcx> for LetIfSeq {
if let Some(value) = check_assign(cx, canonical_id, &*then);
if !used_visitor.check_expr(value);
then {
let span = stmt.span.to(if_.span);
let span = cx.tcx.hir().span(stmt.hir_id).to(if_.span);

let has_interior_mutability = !cx.typeck_results().node_type(canonical_id).is_freeze(
cx.tcx.at(span),
5 changes: 3 additions & 2 deletions src/tools/clippy/clippy_lints/src/loops/needless_collect.rs
Original file line number Diff line number Diff line change
@@ -116,17 +116,18 @@ fn check_needless_collect_indirect_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCo

// Suggest replacing iter_call with iter_replacement, and removing stmt
let iter_call = &iter_calls[0];
let stmt_span = cx.tcx.hir().span(stmt.hir_id);
span_lint_and_then(
cx,
super::NEEDLESS_COLLECT,
stmt.span.until(iter_call.span),
stmt_span.until(iter_call.span),
NEEDLESS_COLLECT_MSG,
|diag| {
let iter_replacement = format!("{}{}", Sugg::hir(cx, iter_source, ".."), iter_call.get_iter_method(cx));
diag.multipart_suggestion(
iter_call.get_suggestion_text(),
vec![
(stmt.span, String::new()),
(stmt_span, String::new()),
(iter_call.span, iter_replacement)
],
Applicability::MachineApplicable,// MaybeIncorrect,
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ pub(super) fn check<'tcx>(
for_span,
"for loop over a single element",
"try",
format!("{{\n{}let {} = &{};{}}}", " ".repeat(indent_of(cx, block.stmts[0].span).unwrap_or(0)), target.name, list_item_name, block_str),
format!("{{\n{}let {} = &{};{}}}", " ".repeat(indent_of(cx, cx.tcx.hir().span(block.stmts[0].hir_id)).unwrap_or(0)), target.name, list_item_name, block_str),
Applicability::MachineApplicable
)
}
5 changes: 3 additions & 2 deletions src/tools/clippy/clippy_lints/src/macro_use.rs
Original file line number Diff line number Diff line change
@@ -138,8 +138,9 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports {
}
}
fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &hir::Stmt<'_>) {
if in_macro(stmt.span) {
self.push_unique_macro(cx, stmt.span);
let stmt_span = cx.tcx.hir().span(stmt.hir_id);
if in_macro(stmt_span) {
self.push_unique_macro(cx, stmt_span);
}
}
fn check_pat(&mut self, cx: &LateContext<'_>, pat: &hir::Pat<'_>) {
11 changes: 6 additions & 5 deletions src/tools/clippy/clippy_lints/src/map_unit_fn.rs
Original file line number Diff line number Diff line change
@@ -143,7 +143,7 @@ fn reduce_unit_expression<'a>(cx: &LateContext<'_>, expr: &'a hir::Expr<'_>) ->
match inner_stmt.kind {
hir::StmtKind::Local(ref local) => Some(local.span),
hir::StmtKind::Expr(ref e) => Some(e.span),
hir::StmtKind::Semi(..) => Some(inner_stmt.span),
hir::StmtKind::Semi(..) => Some(cx.tcx.hir().span(inner_stmt.hir_id)),
hir::StmtKind::Item(..) => None,
}
},
@@ -216,6 +216,7 @@ fn lint_map_unit_fn(cx: &LateContext<'_>, stmt: &hir::Stmt<'_>, expr: &hir::Expr
};
let fn_arg = &map_args[1];

let stmt_span = cx.tcx.hir().span(stmt.hir_id);
if is_unit_function(cx, fn_arg) {
let msg = suggestion_msg("function", map_type);
let suggestion = format!(
@@ -227,7 +228,7 @@ fn lint_map_unit_fn(cx: &LateContext<'_>, stmt: &hir::Stmt<'_>, expr: &hir::Expr
);

span_lint_and_then(cx, lint, expr.span, &msg, |diag| {
diag.span_suggestion(stmt.span, "try this", suggestion, Applicability::MachineApplicable);
diag.span_suggestion(stmt_span, "try this", suggestion, Applicability::MachineApplicable);
});
} else if let Some((binding, closure_expr)) = unit_closure(cx, fn_arg) {
let msg = suggestion_msg("closure", map_type);
@@ -242,7 +243,7 @@ fn lint_map_unit_fn(cx: &LateContext<'_>, stmt: &hir::Stmt<'_>, expr: &hir::Expr
snippet(cx, reduced_expr_span, "_")
);
diag.span_suggestion(
stmt.span,
stmt_span,
"try this",
suggestion,
Applicability::MachineApplicable, // snippet
@@ -254,15 +255,15 @@ fn lint_map_unit_fn(cx: &LateContext<'_>, stmt: &hir::Stmt<'_>, expr: &hir::Expr
snippet(cx, binding.pat.span, "_"),
snippet(cx, var_arg.span, "_"),
);
diag.span_suggestion(stmt.span, "try this", suggestion, Applicability::HasPlaceholders);
diag.span_suggestion(stmt_span, "try this", suggestion, Applicability::HasPlaceholders);
}
});
}
}

impl<'tcx> LateLintPass<'tcx> for MapUnit {
fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &hir::Stmt<'_>) {
if stmt.span.from_expansion() {
if cx.tcx.hir().span(stmt.hir_id).from_expansion() {
return;
}

7 changes: 4 additions & 3 deletions src/tools/clippy/clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
@@ -936,7 +936,7 @@ fn check_wild_err_arm<'tcx>(cx: &LateContext<'tcx>, ex: &Expr<'tcx>, arms: &[Arm
if_chain! {
if matching_wild;
if let ExprKind::Block(ref block, _) = arm.body.kind;
if is_panic_block(block);
if is_panic_block(cx, block);
then {
// `Err(_)` or `Err(_e)` arm with `panic!` found
span_lint_and_note(cx,
@@ -1065,13 +1065,14 @@ fn check_wild_enum_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>])
}

// If the block contains only a `panic!` macro (as expression or statement)
fn is_panic_block(block: &Block<'_>) -> bool {
fn is_panic_block(cx: &LateContext<'_>, block: &Block<'_>) -> bool {
match (&block.expr, block.stmts.len(), block.stmts.first()) {
(&Some(ref exp), 0, _) => {
is_expn_of(exp.span, "panic").is_some() && is_expn_of(exp.span, "unreachable").is_none()
},
(&None, 1, Some(stmt)) => {
is_expn_of(stmt.span, "panic").is_some() && is_expn_of(stmt.span, "unreachable").is_none()
let stmt_span = cx.tcx.hir().span(stmt.hir_id);
is_expn_of(stmt_span, "panic").is_some() && is_expn_of(stmt_span, "unreachable").is_none()
},
_ => false,
}
10 changes: 6 additions & 4 deletions src/tools/clippy/clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
@@ -301,7 +301,8 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {

fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
if_chain! {
if !in_external_macro(cx.tcx.sess, stmt.span);
let stmt_span = cx.tcx.hir().span(stmt.hir_id);
if !in_external_macro(cx.tcx.sess, stmt_span);
if let StmtKind::Local(ref local) = stmt.kind;
if let PatKind::Binding(an, .., name, None) = local.pat.kind;
if let Some(ref init) = local.init;
@@ -333,7 +334,7 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
"`ref` on an entire `let` pattern is discouraged, take a reference with `&` instead",
|diag| {
diag.span_suggestion(
stmt.span,
cx.tcx.hir().span(stmt.hir_id),
"try",
format!(
"let {name}{tyopt} = {initref};",
@@ -354,14 +355,15 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
if binop.node == BinOpKind::And || binop.node == BinOpKind::Or;
if let Some(sugg) = Sugg::hir_opt(cx, a);
then {
let stmt_span = cx.tcx.hir().span(stmt.hir_id);
span_lint_and_then(cx,
SHORT_CIRCUIT_STATEMENT,
stmt.span,
stmt_span,
"boolean short circuit operator in statement may be clearer using an explicit test",
|diag| {
let sugg = if binop.node == BinOpKind::Or { !sugg } else { sugg };
diag.span_suggestion(
stmt.span,
stmt_span,
"replace it with",
format!(
"if {} {{ {}; }}",
4 changes: 2 additions & 2 deletions src/tools/clippy/clippy_lints/src/no_effect.rs
Original file line number Diff line number Diff line change
@@ -92,7 +92,7 @@ impl<'tcx> LateLintPass<'tcx> for NoEffect {
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
if let StmtKind::Semi(ref expr) = stmt.kind {
if has_no_effect(cx, expr) {
span_lint(cx, NO_EFFECT, stmt.span, "statement with no effect");
span_lint(cx, NO_EFFECT, cx.tcx.hir().span(stmt.hir_id), "statement with no effect");
} else if let Some(reduced) = reduce_expression(cx, expr) {
let mut snippet = String::new();
for e in reduced {
@@ -109,7 +109,7 @@ impl<'tcx> LateLintPass<'tcx> for NoEffect {
span_lint_and_sugg(
cx,
UNNECESSARY_OPERATION,
stmt.span,
cx.tcx.hir().span(stmt.hir_id),
"statement can be reduced",
"replace it with",
snippet,
3 changes: 2 additions & 1 deletion src/tools/clippy/clippy_lints/src/returns.rs
Original file line number Diff line number Diff line change
@@ -159,7 +159,8 @@ fn check_block_return<'tcx>(cx: &LateContext<'tcx>, block: &Block<'tcx>) {
} else if let Some(stmt) = block.stmts.iter().last() {
match stmt.kind {
StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => {
check_final_expr(cx, expr, Some(stmt.span), RetReplacement::Empty);
let stmt_span = cx.tcx.hir().span(stmt.hir_id);
check_final_expr(cx, expr, Some(stmt_span), RetReplacement::Empty);
},
_ => (),
}
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_lints/src/swap.rs
Original file line number Diff line number Diff line change
@@ -133,7 +133,7 @@ fn check_manual_swap(cx: &LateContext<'_>, block: &Block<'_>) {
(true, String::new(), String::new())
};

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

span_lint_and_then(
cx,
9 changes: 5 additions & 4 deletions src/tools/clippy/clippy_lints/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -415,7 +415,8 @@ impl<'tcx> LateLintPass<'tcx> for LetUnitValue {
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
if let StmtKind::Local(ref local) = stmt.kind {
if is_unit(cx.typeck_results().pat_ty(&local.pat)) {
if in_external_macro(cx.sess(), stmt.span) || local.pat.span.from_expansion() {
let stmt_span = cx.tcx.hir().span(stmt.hir_id);
if in_external_macro(cx.sess(), stmt_span) || local.pat.span.from_expansion() {
return;
}
if higher::is_from_for_desugar(local) {
@@ -424,13 +425,13 @@ impl<'tcx> LateLintPass<'tcx> for LetUnitValue {
span_lint_and_then(
cx,
LET_UNIT_VALUE,
stmt.span,
stmt_span,
"this let-binding has unit value",
|diag| {
if let Some(expr) = &local.init {
let snip = snippet_with_macro_callsite(cx, expr.span, "()");
diag.span_suggestion(
stmt.span,
stmt_span,
"omit the `let` binding",
format!("{};", snip),
Applicability::MachineApplicable, // snippet
@@ -676,7 +677,7 @@ fn lint_unit_args(cx: &LateContext<'_>, expr: &Expr<'_>, args_to_recover: &[&Exp
if let Some(snip) = snippet_opt(cx, last_expr.span);
then {
Some((
last_stmt.span,
cx.tcx.hir().span(last_stmt.hir_id),
snip,
))
}
Original file line number Diff line number Diff line change
@@ -122,7 +122,7 @@ fn check_arg<'tcx>(cx: &LateContext<'tcx>, arg: &'tcx Expr<'tcx>) -> Option<(Spa
if let Some(stmt) = block.stmts.last();
if let StmtKind::Semi(_) = stmt.kind;
then {
let data = stmt.span.data();
let data = cx.tcx.hir().span(stmt.hir_id).data();
// Make a span out of the semicolon for the help message
Some((span, Some(Span::new(data.hi-BytePos(1), data.hi, data.ctxt))))
} else {
3 changes: 2 additions & 1 deletion src/tools/clippy/clippy_lints/src/vec_init_then_push.rs
Original file line number Diff line number Diff line change
@@ -134,10 +134,11 @@ impl LateLintPass<'_> for VecInitThenPush {
if let ExprKind::MethodCall(path, _, [self_arg, _], _) = expr.kind;
if path_to_local_id(self_arg, searcher.local_id);
if path.ident.name.as_str() == "push";
let stmt_span = cx.tcx.hir().span(stmt.hir_id);
then {
self.searcher = Some(VecPushSearcher {
found: searcher.found + 1,
err_span: searcher.err_span.to(stmt.span),
err_span: searcher.err_span.to(stmt_span),
.. searcher
});
} else {