Skip to content

Use outer_expn_data instead of outer_expn_info #4398

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 16, 2019
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
4 changes: 2 additions & 2 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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![
Expand Down
10 changes: 7 additions & 3 deletions clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions clippy_lints/src/panic_unimplemented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 8 additions & 4 deletions clippy_lints/src/ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion clippy_lints/src/returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,11 @@ 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))
if span.from_expansion() {
Some(span.ctxt().outer_expn_data().def_site)
} else {
None
}
}

// is this expr a `()` unit?
Expand Down
34 changes: 15 additions & 19 deletions clippy_lints/src/utils/internal_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
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 @@ -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<LocalInternedString> = 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];
Expand All @@ -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,
);
}
Expand Down
41 changes: 27 additions & 14 deletions clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Span> {
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;
}
}
}
Expand All @@ -706,11 +712,18 @@ 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));
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
}
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/ui/outer_expn_data.stderr
Original file line number Diff line number Diff line change
@@ -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

15 changes: 0 additions & 15 deletions tests/ui/outer_expn_info.stderr

This file was deleted.