Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
tesuji committed Aug 16, 2019
1 parent 348d398 commit 1bd8714
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 58 deletions.
14 changes: 7 additions & 7 deletions clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,13 +625,13 @@ fn is_used(cx: &LateContext<'_, '_>, expr: &Expr) -> bool {
/// generated by `#[derive(...)]` or the like).
fn in_attributes_expansion(expr: &Expr) -> bool {
use syntax::ext::hygiene::MacroKind;
expr.span.ctxt().outer_expn_info().map_or(false, |info| {
if let ExpnKind::Macro(MacroKind::Attr, _) = info.kind {
true
} else {
false
}
})

let expn_data = expr.span.ctxt().outer_expn_data();
if let ExpnKind::Macro(MacroKind::Attr, _) = expn_data.kind {
true
} else {
false
}
}

/// Tests whether `res` is a variable defined outside a macro.
Expand Down
16 changes: 7 additions & 9 deletions clippy_lints/src/panic_unimplemented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PanicUnimplemented {
}

fn get_outer_span(expr: &Expr) -> Span {
if_chain! {
if let Some(first) = expr.span.ctxt().outer_expn_info();
if let Some(second) = first.call_site.ctxt().outer_expn_info();
then {
second.call_site
} else {
expr.span
}
}
expr.span
.ctxt()
.outer_expn_data()
.call_site
.ctxt()
.outer_expn_data()
.call_site
}

fn match_panic(params: &P<[Expr]>, expr: &Expr, cx: &LateContext<'_, '_>) {
Expand Down
5 changes: 1 addition & 4 deletions clippy_lints/src/ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Ranges {
}) = higher::range(cx, expr);
if let Some(y) = y_plus_one(end);
then {
let span = expr.span
.ctxt()
.outer_expn_info()
.map_or(expr.span, |info| info.call_site);
let span = expr.span.ctxt().outer_expn_data().call_site;
span_lint_and_then(
cx,
RANGE_PLUS_ONE,
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ fn attr_is_cfg(attr: &ast::Attribute) -> bool {
}

// get the def site
fn get_def(span: Span) -> Option<Span> {
span.ctxt().outer_expn_info().and_then(|info| Some(info.def_site))
fn get_def(span: Span) -> Span {
span.ctxt().outer_expn_data().def_site
}

// is this expr a `()` unit?
Expand Down
16 changes: 6 additions & 10 deletions clippy_lints/src/utils/internal_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ declare_clippy_lint! {

declare_clippy_lint! {
/// **What it does:** Checks for calls to `cx.outer().expn_info()` and suggests to use
/// the `cx.outer_expn_info()`
/// the `cx.outer_expn_data()`
///
/// **Why is this bad?** `cx.outer_expn_info()` is faster and more concise.
/// **Why is this bad?** `cx.outer_expn_data()` is faster and more concise.
///
/// **Known problems:** None.
///
Expand All @@ -91,11 +91,11 @@ declare_clippy_lint! {
///
/// Good:
/// ```rust,ignore
/// expr.span.ctxt().outer_expn_info()
/// expr.span.ctxt().outer_expn_data()
/// ```
pub OUTER_EXPN_EXPN_INFO,
internal,
"using `cx.outer_expn().expn_info()` instead of `cx.outer_expn_info()`"
"using `cx.outer_expn().expn_info()` instead of `cx.outer_expn_data()`"
}

declare_lint_pass!(ClippyLintsInternal => [CLIPPY_LINTS_INTERNAL]);
Expand Down Expand Up @@ -180,11 +180,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LintWithoutLintPass {
// not able to capture the error.
// Therefore, we need to climb the macro expansion tree and find the
// actual span that invoked `declare_tool_lint!`:
let lint_span = lint_span
.ctxt()
.outer_expn_info()
.map(|ei| ei.call_site)
.expect("unable to get call_site");
let lint_span = lint_span.ctxt().outer_expn_data().call_site;

if !self.registered_lints.contains(lint_name) {
span_lint(
Expand Down Expand Up @@ -301,7 +297,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OuterExpnInfoPass {
expr.span.trim_start(self_arg.span).unwrap_or(expr.span),
"usage of `outer_expn().expn_info()`",
"try",
".outer_expn_info()".to_string(),
".outer_expn_data()".to_string(),
Applicability::MachineApplicable,
);
}
Expand Down
36 changes: 19 additions & 17 deletions clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,19 @@ pub fn in_constant(cx: &LateContext<'_, '_>, id: HirId) -> bool {

/// Returns `true` if this `expn_info` was expanded by any macro or desugaring
pub fn in_macro_or_desugar(span: Span) -> bool {
span.ctxt().outer_expn_info().is_some()
span.from_expansion()
}

/// Returns `true` if this `expn_info` was expanded by any macro.
pub fn in_macro(span: Span) -> bool {
if let Some(info) = span.ctxt().outer_expn_info() {
if let ExpnKind::Desugaring(..) = info.kind {
false
} else {
true
}
} else {
let expn_data = span.ctxt().outer_expn_data();
if expn_data.is_root() {
return false;
}
if let ExpnKind::Desugaring(..) = expn_data.kind {
false
} else {
true
}
}
// If the snippet is empty, it's an attribute that was inserted during macro
Expand Down Expand Up @@ -686,13 +686,14 @@ pub fn is_adjusted(cx: &LateContext<'_, '_>, e: &Expr) -> bool {
/// 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| (ei.kind.descr(), ei.call_site));
let expn_data = span.ctxt().outer_expn_data();
let (mac_name, new_span) = (expn_data.kind.descr(), expn_data.call_site);

match span_name_span {
Some((mac_name, new_span)) if mac_name.as_str() == name => return Some(new_span),
None => return None,
Some((_, new_span)) => span = new_span,
if mac_name.as_str() == name {
return Some(new_span);
}

span = new_span;
}
}

Expand All @@ -706,12 +707,13 @@ 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| (ei.kind.descr(), ei.call_site));
let expn_data = span.ctxt().outer_expn_data();
let (mac_name, new_span) = (expn_data.kind.descr(), expn_data.call_site);

match span_name_span {
Some((mac_name, new_span)) if mac_name.as_str() == name => Some(new_span),
_ => None,
if mac_name.as_str() == name {
return Some(new_span);
}
None
}

/// Convenience function to get the return type of a function.
Expand Down
10 changes: 4 additions & 6 deletions clippy_lints/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessVec {
// 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")
.outer_expn_data()
.call_site
.ctxt()
.outer_expn_info()
.map(|info| info.call_site)
.expect("unable to get call_site");
.outer_expn_data()
.call_site;
check_vec_macro(cx, &vec_args, span);
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/outer_expn_info.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error: usage of `outer_expn().expn_info()`
--> $DIR/outer_expn_info.rs:19:33
--> $DIR.outer_expn_data.rs:19:33
|
LL | let _ = expr.span.ctxt().outer_expn().expn_info();
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.outer_expn_info()`
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.outer_expn_data()`
|
note: lint level defined here
--> $DIR/outer_expn_info.rs:1:9
--> $DIR.outer_expn_data.rs:1:9
|
LL | #![deny(clippy::internal)]
| ^^^^^^^^^^^^^^^^
Expand Down

0 comments on commit 1bd8714

Please sign in to comment.