mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 07:00:55 +00:00
Use outer_expn_data instead of outer_expn_info
This commit is contained in:
parent
348d398b1c
commit
f74d9db7f4
11 changed files with 89 additions and 65 deletions
|
@ -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![
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
let span = if expr.span.from_expansion() {
|
||||
expr.span
|
||||
.ctxt()
|
||||
.outer_expn_info()
|
||||
.map_or(expr.span, |info| info.call_site);
|
||||
.outer_expn_data()
|
||||
.call_site
|
||||
} else {
|
||||
expr.span
|
||||
};
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
RANGE_PLUS_ONE,
|
||||
|
|
|
@ -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?
|
||||
|
|
|
@ -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]);
|
||||
|
@ -182,9 +182,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LintWithoutLintPass {
|
|||
// 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");
|
||||
.outer_expn_data()
|
||||
.call_site;
|
||||
|
||||
if !self.registered_lints.contains(lint_name) {
|
||||
span_lint(
|
||||
|
@ -278,17 +277,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];
|
||||
|
@ -297,11 +296,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,
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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<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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
15
tests/ui/outer_expn_data.stderr
Normal file
15
tests/ui/outer_expn_data.stderr
Normal file
|
@ -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
|
||||
|
|
@ -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
|
||||
|
Loading…
Reference in a new issue