diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index c8dbf77704c9..3054f11c1769 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -445,7 +445,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { reg.register_early_lint_pass(box utils::internal_lints::ClippyLintsInternal); reg.register_late_lint_pass(box utils::internal_lints::CompilerLintFunctions::new()); reg.register_late_lint_pass(box utils::internal_lints::LintWithoutLintPass::default()); - reg.register_late_lint_pass(box utils::internal_lints::OuterExpnInfoPass); + reg.register_late_lint_pass(box utils::internal_lints::OuterExpnDataPass); reg.register_late_lint_pass(box utils::inspector::DeepCodeInspector); reg.register_late_lint_pass(box utils::author::Author); reg.register_late_lint_pass(box types::Types); @@ -681,7 +681,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { utils::internal_lints::CLIPPY_LINTS_INTERNAL, utils::internal_lints::COMPILER_LINT_FUNCTIONS, utils::internal_lints::LINT_WITHOUT_LINT_PASS, - utils::internal_lints::OUTER_EXPN_EXPN_INFO, + utils::internal_lints::OUTER_EXPN_EXPN_DATA, ]); reg.register_lint_group("clippy::all", Some("clippy"), vec![ diff --git a/clippy_lints/src/misc.rs b/clippy_lints/src/misc.rs index f8a24982934c..a127431e2d53 100644 --- a/clippy_lints/src/misc.rs +++ b/clippy_lints/src/misc.rs @@ -625,13 +625,17 @@ 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 { + if expr.span.from_expansion() { + let data = expr.span.ctxt().outer_expn_data(); + + if let ExpnKind::Macro(MacroKind::Attr, _) = data.kind { true } else { false } - }) + } else { + false + } } /// Tests whether `res` is a variable defined outside a macro. diff --git a/clippy_lints/src/panic_unimplemented.rs b/clippy_lints/src/panic_unimplemented.rs index 8327dbcc641b..617ac2393d51 100644 --- a/clippy_lints/src/panic_unimplemented.rs +++ b/clippy_lints/src/panic_unimplemented.rs @@ -69,8 +69,10 @@ 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(); + if expr.span.from_expansion(); + let first = expr.span.ctxt().outer_expn_data(); + if first.call_site.from_expansion(); + let second = first.call_site.ctxt().outer_expn_data(); then { second.call_site } else { diff --git a/clippy_lints/src/ranges.rs b/clippy_lints/src/ranges.rs index a0391e3364e1..a4ce129c95ea 100644 --- a/clippy_lints/src/ranges.rs +++ b/clippy_lints/src/ranges.rs @@ -147,10 +147,14 @@ 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 = if expr.span.from_expansion() { + expr.span + .ctxt() + .outer_expn_data() + .call_site + } else { + expr.span + }; span_lint_and_then( cx, RANGE_PLUS_ONE, diff --git a/clippy_lints/src/returns.rs b/clippy_lints/src/returns.rs index 0f2084e819e3..8a8133bfa1ea 100644 --- a/clippy_lints/src/returns.rs +++ b/clippy_lints/src/returns.rs @@ -317,7 +317,11 @@ fn attr_is_cfg(attr: &ast::Attribute) -> bool { // get the def site fn get_def(span: Span) -> Option { - span.ctxt().outer_expn_info().and_then(|info| Some(info.def_site)) + if span.from_expansion() { + Some(span.ctxt().outer_expn_data().def_site) + } else { + None + } } // is this expr a `()` unit? diff --git a/clippy_lints/src/utils/internal_lints.rs b/clippy_lints/src/utils/internal_lints.rs index 0dcac1121842..855b1e6eff3d 100644 --- a/clippy_lints/src/utils/internal_lints.rs +++ b/clippy_lints/src/utils/internal_lints.rs @@ -76,26 +76,26 @@ 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()` + /// **What it does:** Checks for calls to `cx.outer().expn_data()` and suggests to use + /// 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. /// /// **Example:** /// Bad: /// ```rust,ignore - /// expr.span.ctxt().outer().expn_info() + /// expr.span.ctxt().outer().expn_data() /// ``` /// /// Good: /// ```rust,ignore - /// expr.span.ctxt().outer_expn_info() + /// expr.span.ctxt().outer_expn_data() /// ``` - pub OUTER_EXPN_EXPN_INFO, + pub OUTER_EXPN_EXPN_DATA, internal, - "using `cx.outer_expn().expn_info()` instead of `cx.outer_expn_info()`" + "using `cx.outer_expn().expn_data()` instead of `cx.outer_expn_data()`" } declare_lint_pass!(ClippyLintsInternal => [CLIPPY_LINTS_INTERNAL]); @@ -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( @@ -278,17 +274,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CompilerLintFunctions { } } -pub struct OuterExpnInfoPass; +pub struct OuterExpnDataPass; -impl_lint_pass!(OuterExpnInfoPass => [OUTER_EXPN_EXPN_INFO]); +impl_lint_pass!(OuterExpnDataPass => [OUTER_EXPN_EXPN_DATA]); -impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OuterExpnInfoPass { +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OuterExpnDataPass { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) { let (method_names, arg_lists) = method_calls(expr, 2); let method_names: Vec = method_names.iter().map(|s| s.as_str()).collect(); let method_names: Vec<&str> = method_names.iter().map(std::convert::AsRef::as_ref).collect(); if_chain! { - if let ["expn_info", "outer_expn"] = method_names.as_slice(); + if let ["expn_data", "outer_expn"] = method_names.as_slice(); let args = arg_lists[1]; if args.len() == 1; let self_arg = &args[0]; @@ -297,11 +293,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OuterExpnInfoPass { then { span_lint_and_sugg( cx, - OUTER_EXPN_EXPN_INFO, + OUTER_EXPN_EXPN_DATA, expr.span.trim_start(self_arg.span).unwrap_or(expr.span), - "usage of `outer_expn().expn_info()`", + "usage of `outer_expn().expn_data()`", "try", - ".outer_expn_info()".to_string(), + ".outer_expn_data()".to_string(), Applicability::MachineApplicable, ); } diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index 9981799ed1d5..a25f25041272 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -92,15 +92,15 @@ pub fn in_constant(cx: &LateContext<'_, '_>, id: HirId) -> bool { } } -/// Returns `true` if this `expn_info` was expanded by any macro or desugaring +/// Returns `true` if this `span` 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. +/// Returns `true` if this `span` 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 { + if span.from_expansion() { + if let ExpnKind::Desugaring(..) = span.ctxt().outer_expn_data().kind { false } else { true @@ -686,12 +686,18 @@ 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 { loop { - let span_name_span = span.ctxt().outer_expn_info().map(|ei| (ei.kind.descr(), ei.call_site)); + if span.from_expansion() { + let data = span.ctxt().outer_expn_data(); + let mac_name = data.kind.descr(); + let new_span = 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); + } else { + span = new_span; + } + } else { + return None; } } } @@ -706,11 +712,18 @@ pub fn is_expn_of(mut span: Span, name: &str) -> Option { /// `bar!` by /// `is_direct_expn_of`. pub fn is_direct_expn_of(span: Span, name: &str) -> Option { - let span_name_span = span.ctxt().outer_expn_info().map(|ei| (ei.kind.descr(), ei.call_site)); + if span.from_expansion() { + let data = span.ctxt().outer_expn_data(); + let mac_name = data.kind.descr(); + let new_span = 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 { + Some(new_span) + } else { + None + } + } else { + None } } diff --git a/clippy_lints/src/vec.rs b/clippy_lints/src/vec.rs index c9a2eecd509c..d7709784a3eb 100644 --- a/clippy_lints/src/vec.rs +++ b/clippy_lints/src/vec.rs @@ -49,13 +49,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessVec { // report the error around the `vec!` not inside `:` 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); } } diff --git a/tests/ui/outer_expn_info.rs b/tests/ui/outer_expn_data.rs similarity index 87% rename from tests/ui/outer_expn_info.rs rename to tests/ui/outer_expn_data.rs index ed0542c9298b..15ac315a586f 100644 --- a/tests/ui/outer_expn_info.rs +++ b/tests/ui/outer_expn_data.rs @@ -16,7 +16,7 @@ declare_lint_pass!(Pass => [TEST_LINT]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { fn check_expr(&mut self, _cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { - let _ = expr.span.ctxt().outer_expn().expn_info(); + let _ = expr.span.ctxt().outer_expn().expn_data(); } } diff --git a/tests/ui/outer_expn_data.stderr b/tests/ui/outer_expn_data.stderr new file mode 100644 index 000000000000..cd12ad79dd12 --- /dev/null +++ b/tests/ui/outer_expn_data.stderr @@ -0,0 +1,15 @@ +error: usage of `outer_expn().expn_data()` + --> $DIR/outer_expn_data.rs:19:33 + | +LL | let _ = expr.span.ctxt().outer_expn().expn_data(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.outer_expn_data()` + | +note: lint level defined here + --> $DIR/outer_expn_data.rs:1:9 + | +LL | #![deny(clippy::internal)] + | ^^^^^^^^^^^^^^^^ + = note: `#[deny(clippy::outer_expn_expn_data)]` implied by `#[deny(clippy::internal)]` + +error: aborting due to previous error + diff --git a/tests/ui/outer_expn_info.stderr b/tests/ui/outer_expn_info.stderr deleted file mode 100644 index 4bbd1493dc4f..000000000000 --- a/tests/ui/outer_expn_info.stderr +++ /dev/null @@ -1,15 +0,0 @@ -error: usage of `outer_expn().expn_info()` - --> $DIR/outer_expn_info.rs:19:33 - | -LL | let _ = expr.span.ctxt().outer_expn().expn_info(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.outer_expn_info()` - | -note: lint level defined here - --> $DIR/outer_expn_info.rs:1:9 - | -LL | #![deny(clippy::internal)] - | ^^^^^^^^^^^^^^^^ - = note: `#[deny(clippy::outer_expn_expn_info)]` implied by `#[deny(clippy::internal)]` - -error: aborting due to previous error -