Skip to content

Commit b2caf66

Browse files
authored
Merge pull request #2952 from dwijnand/upstream-in_external_macro
Switch to rustc's in_external_macro function
2 parents b02e53d + a1cce2d commit b2caf66

15 files changed

+35
-65
lines changed

clippy_lints/src/else_if_without_else.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc::lint::*;
44
use rustc::{declare_lint, lint_array};
55
use syntax::ast::*;
66

7-
use crate::utils::{in_external_macro, span_lint_and_sugg};
7+
use crate::utils::span_lint_and_sugg;
88

99
/// **What it does:** Checks for usage of if expressions with an `else if` branch,
1010
/// but without a final `else` branch.
@@ -50,7 +50,7 @@ impl LintPass for ElseIfWithoutElse {
5050

5151
impl EarlyLintPass for ElseIfWithoutElse {
5252
fn check_expr(&mut self, cx: &EarlyContext<'_>, mut item: &Expr) {
53-
if in_external_macro(cx, item.span) {
53+
if in_external_macro(cx.sess(), item.span) {
5454
return;
5555
}
5656

clippy_lints/src/if_not_else.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc::lint::*;
55
use rustc::{declare_lint, lint_array};
66
use syntax::ast::*;
77

8-
use crate::utils::{in_external_macro, span_help_and_lint};
8+
use crate::utils::span_help_and_lint;
99

1010
/// **What it does:** Checks for usage of `!` or `!=` in an if condition with an
1111
/// else branch.
@@ -48,7 +48,7 @@ impl LintPass for IfNotElse {
4848

4949
impl EarlyLintPass for IfNotElse {
5050
fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
51-
if in_external_macro(cx, item.span) {
51+
if in_external_macro(cx.sess(), item.span) {
5252
return;
5353
}
5454
if let ExprKind::If(ref cond, _, Some(ref els)) = item.node {

clippy_lints/src/lifetimes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc::hir::*;
77
use rustc::hir::intravisit::*;
88
use std::collections::{HashMap, HashSet};
99
use syntax::codemap::Span;
10-
use crate::utils::{in_external_macro, last_path_segment, span_lint};
10+
use crate::utils::{last_path_segment, span_lint};
1111
use syntax::symbol::keywords;
1212

1313
/// **What it does:** Checks for lifetime annotations which can be removed by
@@ -98,7 +98,7 @@ fn check_fn_inner<'a, 'tcx>(
9898
generics: &'tcx Generics,
9999
span: Span,
100100
) {
101-
if in_external_macro(cx, span) || has_where_lifetimes(cx, &generics.where_clause) {
101+
if in_external_macro(cx.sess(), span) || has_where_lifetimes(cx, &generics.where_clause) {
102102
return;
103103
}
104104

clippy_lints/src/literal_representation.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc::{declare_lint, lint_array};
66
use if_chain::if_chain;
77
use syntax::ast::*;
88
use syntax_pos;
9-
use crate::utils::{in_external_macro, snippet_opt, span_lint_and_sugg};
9+
use crate::utils::{snippet_opt, span_lint_and_sugg};
1010

1111
/// **What it does:** Warns if a long integral or floating-point constant does
1212
/// not contain underscores.
@@ -282,7 +282,7 @@ impl LintPass for LiteralDigitGrouping {
282282

283283
impl EarlyLintPass for LiteralDigitGrouping {
284284
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
285-
if in_external_macro(cx, expr.span) {
285+
if in_external_macro(cx.sess(), expr.span) {
286286
return;
287287
}
288288

@@ -422,7 +422,7 @@ impl LintPass for LiteralRepresentation {
422422

423423
impl EarlyLintPass for LiteralRepresentation {
424424
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
425-
if in_external_macro(cx, expr.span) {
425+
if in_external_macro(cx.sess(), expr.span) {
426426
return;
427427
}
428428

clippy_lints/src/loops.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::utils::{sugg, sext};
2323
use crate::utils::usage::mutated_variables;
2424
use crate::consts::{constant, Constant};
2525

26-
use crate::utils::{get_enclosing_block, get_parent_expr, higher, in_external_macro, is_integer_literal, is_refutable,
26+
use crate::utils::{get_enclosing_block, get_parent_expr, higher, is_integer_literal, is_refutable,
2727
last_path_segment, match_trait_method, match_type, match_var, multispan_sugg, snippet, snippet_opt,
2828
span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then, SpanlessEq};
2929
use crate::utils::paths;
@@ -450,7 +450,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
450450
&& arms[1].pats.len() == 1 && arms[1].guard.is_none()
451451
&& is_simple_break_expr(&arms[1].body)
452452
{
453-
if in_external_macro(cx, expr.span) {
453+
if in_external_macro(cx.sess(), expr.span) {
454454
return;
455455
}
456456

clippy_lints/src/matches.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::collections::Bound;
88
use syntax::ast::LitKind;
99
use syntax::codemap::Span;
1010
use crate::utils::paths;
11-
use crate::utils::{expr_block, in_external_macro, is_allowed, is_expn_of, match_qpath, match_type, multispan_sugg,
11+
use crate::utils::{expr_block, is_allowed, is_expn_of, match_qpath, match_type, multispan_sugg,
1212
remove_blocks, snippet, span_lint_and_sugg, span_lint_and_then, span_note_and_lint, walk_ptrs_ty};
1313
use crate::utils::sugg::Sugg;
1414
use crate::consts::{constant, Constant};
@@ -183,7 +183,7 @@ impl LintPass for MatchPass {
183183

184184
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MatchPass {
185185
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
186-
if in_external_macro(cx, expr.span) {
186+
if in_external_macro(cx.sess(), expr.span) {
187187
return;
188188
}
189189
if let ExprKind::Match(ref ex, ref arms, MatchSource::Normal) = expr.node {

clippy_lints/src/methods.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::fmt;
1010
use std::iter;
1111
use syntax::ast;
1212
use syntax::codemap::{Span, BytePos};
13-
use crate::utils::{get_arg_name, get_trait_def_id, implements_trait, in_external_macro, in_macro, is_copy, is_expn_of, is_self,
13+
use crate::utils::{get_arg_name, get_trait_def_id, implements_trait, in_macro, is_copy, is_expn_of, is_self,
1414
is_self_ty, iter_input_pats, last_path_segment, match_def_path, match_path, match_qpath, match_trait_method,
1515
match_type, method_chain_args, match_var, return_ty, remove_blocks, same_tys, single_segment_path, snippet,
1616
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 {
806806
}
807807

808808
fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, implitem: &'tcx hir::ImplItem) {
809-
if in_external_macro(cx, implitem.span) {
809+
if in_external_macro(cx.sess(), implitem.span) {
810810
return;
811811
}
812812
let name = implitem.ident.name;

clippy_lints/src/misc_early.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::char;
66
use syntax::ast::*;
77
use syntax::codemap::Span;
88
use syntax::visit::FnKind;
9-
use crate::utils::{constants, in_external_macro, snippet, snippet_opt, span_help_and_lint, span_lint, span_lint_and_then};
9+
use crate::utils::{constants, snippet, snippet_opt, span_help_and_lint, span_lint, span_lint_and_then};
1010

1111
/// **What it does:** Checks for structure field patterns bound to wildcards.
1212
///
@@ -294,7 +294,7 @@ impl EarlyLintPass for MiscEarly {
294294
}
295295

296296
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
297-
if in_external_macro(cx, expr.span) {
297+
if in_external_macro(cx.sess(), expr.span) {
298298
return;
299299
}
300300
match expr.node {

clippy_lints/src/mut_mut.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc::hir::intravisit;
33
use rustc::lint::*;
44
use rustc::{declare_lint, lint_array};
55
use rustc::ty;
6-
use crate::utils::{higher, in_external_macro, span_lint};
6+
use crate::utils::{higher, span_lint};
77

88
/// **What it does:** Checks for instances of `mut mut` references.
99
///
@@ -50,7 +50,7 @@ pub struct MutVisitor<'a, 'tcx: 'a> {
5050

5151
impl<'a, 'tcx> intravisit::Visitor<'tcx> for MutVisitor<'a, 'tcx> {
5252
fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
53-
if in_external_macro(self.cx, expr.span) {
53+
if in_external_macro(self.cx.sess(), expr.span) {
5454
return;
5555
}
5656

clippy_lints/src/neg_cmp_op_on_partial_ord.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc::lint::*;
33
use rustc::{declare_lint, lint_array};
44
use if_chain::if_chain;
55

6-
use crate::utils::{self, paths, span_lint, in_external_macro};
6+
use crate::utils::{self, paths, span_lint};
77

88
/// **What it does:**
99
/// 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 {
5555
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
5656
if_chain! {
5757

58-
if !in_external_macro(cx, expr.span);
58+
if !in_external_macro(cx.sess(), expr.span);
5959
if let ExprKind::Unary(UnOp::UnNot, ref inner) = expr.node;
6060
if let ExprKind::Binary(ref op, ref left, _) = inner.node;
6161
if let BinOpKind::Le | BinOpKind::Ge | BinOpKind::Lt | BinOpKind::Gt = op.node;

clippy_lints/src/new_without_default.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use if_chain::if_chain;
66
use rustc::ty::{self, Ty};
77
use syntax::codemap::Span;
88
use crate::utils::paths;
9-
use crate::utils::{get_trait_def_id, implements_trait, in_external_macro, return_ty, same_tys, span_lint_and_then};
9+
use crate::utils::{get_trait_def_id, implements_trait, return_ty, same_tys, span_lint_and_then};
1010
use crate::utils::sugg::DiagnosticBuilderExt;
1111

1212
/// **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 {
9595
for assoc_item in items {
9696
if let hir::AssociatedItemKind::Method { has_self: false } = assoc_item.kind {
9797
let impl_item = cx.tcx.hir.impl_item(assoc_item.id);
98-
if in_external_macro(cx, impl_item.span) {
98+
if in_external_macro(cx.sess(), impl_item.span) {
9999
return;
100100
}
101101
if let hir::ImplItemKind::Method(ref sig, _) = impl_item.node {

clippy_lints/src/returns.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use syntax::ast;
55
use syntax::codemap::Span;
66
use syntax::visit::FnKind;
77

8-
use crate::utils::{in_external_macro, in_macro, match_path_ast, snippet_opt, span_lint_and_then, span_note_and_lint};
8+
use crate::utils::{in_macro, match_path_ast, snippet_opt, span_lint_and_then, span_note_and_lint};
99

1010
/// **What it does:** Checks for return statements at the end of a block.
1111
///
@@ -90,7 +90,7 @@ impl ReturnPass {
9090
}
9191

9292
fn emit_return_lint(&mut self, cx: &EarlyContext<'_>, ret_span: Span, inner_span: Span) {
93-
if in_external_macro(cx, inner_span) || in_macro(inner_span) {
93+
if in_external_macro(cx.sess(), inner_span) || in_macro(inner_span) {
9494
return;
9595
}
9696
span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded return statement", |db| {
@@ -117,7 +117,7 @@ impl ReturnPass {
117117
if let ast::PatKind::Ident(_, ident, _) = local.pat.node;
118118
if let ast::ExprKind::Path(_, ref path) = retexpr.node;
119119
if match_path_ast(path, &[&ident.as_str()]);
120-
if !in_external_macro(cx, initexpr.span);
120+
if !in_external_macro(cx.sess(), initexpr.span);
121121
then {
122122
span_note_and_lint(cx,
123123
LET_AND_RETURN,

clippy_lints/src/shadow.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc::hir::*;
55
use rustc::hir::intravisit::FnKind;
66
use rustc::ty;
77
use syntax::codemap::Span;
8-
use crate::utils::{contains_name, higher, in_external_macro, iter_input_pats, snippet, span_lint_and_then};
8+
use crate::utils::{contains_name, higher, iter_input_pats, snippet, span_lint_and_then};
99

1010
/// **What it does:** Checks for bindings that shadow other bindings already in
1111
/// scope, while just changing reference level or mutability.
@@ -90,7 +90,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
9090
_: Span,
9191
_: NodeId,
9292
) {
93-
if in_external_macro(cx, body.value.span) {
93+
if in_external_macro(cx.sess(), body.value.span) {
9494
return;
9595
}
9696
check_fn(cx, decl, body);
@@ -122,7 +122,7 @@ fn check_block<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, block: &'tcx Block, binding
122122
}
123123

124124
fn check_decl<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, decl: &'tcx Decl, bindings: &mut Vec<(Name, Span)>) {
125-
if in_external_macro(cx, decl.span) {
125+
if in_external_macro(cx.sess(), decl.span) {
126126
return;
127127
}
128128
if higher::is_from_for_desugar(decl) {
@@ -303,7 +303,7 @@ fn lint_shadow<'a, 'tcx: 'a>(
303303
}
304304

305305
fn check_expr<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, bindings: &mut Vec<(Name, Span)>) {
306-
if in_external_macro(cx, expr.span) {
306+
if in_external_macro(cx.sess(), expr.span) {
307307
return;
308308
}
309309
match expr.node {

clippy_lints/src/types.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::borrow::Cow;
1414
use syntax::ast::{FloatTy, IntTy, UintTy};
1515
use syntax::codemap::Span;
1616
use syntax::errors::DiagnosticBuilder;
17-
use crate::utils::{comparisons, differing_macro_contexts, higher, in_constant, in_external_macro, in_macro, last_path_segment, match_def_path, match_path,
17+
use crate::utils::{comparisons, differing_macro_contexts, higher, in_constant, in_macro, last_path_segment, match_def_path, match_path,
1818
match_type, multispan_sugg, opt_def_id, same_tys, snippet, snippet_opt, span_help_and_lint, span_lint,
1919
span_lint_and_sugg, span_lint_and_then, clip, unsext, sext, int_bits};
2020
use crate::utils::paths;
@@ -381,7 +381,7 @@ declare_clippy_lint! {
381381
fn check_let_unit(cx: &LateContext<'_, '_>, decl: &Decl) {
382382
if let DeclKind::Local(ref local) = decl.node {
383383
if is_unit(cx.tables.pat_ty(&local.pat)) {
384-
if in_external_macro(cx, decl.span) || in_macro(local.pat.span) {
384+
if in_external_macro(cx.sess(), decl.span) || in_macro(local.pat.span) {
385385
return;
386386
}
387387
if higher::is_from_for_desugar(decl) {
@@ -959,7 +959,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CastPass {
959959
use syntax::ast::{LitIntType, LitKind};
960960
match lit.node {
961961
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::FloatUnsuffixed(_) => {},
962-
_ => if cast_from.sty == cast_to.sty && !in_external_macro(cx, expr.span) {
962+
_ => if cast_from.sty == cast_to.sty && !in_external_macro(cx.sess(), expr.span) {
963963
span_lint(
964964
cx,
965965
UNNECESSARY_CAST,
@@ -969,7 +969,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CastPass {
969969
},
970970
}
971971
}
972-
if cast_from.is_numeric() && cast_to.is_numeric() && !in_external_macro(cx, expr.span) {
972+
if cast_from.is_numeric() && cast_to.is_numeric() && !in_external_macro(cx.sess(), expr.span) {
973973
match (cast_from.is_integral(), cast_to.is_integral()) {
974974
(true, false) => {
975975
let from_nbits = int_ty_to_nbits(cast_from, cx.tcx);

clippy_lints/src/utils/mod.rs

+1-31
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::str::FromStr;
1919
use std::rc::Rc;
2020
use syntax::ast::{self, LitKind};
2121
use syntax::attr;
22-
use syntax::codemap::{CompilerDesugaringKind, ExpnFormat, ExpnInfo, Span, DUMMY_SP};
22+
use syntax::codemap::{CompilerDesugaringKind, ExpnFormat, Span, DUMMY_SP};
2323
use syntax::errors::DiagnosticBuilder;
2424
use syntax::ptr::P;
2525
use syntax::symbol::keywords;
@@ -77,36 +77,6 @@ pub fn is_range_expression(span: Span) -> bool {
7777
})
7878
}
7979

80-
/// Returns true if the macro that expanded the crate was outside of the
81-
/// current crate or was a
82-
/// compiler plugin.
83-
pub fn in_external_macro<'a, T: LintContext<'a>>(cx: &T, span: Span) -> bool {
84-
/// Invokes `in_macro` with the expansion info of the given span slightly
85-
/// heavy, try to use
86-
/// this after other checks have already happened.
87-
fn in_macro_ext<'a, T: LintContext<'a>>(cx: &T, info: &ExpnInfo) -> bool {
88-
// no ExpnInfo = no macro
89-
if let ExpnFormat::MacroAttribute(..) = info.format {
90-
// these are all plugins
91-
return true;
92-
}
93-
// no span for the callee = external macro
94-
info.def_site.map_or(true, |span| {
95-
// no snippet = external macro or compiler-builtin expansion
96-
cx.sess()
97-
.codemap()
98-
.span_to_snippet(span)
99-
.ok()
100-
.map_or(true, |code| !code.starts_with("macro_rules"))
101-
})
102-
}
103-
104-
span.ctxt()
105-
.outer()
106-
.expn_info()
107-
.map_or(false, |info| in_macro_ext(cx, &info))
108-
}
109-
11080
/// Check if a `DefId`'s path matches the given absolute type path usage.
11181
///
11282
/// # Examples

0 commit comments

Comments
 (0)