Merge pull request #2952 from dwijnand/upstream-in_external_macro

Switch to rustc's in_external_macro function
This commit is contained in:
Manish Goregaokar 2018-07-24 08:27:38 -07:00 committed by GitHub
commit b2caf669a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 35 additions and 65 deletions

View file

@ -4,7 +4,7 @@ use rustc::lint::*;
use rustc::{declare_lint, lint_array};
use syntax::ast::*;
use crate::utils::{in_external_macro, span_lint_and_sugg};
use crate::utils::span_lint_and_sugg;
/// **What it does:** Checks for usage of if expressions with an `else if` branch,
/// but without a final `else` branch.
@ -50,7 +50,7 @@ impl LintPass for ElseIfWithoutElse {
impl EarlyLintPass for ElseIfWithoutElse {
fn check_expr(&mut self, cx: &EarlyContext<'_>, mut item: &Expr) {
if in_external_macro(cx, item.span) {
if in_external_macro(cx.sess(), item.span) {
return;
}

View file

@ -5,7 +5,7 @@ use rustc::lint::*;
use rustc::{declare_lint, lint_array};
use syntax::ast::*;
use crate::utils::{in_external_macro, span_help_and_lint};
use crate::utils::span_help_and_lint;
/// **What it does:** Checks for usage of `!` or `!=` in an if condition with an
/// else branch.
@ -48,7 +48,7 @@ impl LintPass for IfNotElse {
impl EarlyLintPass for IfNotElse {
fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
if in_external_macro(cx, item.span) {
if in_external_macro(cx.sess(), item.span) {
return;
}
if let ExprKind::If(ref cond, _, Some(ref els)) = item.node {

View file

@ -7,7 +7,7 @@ use rustc::hir::*;
use rustc::hir::intravisit::*;
use std::collections::{HashMap, HashSet};
use syntax::codemap::Span;
use crate::utils::{in_external_macro, last_path_segment, span_lint};
use crate::utils::{last_path_segment, span_lint};
use syntax::symbol::keywords;
/// **What it does:** Checks for lifetime annotations which can be removed by
@ -98,7 +98,7 @@ fn check_fn_inner<'a, 'tcx>(
generics: &'tcx Generics,
span: Span,
) {
if in_external_macro(cx, span) || has_where_lifetimes(cx, &generics.where_clause) {
if in_external_macro(cx.sess(), span) || has_where_lifetimes(cx, &generics.where_clause) {
return;
}

View file

@ -6,7 +6,7 @@ use rustc::{declare_lint, lint_array};
use if_chain::if_chain;
use syntax::ast::*;
use syntax_pos;
use crate::utils::{in_external_macro, snippet_opt, span_lint_and_sugg};
use crate::utils::{snippet_opt, span_lint_and_sugg};
/// **What it does:** Warns if a long integral or floating-point constant does
/// not contain underscores.
@ -282,7 +282,7 @@ impl LintPass for LiteralDigitGrouping {
impl EarlyLintPass for LiteralDigitGrouping {
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
if in_external_macro(cx, expr.span) {
if in_external_macro(cx.sess(), expr.span) {
return;
}
@ -422,7 +422,7 @@ impl LintPass for LiteralRepresentation {
impl EarlyLintPass for LiteralRepresentation {
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
if in_external_macro(cx, expr.span) {
if in_external_macro(cx.sess(), expr.span) {
return;
}

View file

@ -23,7 +23,7 @@ use crate::utils::{sugg, sext};
use crate::utils::usage::mutated_variables;
use crate::consts::{constant, Constant};
use crate::utils::{get_enclosing_block, get_parent_expr, higher, in_external_macro, is_integer_literal, is_refutable,
use crate::utils::{get_enclosing_block, get_parent_expr, higher, is_integer_literal, is_refutable,
last_path_segment, match_trait_method, match_type, match_var, multispan_sugg, snippet, snippet_opt,
span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then, SpanlessEq};
use crate::utils::paths;
@ -450,7 +450,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
&& arms[1].pats.len() == 1 && arms[1].guard.is_none()
&& is_simple_break_expr(&arms[1].body)
{
if in_external_macro(cx, expr.span) {
if in_external_macro(cx.sess(), expr.span) {
return;
}

View file

@ -8,7 +8,7 @@ use std::collections::Bound;
use syntax::ast::LitKind;
use syntax::codemap::Span;
use crate::utils::paths;
use crate::utils::{expr_block, in_external_macro, is_allowed, is_expn_of, match_qpath, match_type, multispan_sugg,
use crate::utils::{expr_block, is_allowed, is_expn_of, match_qpath, match_type, multispan_sugg,
remove_blocks, snippet, span_lint_and_sugg, span_lint_and_then, span_note_and_lint, walk_ptrs_ty};
use crate::utils::sugg::Sugg;
use crate::consts::{constant, Constant};
@ -183,7 +183,7 @@ impl LintPass for MatchPass {
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MatchPass {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
if in_external_macro(cx, expr.span) {
if in_external_macro(cx.sess(), expr.span) {
return;
}
if let ExprKind::Match(ref ex, ref arms, MatchSource::Normal) = expr.node {

View file

@ -10,7 +10,7 @@ use std::fmt;
use std::iter;
use syntax::ast;
use syntax::codemap::{Span, BytePos};
use crate::utils::{get_arg_name, get_trait_def_id, implements_trait, in_external_macro, in_macro, is_copy, is_expn_of, is_self,
use crate::utils::{get_arg_name, get_trait_def_id, implements_trait, in_macro, is_copy, is_expn_of, is_self,
is_self_ty, iter_input_pats, last_path_segment, match_def_path, match_path, match_qpath, match_trait_method,
match_type, method_chain_args, match_var, return_ty, remove_blocks, same_tys, single_segment_path, snippet,
span_lint, span_lint_and_sugg, span_lint_and_then, span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth, SpanlessEq};
@ -806,7 +806,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
}
fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, implitem: &'tcx hir::ImplItem) {
if in_external_macro(cx, implitem.span) {
if in_external_macro(cx.sess(), implitem.span) {
return;
}
let name = implitem.ident.name;

View file

@ -6,7 +6,7 @@ use std::char;
use syntax::ast::*;
use syntax::codemap::Span;
use syntax::visit::FnKind;
use crate::utils::{constants, in_external_macro, snippet, snippet_opt, span_help_and_lint, span_lint, span_lint_and_then};
use crate::utils::{constants, snippet, snippet_opt, span_help_and_lint, span_lint, span_lint_and_then};
/// **What it does:** Checks for structure field patterns bound to wildcards.
///
@ -294,7 +294,7 @@ impl EarlyLintPass for MiscEarly {
}
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
if in_external_macro(cx, expr.span) {
if in_external_macro(cx.sess(), expr.span) {
return;
}
match expr.node {

View file

@ -3,7 +3,7 @@ use rustc::hir::intravisit;
use rustc::lint::*;
use rustc::{declare_lint, lint_array};
use rustc::ty;
use crate::utils::{higher, in_external_macro, span_lint};
use crate::utils::{higher, span_lint};
/// **What it does:** Checks for instances of `mut mut` references.
///
@ -50,7 +50,7 @@ pub struct MutVisitor<'a, 'tcx: 'a> {
impl<'a, 'tcx> intravisit::Visitor<'tcx> for MutVisitor<'a, 'tcx> {
fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
if in_external_macro(self.cx, expr.span) {
if in_external_macro(self.cx.sess(), expr.span) {
return;
}

View file

@ -3,7 +3,7 @@ use rustc::lint::*;
use rustc::{declare_lint, lint_array};
use if_chain::if_chain;
use crate::utils::{self, paths, span_lint, in_external_macro};
use crate::utils::{self, paths, span_lint};
/// **What it does:**
/// Checks for the usage of negated comparision operators on types which only implement
@ -55,7 +55,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NoNegCompOpForPartialOrd {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
if_chain! {
if !in_external_macro(cx, expr.span);
if !in_external_macro(cx.sess(), expr.span);
if let ExprKind::Unary(UnOp::UnNot, ref inner) = expr.node;
if let ExprKind::Binary(ref op, ref left, _) = inner.node;
if let BinOpKind::Le | BinOpKind::Ge | BinOpKind::Lt | BinOpKind::Gt = op.node;

View file

@ -6,7 +6,7 @@ use if_chain::if_chain;
use rustc::ty::{self, Ty};
use syntax::codemap::Span;
use crate::utils::paths;
use crate::utils::{get_trait_def_id, implements_trait, in_external_macro, return_ty, same_tys, span_lint_and_then};
use crate::utils::{get_trait_def_id, implements_trait, return_ty, same_tys, span_lint_and_then};
use crate::utils::sugg::DiagnosticBuilderExt;
/// **What it does:** Checks for types with a `fn new() -> Self` method and no
@ -95,7 +95,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
for assoc_item in items {
if let hir::AssociatedItemKind::Method { has_self: false } = assoc_item.kind {
let impl_item = cx.tcx.hir.impl_item(assoc_item.id);
if in_external_macro(cx, impl_item.span) {
if in_external_macro(cx.sess(), impl_item.span) {
return;
}
if let hir::ImplItemKind::Method(ref sig, _) = impl_item.node {

View file

@ -5,7 +5,7 @@ use syntax::ast;
use syntax::codemap::Span;
use syntax::visit::FnKind;
use crate::utils::{in_external_macro, in_macro, match_path_ast, snippet_opt, span_lint_and_then, span_note_and_lint};
use crate::utils::{in_macro, match_path_ast, snippet_opt, span_lint_and_then, span_note_and_lint};
/// **What it does:** Checks for return statements at the end of a block.
///
@ -90,7 +90,7 @@ impl ReturnPass {
}
fn emit_return_lint(&mut self, cx: &EarlyContext<'_>, ret_span: Span, inner_span: Span) {
if in_external_macro(cx, inner_span) || in_macro(inner_span) {
if in_external_macro(cx.sess(), inner_span) || in_macro(inner_span) {
return;
}
span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded return statement", |db| {
@ -117,7 +117,7 @@ impl ReturnPass {
if let ast::PatKind::Ident(_, ident, _) = local.pat.node;
if let ast::ExprKind::Path(_, ref path) = retexpr.node;
if match_path_ast(path, &[&ident.as_str()]);
if !in_external_macro(cx, initexpr.span);
if !in_external_macro(cx.sess(), initexpr.span);
then {
span_note_and_lint(cx,
LET_AND_RETURN,

View file

@ -5,7 +5,7 @@ use rustc::hir::*;
use rustc::hir::intravisit::FnKind;
use rustc::ty;
use syntax::codemap::Span;
use crate::utils::{contains_name, higher, in_external_macro, iter_input_pats, snippet, span_lint_and_then};
use crate::utils::{contains_name, higher, iter_input_pats, snippet, span_lint_and_then};
/// **What it does:** Checks for bindings that shadow other bindings already in
/// scope, while just changing reference level or mutability.
@ -90,7 +90,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
_: Span,
_: NodeId,
) {
if in_external_macro(cx, body.value.span) {
if in_external_macro(cx.sess(), body.value.span) {
return;
}
check_fn(cx, decl, body);
@ -122,7 +122,7 @@ fn check_block<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, block: &'tcx Block, binding
}
fn check_decl<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, decl: &'tcx Decl, bindings: &mut Vec<(Name, Span)>) {
if in_external_macro(cx, decl.span) {
if in_external_macro(cx.sess(), decl.span) {
return;
}
if higher::is_from_for_desugar(decl) {
@ -303,7 +303,7 @@ fn lint_shadow<'a, 'tcx: 'a>(
}
fn check_expr<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, bindings: &mut Vec<(Name, Span)>) {
if in_external_macro(cx, expr.span) {
if in_external_macro(cx.sess(), expr.span) {
return;
}
match expr.node {

View file

@ -14,7 +14,7 @@ use std::borrow::Cow;
use syntax::ast::{FloatTy, IntTy, UintTy};
use syntax::codemap::Span;
use syntax::errors::DiagnosticBuilder;
use crate::utils::{comparisons, differing_macro_contexts, higher, in_constant, in_external_macro, in_macro, last_path_segment, match_def_path, match_path,
use crate::utils::{comparisons, differing_macro_contexts, higher, in_constant, in_macro, last_path_segment, match_def_path, match_path,
match_type, multispan_sugg, opt_def_id, same_tys, snippet, snippet_opt, span_help_and_lint, span_lint,
span_lint_and_sugg, span_lint_and_then, clip, unsext, sext, int_bits};
use crate::utils::paths;
@ -381,7 +381,7 @@ declare_clippy_lint! {
fn check_let_unit(cx: &LateContext<'_, '_>, decl: &Decl) {
if let DeclKind::Local(ref local) = decl.node {
if is_unit(cx.tables.pat_ty(&local.pat)) {
if in_external_macro(cx, decl.span) || in_macro(local.pat.span) {
if in_external_macro(cx.sess(), decl.span) || in_macro(local.pat.span) {
return;
}
if higher::is_from_for_desugar(decl) {
@ -959,7 +959,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CastPass {
use syntax::ast::{LitIntType, LitKind};
match lit.node {
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::FloatUnsuffixed(_) => {},
_ => if cast_from.sty == cast_to.sty && !in_external_macro(cx, expr.span) {
_ => if cast_from.sty == cast_to.sty && !in_external_macro(cx.sess(), expr.span) {
span_lint(
cx,
UNNECESSARY_CAST,
@ -969,7 +969,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CastPass {
},
}
}
if cast_from.is_numeric() && cast_to.is_numeric() && !in_external_macro(cx, expr.span) {
if cast_from.is_numeric() && cast_to.is_numeric() && !in_external_macro(cx.sess(), expr.span) {
match (cast_from.is_integral(), cast_to.is_integral()) {
(true, false) => {
let from_nbits = int_ty_to_nbits(cast_from, cx.tcx);

View file

@ -19,7 +19,7 @@ use std::str::FromStr;
use std::rc::Rc;
use syntax::ast::{self, LitKind};
use syntax::attr;
use syntax::codemap::{CompilerDesugaringKind, ExpnFormat, ExpnInfo, Span, DUMMY_SP};
use syntax::codemap::{CompilerDesugaringKind, ExpnFormat, Span, DUMMY_SP};
use syntax::errors::DiagnosticBuilder;
use syntax::ptr::P;
use syntax::symbol::keywords;
@ -77,36 +77,6 @@ pub fn is_range_expression(span: Span) -> bool {
})
}
/// Returns true if the macro that expanded the crate was outside of the
/// current crate or was a
/// compiler plugin.
pub fn in_external_macro<'a, T: LintContext<'a>>(cx: &T, span: Span) -> bool {
/// Invokes `in_macro` with the expansion info of the given span slightly
/// heavy, try to use
/// this after other checks have already happened.
fn in_macro_ext<'a, T: LintContext<'a>>(cx: &T, info: &ExpnInfo) -> bool {
// no ExpnInfo = no macro
if let ExpnFormat::MacroAttribute(..) = info.format {
// these are all plugins
return true;
}
// no span for the callee = external macro
info.def_site.map_or(true, |span| {
// no snippet = external macro or compiler-builtin expansion
cx.sess()
.codemap()
.span_to_snippet(span)
.ok()
.map_or(true, |code| !code.starts_with("macro_rules"))
})
}
span.ctxt()
.outer()
.expn_info()
.map_or(false, |info| in_macro_ext(cx, &info))
}
/// Check if a `DefId`'s path matches the given absolute type path usage.
///
/// # Examples