Skip to content

Accessing Span internals is deprecated #2006

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

Merged
merged 2 commits into from
Aug 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion clippy_lints/src/collapsible_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn check_collapsible_no_if_let(cx: &EarlyContext, expr: &ast::Expr, check: &ast:
let Some(inner) = expr_block(then),
let ast::ExprKind::If(ref check_inner, ref content, None) = inner.node,
], {
if expr.span.ctxt != inner.span.ctxt {
if expr.span.ctxt() != inner.span.ctxt() {
return;
}
span_lint_and_then(cx, COLLAPSIBLE_IF, expr.span, "this if statement can be collapsed", |db| {
Expand Down
25 changes: 8 additions & 17 deletions clippy_lints/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,7 @@ pub fn strip_doc_comment_decoration(comment: &str, span: Span) -> (String, Vec<(
vec![
(
doc.len(),
Span {
lo: span.lo + BytePos(prefix.len() as u32),
..span
}
span.with_lo(span.lo() + BytePos(prefix.len() as u32)),
),
],
);
Expand All @@ -117,10 +114,7 @@ pub fn strip_doc_comment_decoration(comment: &str, span: Span) -> (String, Vec<(
// +1 for the newline
sizes.push((
line.len() + 1,
Span {
lo: span.lo + BytePos(offset as u32),
..span
},
span.with_lo(span.lo() + BytePos(offset as u32)),
));
}
if !contains_initial_stars {
Expand Down Expand Up @@ -228,10 +222,7 @@ fn check_doc<'a, Events: Iterator<Item = (usize, pulldown_cmark::Event<'a>)>>(
let (begin, span) = spans[index];

// Adjust for the begining of the current `Event`
let span = Span {
lo: span.lo + BytePos::from_usize(offset - begin),
..span
};
let span = span.with_lo(span.lo() + BytePos::from_usize(offset - begin));

check_text(cx, valid_idents, &text, span);
}
Expand All @@ -253,11 +244,11 @@ fn check_text(cx: &EarlyContext, valid_idents: &[String], text: &str, span: Span

// Adjust for the current word
let offset = word.as_ptr() as usize - text.as_ptr() as usize;
let span = Span {
lo: span.lo + BytePos::from_usize(offset),
hi: span.lo + BytePos::from_usize(offset + word.len()),
..span
};
let span = Span::new(
span.lo() + BytePos::from_usize(offset),
span.lo() + BytePos::from_usize(offset + word.len()),
span.ctxt(),
);

check_word(cx, word, span);
}
Expand Down
37 changes: 6 additions & 31 deletions clippy_lints/src/formatting.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use rustc::lint::*;
use syntax::ast;
use syntax_pos::{Span, NO_EXPANSION};
use utils::{differing_macro_contexts, in_macro, snippet_opt, span_note_and_lint};
use syntax::ptr::P;

Expand Down Expand Up @@ -106,19 +105,11 @@ impl EarlyLintPass for Formatting {
fn check_assign(cx: &EarlyContext, expr: &ast::Expr) {
if let ast::ExprKind::Assign(ref lhs, ref rhs) = expr.node {
if !differing_macro_contexts(lhs.span, rhs.span) && !in_macro(lhs.span) {
let eq_span = Span {
lo: lhs.span.hi,
hi: rhs.span.lo,
ctxt: NO_EXPANSION,
};
let eq_span = lhs.span.between(rhs.span);
if let ast::ExprKind::Unary(op, ref sub_rhs) = rhs.node {
if let Some(eq_snippet) = snippet_opt(cx, eq_span) {
let op = ast::UnOp::to_string(op);
let eqop_span = Span {
lo: lhs.span.hi,
hi: sub_rhs.span.lo,
ctxt: NO_EXPANSION,
};
let eqop_span = lhs.span.between(sub_rhs.span);
if eq_snippet.ends_with('=') {
span_note_and_lint(
cx,
Expand Down Expand Up @@ -146,11 +137,7 @@ fn check_else_if(cx: &EarlyContext, expr: &ast::Expr) {
// this will be a span from the closing ‘}’ of the “then” block (excluding) to
// the
// “if” of the “else if” block (excluding)
let else_span = Span {
lo: then.span.hi,
hi: else_.span.lo,
ctxt: NO_EXPANSION,
};
let else_span = then.span.between(else_.span);

// the snippet should look like " else \n " with maybe comments anywhere
// it’s bad when there is a ‘\n’ after the “else”
Expand Down Expand Up @@ -181,17 +168,9 @@ fn check_array(cx: &EarlyContext, expr: &ast::Expr) {
for element in array {
if let ast::ExprKind::Binary(ref op, ref lhs, _) = element.node {
if !differing_macro_contexts(lhs.span, op.span) {
let space_span = Span {
lo: lhs.span.hi,
hi: op.span.lo,
ctxt: NO_EXPANSION,
};
let space_span = lhs.span.between(op.span);
if let Some(space_snippet) = snippet_opt(cx, space_span) {
let lint_span = Span {
lo: lhs.span.hi,
hi: lhs.span.hi,
ctxt: NO_EXPANSION,
};
let lint_span = lhs.span.with_lo(lhs.span.hi());
if space_snippet.contains('\n') {
span_note_and_lint(
cx,
Expand All @@ -215,11 +194,7 @@ fn check_consecutive_ifs(cx: &EarlyContext, first: &ast::Expr, second: &ast::Exp
unsugar_if(second).is_some()
{
// where the else would be
let else_span = Span {
lo: first.span.hi,
hi: second.span.lo,
ctxt: NO_EXPANSION,
};
let else_span = first.span.between(second.span);

if let Some(else_snippet) = snippet_opt(cx, else_span) {
if !else_snippet.contains('\n') {
Expand Down
7 changes: 1 addition & 6 deletions clippy_lints/src/if_let_redundant_pattern_matching.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use rustc::lint::*;
use rustc::hir::*;
use syntax::codemap::Span;
use utils::{paths, span_lint_and_then, match_qpath, snippet};

/// **What it does:*** Lint for redundant pattern matching over `Result` or
Expand Down Expand Up @@ -74,11 +73,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
arms[0].pats[0].span,
&format!("redundant pattern matching, consider using `{}`", good_method),
|db| {
let span = Span {
lo: expr.span.lo,
hi: op.span.hi,
ctxt: expr.span.ctxt,
};
let span = expr.span.with_hi(op.span.hi());
db.span_suggestion(span, "try this", format!("if {}.{}", snippet(cx, op.span, "_"), good_method));
});
}
Expand Down
3 changes: 1 addition & 2 deletions clippy_lints/src/let_if_seq.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use rustc::lint::*;
use rustc::hir;
use rustc::hir::BindingAnnotation;
use syntax_pos::{Span, NO_EXPANSION};
use utils::{snippet, span_lint_and_then};

/// **What it does:** Checks for variable declarations immediately followed by a
Expand Down Expand Up @@ -74,7 +73,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetIfSeq {
let Some(value) = check_assign(cx, def_id, &*then),
!used_in_expr(cx, def_id, value),
], {
let span = Span { lo: stmt.span.lo, hi: if_.span.hi, ctxt: NO_EXPANSION };
let span = stmt.span.to(if_.span);

let (default_multi_stmts, default) = if let Some(ref else_) = *else_ {
if let hir::ExprBlock(ref else_) = else_.node {
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ fn lint_map_unwrap_or(cx: &LateContext, expr: &hir::Expr, map_args: &[hir::Expr]
// lint, with note if neither arg is > 1 line and both map() and
// unwrap_or() have the same span
let multiline = map_snippet.lines().count() > 1 || unwrap_snippet.lines().count() > 1;
let same_span = map_args[1].span.ctxt == unwrap_args[1].span.ctxt;
let same_span = map_args[1].span.ctxt() == unwrap_args[1].span.ctxt();
if same_span && !multiline {
span_note_and_lint(
cx,
Expand Down Expand Up @@ -1125,7 +1125,7 @@ fn lint_map_unwrap_or_else(cx: &LateContext, expr: &hir::Expr, map_args: &[hir::
// lint, with note if neither arg is > 1 line and both map() and
// unwrap_or_else() have the same span
let multiline = map_snippet.lines().count() > 1 || unwrap_snippet.lines().count() > 1;
let same_span = map_args[1].span.ctxt == unwrap_args[1].span.ctxt;
let same_span = map_args[1].span.ctxt() == unwrap_args[1].span.ctxt();
if same_span && !multiline {
span_note_and_lint(
cx,
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ fn is_used(cx: &LateContext, expr: &Expr) -> bool {
/// generated by
/// `#[derive(...)`] or the like).
fn in_attributes_expansion(expr: &Expr) -> bool {
expr.span.ctxt.outer().expn_info().map_or(
expr.span.ctxt().outer().expn_info().map_or(
false,
|info| matches!(info.callee.format, ExpnFormat::MacroAttribute(_)),
)
Expand Down
10 changes: 5 additions & 5 deletions clippy_lints/src/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ fn str_span(base: Span, s: &str, c: usize) -> Span {

match (si.next(), si.next()) {
(Some((l, _)), Some((h, _))) => {
Span {
lo: base.lo + BytePos(l as u32),
hi: base.lo + BytePos(h as u32),
..base
}
Span::new(
base.lo() + BytePos(l as u32),
base.lo() + BytePos(h as u32),
base.ctxt(),
)
},
_ => base,
}
Expand Down
5 changes: 2 additions & 3 deletions clippy_lints/src/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use rustc::lint::*;
use rustc::ty;
use utils::{differing_macro_contexts, match_type, paths, snippet, span_lint_and_then, walk_ptrs_ty, SpanlessEq};
use utils::sugg::Sugg;
use syntax_pos::{Span, NO_EXPANSION};

/// **What it does:** Checks for manual swapping.
///
Expand Down Expand Up @@ -122,7 +121,7 @@ fn check_manual_swap(cx: &LateContext, block: &Block) {
(true, "".to_owned(), "".to_owned())
};

let span = Span { lo: w[0].span.lo, hi: second.span.hi, ctxt: NO_EXPANSION};
let span = w[0].span.to(second.span);

span_lint_and_then(cx,
MANUAL_SWAP,
Expand Down Expand Up @@ -161,7 +160,7 @@ fn check_suspicious_swap(cx: &LateContext, block: &Block) {
("".to_owned(), "".to_owned(), "".to_owned())
};

let span = Span{ lo: first.span.lo, hi: second.span.hi, ctxt: NO_EXPANSION};
let span = first.span.to(second.span);

span_lint_and_then(cx,
ALMOST_SWAPPED,
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/utils/internal_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LintWithoutLintPass {
// Therefore, we need to climb the macro expansion tree and find the
// actual span that invoked `declare_lint!`:
let lint_span = lint_span
.ctxt
.ctxt()
.outer()
.expn_info()
.map(|ei| ei.call_site)
Expand Down
10 changes: 5 additions & 5 deletions clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub mod higher;
/// from a macro and one
/// isn't).
pub fn differing_macro_contexts(lhs: Span, rhs: Span) -> bool {
rhs.ctxt != lhs.ctxt
rhs.ctxt() != lhs.ctxt()
}

pub fn in_constant(cx: &LateContext, id: NodeId) -> bool {
Expand All @@ -114,7 +114,7 @@ pub fn in_constant(cx: &LateContext, id: NodeId) -> bool {

/// Returns true if this `expn_info` was expanded by any macro.
pub fn in_macro(span: Span) -> bool {
span.ctxt.outer().expn_info().map_or(false, |info| {
span.ctxt().outer().expn_info().map_or(false, |info| {
match info.callee.format {// don't treat range expressions desugared to structs as "in_macro"
ExpnFormat::CompilerDesugaring(kind) => kind != CompilerDesugaringKind::DotFill,
_ => true,
Expand Down Expand Up @@ -147,7 +147,7 @@ pub fn in_external_macro<'a, T: LintContext<'a>>(cx: &T, span: Span) -> bool {
})
}

span.ctxt.outer().expn_info().map_or(false, |info| {
span.ctxt().outer().expn_info().map_or(false, |info| {
in_macro_ext(cx, &info)
})
}
Expand Down Expand Up @@ -740,7 +740,7 @@ fn parse_attrs<F: FnMut(u64)>(sess: &Session, attrs: &[ast::Attribute], name: &'
/// See also `is_direct_expn_of`.
pub fn is_expn_of(mut span: Span, name: &str) -> Option<Span> {
loop {
let span_name_span = span.ctxt.outer().expn_info().map(|ei| {
let span_name_span = span.ctxt().outer().expn_info().map(|ei| {
(ei.callee.name(), ei.call_site)
});

Expand All @@ -762,7 +762,7 @@ pub fn is_expn_of(mut span: Span, name: &str) -> Option<Span> {
/// `bar!` by
/// `is_direct_expn_of`.
pub fn is_direct_expn_of(span: Span, name: &str) -> Option<Span> {
let span_name_span = span.ctxt.outer().expn_info().map(|ei| {
let span_name_span = span.ctxt().outer().expn_info().map(|ei| {
(ei.callee.name(), ei.call_site)
});

Expand Down
12 changes: 3 additions & 9 deletions clippy_lints/src/utils/sugg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ fn astbinop2assignop(op: ast::BinOp) -> AssocOp {
/// Return the indentation before `span` if there are nothing but `[ \t]`
/// before it on its line.
fn indentation<'a, T: LintContext<'a>>(cx: &T, span: Span) -> Option<String> {
let lo = cx.sess().codemap().lookup_char_pos(span.lo);
let lo = cx.sess().codemap().lookup_char_pos(span.lo());
if let Some(line) = lo.file.get_line(
lo.line - 1, /* line numbers in `Loc` are 1-based */
)
Expand Down Expand Up @@ -443,21 +443,15 @@ pub trait DiagnosticBuilderExt<'a, T: LintContext<'a>> {
impl<'a, 'b, 'c, T: LintContext<'c>> DiagnosticBuilderExt<'c, T> for rustc_errors::DiagnosticBuilder<'b> {
fn suggest_item_with_attr<D: Display + ?Sized>(&mut self, cx: &T, item: Span, msg: &str, attr: &D) {
if let Some(indent) = indentation(cx, item) {
let span = Span {
hi: item.lo,
..item
};
let span = item.with_hi(item.lo());

self.span_suggestion(span, msg, format!("{}\n{}", attr, indent));
}
}

fn suggest_prepend_item(&mut self, cx: &T, item: Span, msg: &str, new_item: &str) {
if let Some(indent) = indentation(cx, item) {
let span = Span {
hi: item.lo,
..item
};
let span = item.with_hi(item.lo());

let mut first = true;
let new_item = new_item
Expand Down
8 changes: 2 additions & 6 deletions clippy_lints/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
is_copy(cx, vec_type(cx.tables.expr_ty_adjusted(arg))),
], {
// report the error around the `vec!` not inside `<std macros>:`
let span = arg.span.ctxt.outer().expn_info().map(|info| info.call_site).expect("unable to get call_site");
let span = arg.span.ctxt().outer().expn_info().map(|info| info.call_site).expect("unable to get call_site");
check_vec_macro(cx, &vec_args, span);
}}
}
Expand All @@ -74,11 +74,7 @@ fn check_vec_macro(cx: &LateContext, vec_args: &higher::VecArgs, span: Span) {
},
higher::VecArgs::Vec(args) => {
if let Some(last) = args.iter().last() {
let span = Span {
lo: args[0].span.lo,
hi: last.span.hi,
ctxt: args[0].span.ctxt,
};
let span = args[0].span.to(last.span);

format!("&[{}]", snippet(cx, span, "..")).into()
} else {
Expand Down