diff --git a/src/bootstrap/test.rs b/src/bootstrap/test.rs index 161f5d5ffd8f5..b8c5751565838 100644 --- a/src/bootstrap/test.rs +++ b/src/bootstrap/test.rs @@ -554,10 +554,7 @@ impl Step for Clippy { builder.add_rustc_lib_path(compiler, &mut cargo); - // FIXME: Disable clippy tests for now, they're failing on master - // (generally this would mean a toolstate failure but we don't have - // toolstate for clippy anymore). - // builder.run(&mut cargo.into()); + builder.run(&mut cargo.into()); } } diff --git a/src/librustc_middle/lint.rs b/src/librustc_middle/lint.rs index 923119e359c47..5b792426d28e1 100644 --- a/src/librustc_middle/lint.rs +++ b/src/librustc_middle/lint.rs @@ -7,8 +7,8 @@ use rustc_errors::{DiagnosticBuilder, DiagnosticId}; use rustc_hir::HirId; use rustc_session::lint::{builtin, Level, Lint, LintId}; use rustc_session::{DiagnosticMessageId, Session}; -use rustc_span::hygiene::MacroKind; -use rustc_span::source_map::{DesugaringKind, ExpnKind, MultiSpan}; +use rustc_span::hygiene::{ClosestAstOrMacro, MacroKind}; +use rustc_span::source_map::{ExpnKind, MultiSpan}; use rustc_span::{Span, Symbol}; /// How a lint level was set. @@ -337,16 +337,19 @@ pub fn struct_lint_level<'s, 'd>( /// This is used to test whether a lint should not even begin to figure out whether it should /// be reported on the current node. pub fn in_external_macro(sess: &Session, span: Span) -> bool { - let expn_data = span.ctxt().outer_expn_data(); - match expn_data.kind { - ExpnKind::Root - | ExpnKind::Desugaring(DesugaringKind::ForLoop(_)) - | ExpnKind::Desugaring(DesugaringKind::Operator) => false, - ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external" - ExpnKind::Macro(MacroKind::Bang, _) => { - // Dummy span for the `def_site` means it's an external macro. - expn_data.def_site.is_dummy() || sess.source_map().is_imported(expn_data.def_site) + match span.ctxt().outer_expn().closest_ast_or_macro() { + ClosestAstOrMacro::Expn(expn_id) => { + let data = expn_id.expn_data(); + match data.kind { + ExpnKind::Macro(MacroKind::Bang, _) => { + // Dummy span for the `def_site` means it's an external macro. + data.def_site.is_dummy() || sess.source_map().is_imported(data.def_site) + } + ExpnKind::Macro(_, _) => true, // definitely a plugin + ExpnKind::AstPass(_) => true, // well, it's "External" + _ => unreachable!("unexpected ExpnData {:?}", data), + } } - ExpnKind::Macro(..) => true, // definitely a plugin + ClosestAstOrMacro::None => false, } } diff --git a/src/librustc_span/hygiene.rs b/src/librustc_span/hygiene.rs index f2c9f8055b975..dc1b3b9087801 100644 --- a/src/librustc_span/hygiene.rs +++ b/src/librustc_span/hygiene.rs @@ -104,9 +104,21 @@ impl ExpnId { HygieneData::with(|data| data.expn_data(self).clone()) } + /// Retrieves the `ClosestAstOrMacro` associated with this `ExpnId` + /// See `ClosestAstOrMacro` for more details + #[inline] + pub fn closest_ast_or_macro(self) -> ClosestAstOrMacro { + HygieneData::with(|data| data.closest_ast_or_macro(self)) + } + #[inline] pub fn set_expn_data(self, expn_data: ExpnData) { HygieneData::with(|data| { + let closest = data.determine_closest_ast_or_macro(self, &expn_data); + let old_closest = &mut data.closest_ast_or_macro[self.0 as usize]; + assert!(old_closest.is_none(), "closest ast/macro data reset for an expansion ID"); + *old_closest = Some(closest); + let old_expn_data = &mut data.expn_data[self.0 as usize]; assert!(old_expn_data.is_none(), "expansion data is reset for an expansion ID"); *old_expn_data = Some(expn_data); @@ -149,6 +161,10 @@ crate struct HygieneData { /// between creation of an expansion ID and obtaining its data (e.g. macros are collected /// first and then resolved later), so we use an `Option` here. expn_data: Vec>, + /// Stores the computed `ClosestAstOrMacro` for each `ExpnId`. This is updated + /// at the same time as `expn_data`, and its contents it determined entirely + /// by the `ExpnData` - this field is just a cache. + closest_ast_or_macro: Vec>, syntax_context_data: Vec, syntax_context_map: FxHashMap<(SyntaxContext, ExpnId, Transparency), SyntaxContext>, } @@ -162,6 +178,7 @@ impl HygieneData { edition, Some(DefId::local(CRATE_DEF_INDEX)), ))], + closest_ast_or_macro: vec![Some(ClosestAstOrMacro::None)], syntax_context_data: vec![SyntaxContextData { outer_expn: ExpnId::root(), outer_transparency: Transparency::Opaque, @@ -178,9 +195,36 @@ impl HygieneData { GLOBALS.with(|globals| f(&mut *globals.hygiene_data.borrow_mut())) } + fn determine_closest_ast_or_macro( + &self, + id: ExpnId, + expn_data: &ExpnData, + ) -> ClosestAstOrMacro { + match expn_data.kind { + ExpnKind::Macro(_, _) | ExpnKind::AstPass(_) => ClosestAstOrMacro::Expn(id), + ExpnKind::Desugaring(_) | ExpnKind::Root => { + // Avoid using `HygieneData` when construction root + // `ExpnData` + if expn_data.call_site.ctxt() == SyntaxContext::root() { + ClosestAstOrMacro::None + } else { + self.closest_ast_or_macro(self.outer_expn(expn_data.call_site.ctxt())) + } + } + } + } + + fn closest_ast_or_macro(&self, expn_id: ExpnId) -> ClosestAstOrMacro { + self.closest_ast_or_macro[expn_id.0 as usize].as_ref().copied().unwrap() + } + fn fresh_expn(&mut self, expn_data: Option) -> ExpnId { + let expn_id = ExpnId(self.expn_data.len() as u32); + self.closest_ast_or_macro.push( + expn_data.as_ref().map(|data| self.determine_closest_ast_or_macro(expn_id, data)), + ); self.expn_data.push(expn_data); - ExpnId(self.expn_data.len() as u32 - 1) + expn_id } fn expn_data(&self, expn_id: ExpnId) -> &ExpnData { @@ -684,6 +728,22 @@ pub struct ExpnData { pub macro_def_id: Option, } +/// The closest `ExpnKind::AstPass` or `ExpnKind::Macro` to an `ExpnData`. +/// 'Closest' is determined by starting at the current `ExpnData`, +/// and walking up the `call_site` tree. If an `ExpnData` with +/// `Expn::AstPass` or `ExpnKind::Macro` is found, it is represented +/// by `ClosestAstOrMacro::Expn(id)`, where `id` is the `EpxId` of +/// the found `ExpnData`. +/// +/// A `ClosestAstOrMacro` implies that no `ExpnKind::AstPass` or `ExpnKind::Macro` +/// are found anywhere in the `call_site` tree - that is, there no macro +/// expansions or ast pass expansions. +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +pub enum ClosestAstOrMacro { + None, + Expn(ExpnId), +} + impl ExpnData { /// Constructs expansion data with default properties. pub fn default( diff --git a/src/tools/clippy/clippy_lints/src/assertions_on_constants.rs b/src/tools/clippy/clippy_lints/src/assertions_on_constants.rs index f8a8fdcd3aa35..d57fa020067ea 100644 --- a/src/tools/clippy/clippy_lints/src/assertions_on_constants.rs +++ b/src/tools/clippy/clippy_lints/src/assertions_on_constants.rs @@ -1,5 +1,5 @@ use crate::consts::{constant, Constant}; -use crate::utils::paths; +use crate::utils::{paths, in_macro}; use crate::utils::{is_direct_expn_of, is_expn_of, match_function_call, snippet_opt, span_lint_and_help}; use if_chain::if_chain; use rustc_ast::ast::LitKind; @@ -67,7 +67,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { }; if let Some(debug_assert_span) = is_expn_of(e.span, "debug_assert") { - if debug_assert_span.from_expansion() { + if in_macro(debug_assert_span) { return; } if_chain! { @@ -79,7 +79,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { } }; } else if let Some(assert_span) = is_direct_expn_of(e.span, "assert") { - if assert_span.from_expansion() { + if in_macro(assert_span) { return; } if let Some(assert_match) = match_assert_with_message(&cx, e) { diff --git a/src/tools/clippy/clippy_lints/src/attrs.rs b/src/tools/clippy/clippy_lints/src/attrs.rs index 41f125d48398f..a1ce6c56085e4 100644 --- a/src/tools/clippy/clippy_lints/src/attrs.rs +++ b/src/tools/clippy/clippy_lints/src/attrs.rs @@ -1,10 +1,7 @@ //! checks for attributes use crate::reexport::Name; -use crate::utils::{ - first_line_of_span, is_present_in_source, match_def_path, paths, snippet_opt, span_lint, span_lint_and_sugg, - span_lint_and_then, without_block_comments, -}; +use crate::utils::{first_line_of_span, is_present_in_source, match_def_path, paths, snippet_opt, span_lint, span_lint_and_sugg, span_lint_and_then, without_block_comments, in_macro}; use if_chain::if_chain; use rustc_ast::ast::{AttrKind, AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem}; use rustc_ast::util::lev_distance::find_best_match_for_name; @@ -474,7 +471,7 @@ fn is_relevant_expr(cx: &LateContext<'_, '_>, tables: &ty::TypeckTables<'_>, exp } fn check_attrs(cx: &LateContext<'_, '_>, span: Span, name: Name, attrs: &[Attribute]) { - if span.from_expansion() { + if in_macro(span) { return; } diff --git a/src/tools/clippy/clippy_lints/src/blocks_in_if_conditions.rs b/src/tools/clippy/clippy_lints/src/blocks_in_if_conditions.rs index 8fa9b05ca3297..25b333fd7ad72 100644 --- a/src/tools/clippy/clippy_lints/src/blocks_in_if_conditions.rs +++ b/src/tools/clippy/clippy_lints/src/blocks_in_if_conditions.rs @@ -1,4 +1,4 @@ -use crate::utils::{differing_macro_contexts, higher, snippet_block_with_applicability, span_lint, span_lint_and_sugg}; +use crate::utils::{differing_macro_contexts, higher, snippet_block_with_applicability, span_lint, span_lint_and_sugg, in_macro}; use rustc_errors::Applicability; use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor}; use rustc_hir::{BlockCheckMode, Expr, ExprKind}; @@ -55,7 +55,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ExVisitor<'a, 'tcx> { if let ExprKind::Closure(_, _, eid, _, _) = expr.kind { let body = self.cx.tcx.hir().body(eid); let ex = &body.value; - if matches!(ex.kind, ExprKind::Block(_, _)) && !body.value.span.from_expansion() { + if matches!(ex.kind, ExprKind::Block(_, _)) && !in_macro(body.value.span) { self.found_block = Some(ex); return; } @@ -83,7 +83,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlocksInIfConditions { if let Some(ex) = &block.expr { // don't dig into the expression here, just suggest that they remove // the block - if expr.span.from_expansion() || differing_macro_contexts(expr.span, ex.span) { + if in_macro(expr.span) || differing_macro_contexts(expr.span, ex.span) { return; } let mut applicability = Applicability::MachineApplicable; @@ -108,7 +108,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlocksInIfConditions { } } else { let span = block.expr.as_ref().map_or_else(|| block.stmts[0].span, |e| e.span); - if span.from_expansion() || differing_macro_contexts(expr.span, span) { + if in_macro(span) || differing_macro_contexts(expr.span, span) { return; } // move block higher diff --git a/src/tools/clippy/clippy_lints/src/booleans.rs b/src/tools/clippy/clippy_lints/src/booleans.rs index f92c564543b89..cf8328d8ff4d7 100644 --- a/src/tools/clippy/clippy_lints/src/booleans.rs +++ b/src/tools/clippy/clippy_lints/src/booleans.rs @@ -107,7 +107,7 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> { } // prevent folding of `cfg!` macros and the like - if !e.span.from_expansion() { + if !in_macro(e.span) { match &e.kind { ExprKind::Unary(UnOp::UnNot, inner) => return Ok(Bool::Not(box self.run(inner)?)), ExprKind::Binary(binop, lhs, rhs) => match &binop.node { diff --git a/src/tools/clippy/clippy_lints/src/cognitive_complexity.rs b/src/tools/clippy/clippy_lints/src/cognitive_complexity.rs index 3ba72e84fa827..6b74817e3d9e1 100644 --- a/src/tools/clippy/clippy_lints/src/cognitive_complexity.rs +++ b/src/tools/clippy/clippy_lints/src/cognitive_complexity.rs @@ -9,7 +9,7 @@ use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::source_map::Span; use rustc_span::BytePos; -use crate::utils::{is_type_diagnostic_item, snippet_opt, span_lint_and_help, LimitStack}; +use crate::utils::{is_type_diagnostic_item, snippet_opt, span_lint_and_help, LimitStack, in_macro}; declare_clippy_lint! { /// **What it does:** Checks for methods with high cognitive complexity. @@ -51,7 +51,7 @@ impl CognitiveComplexity { body: &'tcx Body<'_>, body_span: Span, ) { - if body_span.from_expansion() { + if in_macro(body_span) { return; } diff --git a/src/tools/clippy/clippy_lints/src/collapsible_if.rs b/src/tools/clippy/clippy_lints/src/collapsible_if.rs index 8090f4673aae0..9636fae5420bf 100644 --- a/src/tools/clippy/clippy_lints/src/collapsible_if.rs +++ b/src/tools/clippy/clippy_lints/src/collapsible_if.rs @@ -18,7 +18,7 @@ use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use crate::utils::sugg::Sugg; -use crate::utils::{snippet_block, snippet_block_with_applicability, span_lint_and_sugg, span_lint_and_then}; +use crate::utils::{snippet_block, snippet_block_with_applicability, span_lint_and_sugg, span_lint_and_then, in_macro}; use rustc_errors::Applicability; declare_clippy_lint! { @@ -75,7 +75,7 @@ declare_lint_pass!(CollapsibleIf => [COLLAPSIBLE_IF]); impl EarlyLintPass for CollapsibleIf { fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) { - if !expr.span.from_expansion() { + if !in_macro(expr.span) { check_if(cx, expr) } } @@ -106,7 +106,7 @@ fn check_collapsible_maybe_if_let(cx: &EarlyContext<'_>, else_: &ast::Expr) { if let ast::ExprKind::Block(ref block, _) = else_.kind; if !block_starts_with_comment(cx, block); if let Some(else_) = expr_block(block); - if !else_.span.from_expansion(); + if !in_macro(else_.span); if let ast::ExprKind::If(..) = else_.kind; then { let mut applicability = Applicability::MachineApplicable; diff --git a/src/tools/clippy/clippy_lints/src/comparison_chain.rs b/src/tools/clippy/clippy_lints/src/comparison_chain.rs index 93e29edcaa58f..a1f093d976e17 100644 --- a/src/tools/clippy/clippy_lints/src/comparison_chain.rs +++ b/src/tools/clippy/clippy_lints/src/comparison_chain.rs @@ -1,6 +1,4 @@ -use crate::utils::{ - get_trait_def_id, if_sequence, implements_trait, parent_node_is_if_expr, paths, span_lint_and_help, SpanlessEq, -}; +use crate::utils::{get_trait_def_id, if_sequence, implements_trait, parent_node_is_if_expr, paths, span_lint_and_help, SpanlessEq, in_macro}; use rustc_hir::{BinOpKind, Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; @@ -54,7 +52,7 @@ declare_lint_pass!(ComparisonChain => [COMPARISON_CHAIN]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ComparisonChain { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) { - if expr.span.from_expansion() { + if in_macro(expr.span) { return; } diff --git a/src/tools/clippy/clippy_lints/src/copies.rs b/src/tools/clippy/clippy_lints/src/copies.rs index b6d50bdfa1466..83f9694905bb9 100644 --- a/src/tools/clippy/clippy_lints/src/copies.rs +++ b/src/tools/clippy/clippy_lints/src/copies.rs @@ -1,4 +1,4 @@ -use crate::utils::{get_parent_expr, higher, if_sequence, snippet, span_lint_and_note, span_lint_and_then}; +use crate::utils::{get_parent_expr, higher, if_sequence, snippet, span_lint_and_note, span_lint_and_then, in_macro}; use crate::utils::{SpanlessEq, SpanlessHash}; use rustc_data_structures::fx::FxHashMap; use rustc_hir::{Arm, Block, Expr, ExprKind, MatchSource, Pat, PatKind}; @@ -153,7 +153,7 @@ declare_lint_pass!(CopyAndPaste => [IFS_SAME_COND, SAME_FUNCTIONS_IN_IF_CONDITIO impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CopyAndPaste { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) { - if !expr.span.from_expansion() { + if !in_macro(expr.span) { // skip ifs directly in else, it will be checked in the parent if if let Some(expr) = get_parent_expr(cx, expr) { if let Some((_, _, Some(ref else_expr))) = higher::if_block(&expr) { diff --git a/src/tools/clippy/clippy_lints/src/dereference.rs b/src/tools/clippy/clippy_lints/src/dereference.rs index 1cd30ae2c6381..7e3c76f38dc7b 100644 --- a/src/tools/clippy/clippy_lints/src/dereference.rs +++ b/src/tools/clippy/clippy_lints/src/dereference.rs @@ -1,4 +1,4 @@ -use crate::utils::{get_parent_expr, implements_trait, snippet, span_lint_and_sugg}; +use crate::utils::{get_parent_expr, implements_trait, snippet, span_lint_and_sugg, in_macro}; use if_chain::if_chain; use rustc_ast::util::parser::{ExprPrecedence, PREC_POSTFIX, PREC_PREFIX}; use rustc_errors::Applicability; @@ -41,7 +41,7 @@ declare_lint_pass!(Dereferencing => [ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Dereferencing { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) { if_chain! { - if !expr.span.from_expansion(); + if !in_macro(expr.span); if let ExprKind::MethodCall(ref method_name, _, ref args, _) = &expr.kind; if args.len() == 1; diff --git a/src/tools/clippy/clippy_lints/src/double_parens.rs b/src/tools/clippy/clippy_lints/src/double_parens.rs index 1eb380a22cc6b..b6ebfe005106b 100644 --- a/src/tools/clippy/clippy_lints/src/double_parens.rs +++ b/src/tools/clippy/clippy_lints/src/double_parens.rs @@ -1,4 +1,4 @@ -use crate::utils::span_lint; +use crate::utils::{span_lint, in_macro}; use rustc_ast::ast::{Expr, ExprKind}; use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; @@ -41,7 +41,7 @@ declare_lint_pass!(DoubleParens => [DOUBLE_PARENS]); impl EarlyLintPass for DoubleParens { fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { - if expr.span.from_expansion() { + if in_macro(expr.span) { return; } diff --git a/src/tools/clippy/clippy_lints/src/enum_variants.rs b/src/tools/clippy/clippy_lints/src/enum_variants.rs index cb0fd59a2d407..a035a6f413034 100644 --- a/src/tools/clippy/clippy_lints/src/enum_variants.rs +++ b/src/tools/clippy/clippy_lints/src/enum_variants.rs @@ -1,6 +1,6 @@ //! lint on enum variants that are prefixed or suffixed by the same characters -use crate::utils::{camel_case, is_present_in_source}; +use crate::utils::{camel_case, is_present_in_source, in_macro}; use crate::utils::{span_lint, span_lint_and_help}; use rustc_ast::ast::{EnumDef, Item, ItemKind, VisibilityKind}; use rustc_lint::{EarlyContext, EarlyLintPass, Lint}; @@ -271,7 +271,7 @@ impl EarlyLintPass for EnumVariantNames { let item_name = item.ident.name.as_str(); let item_name_chars = item_name.chars().count(); let item_camel = to_camel_case(&item_name); - if !item.span.from_expansion() && is_present_in_source(cx, item.span) { + if !in_macro(item.span) && is_present_in_source(cx, item.span) { if let Some(&(ref mod_name, ref mod_camel)) = self.modules.last() { // constants don't have surrounding modules if !mod_camel.is_empty() { diff --git a/src/tools/clippy/clippy_lints/src/eq_op.rs b/src/tools/clippy/clippy_lints/src/eq_op.rs index d7819d737ea04..88c701ac5d8c8 100644 --- a/src/tools/clippy/clippy_lints/src/eq_op.rs +++ b/src/tools/clippy/clippy_lints/src/eq_op.rs @@ -56,7 +56,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp { #[allow(clippy::similar_names, clippy::too_many_lines)] fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) { if let ExprKind::Binary(op, ref left, ref right) = e.kind { - if e.span.from_expansion() { + if in_macro(e.span) { return; } let macro_with_not_op = |expr_kind: &ExprKind<'_>| { diff --git a/src/tools/clippy/clippy_lints/src/erasing_op.rs b/src/tools/clippy/clippy_lints/src/erasing_op.rs index 3ff0506e28d00..29bc16e9b1eae 100644 --- a/src/tools/clippy/clippy_lints/src/erasing_op.rs +++ b/src/tools/clippy/clippy_lints/src/erasing_op.rs @@ -4,7 +4,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::Span; use crate::consts::{constant_simple, Constant}; -use crate::utils::span_lint; +use crate::utils::{span_lint, in_macro}; declare_clippy_lint! { /// **What it does:** Checks for erasing operations, e.g., `x * 0`. @@ -31,7 +31,7 @@ declare_lint_pass!(ErasingOp => [ERASING_OP]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ErasingOp { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) { - if e.span.from_expansion() { + if in_macro(e.span) { return; } if let ExprKind::Binary(ref cmp, ref left, ref right) = e.kind { diff --git a/src/tools/clippy/clippy_lints/src/format.rs b/src/tools/clippy/clippy_lints/src/format.rs index 4cae5ca2c4326..fcf29870880ac 100644 --- a/src/tools/clippy/clippy_lints/src/format.rs +++ b/src/tools/clippy/clippy_lints/src/format.rs @@ -1,4 +1,4 @@ -use crate::utils::paths; +use crate::utils::{paths, in_macro}; use crate::utils::{ is_expn_of, is_type_diagnostic_item, last_path_segment, match_def_path, match_function_call, snippet, span_lint_and_then, walk_ptrs_ty, @@ -43,7 +43,7 @@ declare_lint_pass!(UselessFormat => [USELESS_FORMAT]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessFormat { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) { let span = match is_expn_of(expr.span, "format") { - Some(s) if !s.from_expansion() => s, + Some(s) if !in_macro(s) => s, _ => return, }; diff --git a/src/tools/clippy/clippy_lints/src/formatting.rs b/src/tools/clippy/clippy_lints/src/formatting.rs index 156246fb8bbb0..3eb7e06a760d8 100644 --- a/src/tools/clippy/clippy_lints/src/formatting.rs +++ b/src/tools/clippy/clippy_lints/src/formatting.rs @@ -1,4 +1,4 @@ -use crate::utils::{differing_macro_contexts, snippet_opt, span_lint_and_help, span_lint_and_note}; +use crate::utils::{differing_macro_contexts, snippet_opt, span_lint_and_help, span_lint_and_note, in_macro}; use if_chain::if_chain; use rustc_ast::ast::{BinOpKind, Block, Expr, ExprKind, StmtKind, UnOp}; use rustc_lint::{EarlyContext, EarlyLintPass}; @@ -129,7 +129,7 @@ impl EarlyLintPass for Formatting { /// Implementation of the `SUSPICIOUS_ASSIGNMENT_FORMATTING` lint. fn check_assign(cx: &EarlyContext<'_>, expr: &Expr) { if let ExprKind::Assign(ref lhs, ref rhs, _) = expr.kind { - if !differing_macro_contexts(lhs.span, rhs.span) && !lhs.span.from_expansion() { + if !differing_macro_contexts(lhs.span, rhs.span) && !in_macro(lhs.span) { let eq_span = lhs.span.between(rhs.span); if let ExprKind::Unary(op, ref sub_rhs) = rhs.kind { if let Some(eq_snippet) = snippet_opt(cx, eq_span) { @@ -159,7 +159,7 @@ fn check_assign(cx: &EarlyContext<'_>, expr: &Expr) { fn check_unop(cx: &EarlyContext<'_>, expr: &Expr) { if_chain! { if let ExprKind::Binary(ref binop, ref lhs, ref rhs) = expr.kind; - if !differing_macro_contexts(lhs.span, rhs.span) && !lhs.span.from_expansion(); + if !differing_macro_contexts(lhs.span, rhs.span) && !in_macro(lhs.span); // span between BinOp LHS and RHS let binop_span = lhs.span.between(rhs.span); // if RHS is a UnOp @@ -201,7 +201,7 @@ fn check_else(cx: &EarlyContext<'_>, expr: &Expr) { if let ExprKind::If(_, then, Some(else_)) = &expr.kind; if is_block(else_) || is_if(else_); if !differing_macro_contexts(then.span, else_.span); - if !then.span.from_expansion() && !in_external_macro(cx.sess, expr.span); + if !in_macro(then.span) && !in_external_macro(cx.sess, expr.span); // workaround for rust-lang/rust#43081 if expr.span.lo().0 != 0 && expr.span.hi().0 != 0; @@ -273,7 +273,7 @@ fn check_array(cx: &EarlyContext<'_>, expr: &Expr) { fn check_missing_else(cx: &EarlyContext<'_>, first: &Expr, second: &Expr) { if !differing_macro_contexts(first.span, second.span) - && !first.span.from_expansion() + && !in_macro(first.span) && is_if(first) && (is_block(second) || is_if(second)) { diff --git a/src/tools/clippy/clippy_lints/src/identity_op.rs b/src/tools/clippy/clippy_lints/src/identity_op.rs index 78e07d25f67c5..789e03cd08714 100644 --- a/src/tools/clippy/clippy_lints/src/identity_op.rs +++ b/src/tools/clippy/clippy_lints/src/identity_op.rs @@ -6,7 +6,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::Span; use crate::consts::{constant_simple, Constant}; -use crate::utils::{clip, snippet, span_lint, unsext}; +use crate::utils::{clip, snippet, span_lint, unsext, in_macro}; declare_clippy_lint! { /// **What it does:** Checks for identity operations, e.g., `x + 0`. @@ -30,7 +30,7 @@ declare_lint_pass!(IdentityOp => [IDENTITY_OP]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityOp { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) { - if e.span.from_expansion() { + if in_macro(e.span) { return; } if let ExprKind::Binary(cmp, ref left, ref right) = e.kind { diff --git a/src/tools/clippy/clippy_lints/src/implicit_return.rs b/src/tools/clippy/clippy_lints/src/implicit_return.rs index c4308fd26a302..04276d2a4f5cf 100644 --- a/src/tools/clippy/clippy_lints/src/implicit_return.rs +++ b/src/tools/clippy/clippy_lints/src/implicit_return.rs @@ -1,8 +1,4 @@ -use crate::utils::{ - fn_has_unsatisfiable_preds, match_def_path, - paths::{BEGIN_PANIC, BEGIN_PANIC_FMT}, - snippet_opt, span_lint_and_then, -}; +use crate::utils::{fn_has_unsatisfiable_preds, match_def_path, paths::{BEGIN_PANIC, BEGIN_PANIC_FMT}, snippet_opt, span_lint_and_then, in_macro}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::intravisit::FnKind; @@ -143,7 +139,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitReturn { // checking return type through MIR, HIR is not able to determine inferred closure return types // make sure it's not a macro - if !mir.return_ty().is_unit() && !span.from_expansion() { + if !mir.return_ty().is_unit() && !in_macro(span) { expr_match(cx, &body.value); } } diff --git a/src/tools/clippy/clippy_lints/src/inherent_to_string.rs b/src/tools/clippy/clippy_lints/src/inherent_to_string.rs index 289628a2752af..1aeda23d9c959 100644 --- a/src/tools/clippy/clippy_lints/src/inherent_to_string.rs +++ b/src/tools/clippy/clippy_lints/src/inherent_to_string.rs @@ -3,10 +3,7 @@ use rustc_hir::{ImplItem, ImplItemKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; -use crate::utils::{ - get_trait_def_id, implements_trait, is_type_diagnostic_item, paths, return_ty, span_lint_and_help, - trait_ref_of_method, walk_ptrs_ty, -}; +use crate::utils::{get_trait_def_id, implements_trait, is_type_diagnostic_item, paths, return_ty, span_lint_and_help, trait_ref_of_method, walk_ptrs_ty, in_macro}; declare_clippy_lint! { /// **What it does:** Checks for the definition of inherent methods with a signature of `to_string(&self) -> String`. @@ -94,7 +91,7 @@ declare_lint_pass!(InherentToString => [INHERENT_TO_STRING, INHERENT_TO_STRING_S impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InherentToString { fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx ImplItem<'_>) { - if impl_item.span.from_expansion() { + if in_macro(impl_item.span) { return; } diff --git a/src/tools/clippy/clippy_lints/src/items_after_statements.rs b/src/tools/clippy/clippy_lints/src/items_after_statements.rs index c8576bcfcb444..e55a2d220c392 100644 --- a/src/tools/clippy/clippy_lints/src/items_after_statements.rs +++ b/src/tools/clippy/clippy_lints/src/items_after_statements.rs @@ -1,6 +1,6 @@ //! lint when items are used after statements -use crate::utils::span_lint; +use crate::utils::{span_lint, in_macro}; use rustc_ast::ast::{Block, ItemKind, StmtKind}; use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; @@ -53,7 +53,7 @@ declare_lint_pass!(ItemsAfterStatements => [ITEMS_AFTER_STATEMENTS]); impl EarlyLintPass for ItemsAfterStatements { fn check_block(&mut self, cx: &EarlyContext<'_>, item: &Block) { - if item.span.from_expansion() { + if in_macro(item.span) { return; } @@ -67,7 +67,7 @@ impl EarlyLintPass for ItemsAfterStatements { // lint on all further items for stmt in stmts { if let StmtKind::Item(ref it) = *stmt { - if it.span.from_expansion() { + if in_macro(it.span) { return; } if let ItemKind::MacroDef(..) = it.kind { diff --git a/src/tools/clippy/clippy_lints/src/large_const_arrays.rs b/src/tools/clippy/clippy_lints/src/large_const_arrays.rs index c9e12fc535ec0..d9b6fc4d78f89 100644 --- a/src/tools/clippy/clippy_lints/src/large_const_arrays.rs +++ b/src/tools/clippy/clippy_lints/src/large_const_arrays.rs @@ -1,5 +1,5 @@ use crate::rustc_target::abi::LayoutOf; -use crate::utils::span_lint_and_then; +use crate::utils::{span_lint_and_then, in_macro}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::{Item, ItemKind}; @@ -48,7 +48,7 @@ impl_lint_pass!(LargeConstArrays => [LARGE_CONST_ARRAYS]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LargeConstArrays { fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'_>) { if_chain! { - if !item.span.from_expansion(); + if !in_macro(item.span); if let ItemKind::Const(hir_ty, _) = &item.kind; let ty = hir_ty_to_ty(cx.tcx, hir_ty); if let ty::Array(element_type, cst) = ty.kind; diff --git a/src/tools/clippy/clippy_lints/src/len_zero.rs b/src/tools/clippy/clippy_lints/src/len_zero.rs index 13e85fda8ffeb..044859c26b70b 100644 --- a/src/tools/clippy/clippy_lints/src/len_zero.rs +++ b/src/tools/clippy/clippy_lints/src/len_zero.rs @@ -1,4 +1,4 @@ -use crate::utils::{get_item_name, higher, snippet_with_applicability, span_lint, span_lint_and_sugg, walk_ptrs_ty}; +use crate::utils::{get_item_name, higher, snippet_with_applicability, span_lint, span_lint_and_sugg, walk_ptrs_ty, in_macro}; use rustc_ast::ast::LitKind; use rustc_data_structures::fx::FxHashSet; use rustc_errors::Applicability; @@ -72,7 +72,7 @@ declare_lint_pass!(LenZero => [LEN_ZERO, LEN_WITHOUT_IS_EMPTY]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LenZero { fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'_>) { - if item.span.from_expansion() { + if in_macro(item.span) { return; } @@ -88,7 +88,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LenZero { } fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) { - if expr.span.from_expansion() { + if in_macro(expr.span) { return; } diff --git a/src/tools/clippy/clippy_lints/src/loops.rs b/src/tools/clippy/clippy_lints/src/loops.rs index 771bc8d055825..3bd54928f0351 100644 --- a/src/tools/clippy/clippy_lints/src/loops.rs +++ b/src/tools/clippy/clippy_lints/src/loops.rs @@ -1,6 +1,6 @@ use crate::consts::constant; use crate::reexport::Name; -use crate::utils::paths; +use crate::utils::{paths, in_macro}; use crate::utils::usage::{is_unused, mutated_variables}; use crate::utils::{ get_enclosing_block, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait, @@ -445,14 +445,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Loops { // we don't want to check expanded macros // this check is not at the top of the function // since higher::for_loop expressions are marked as expansions - if body.span.from_expansion() { + if in_macro(body.span) { return; } check_for_loop(cx, pat, arg, body, expr); } // we don't want to check expanded macros - if expr.span.from_expansion() { + if in_macro(expr.span) { return; } diff --git a/src/tools/clippy/clippy_lints/src/map_clone.rs b/src/tools/clippy/clippy_lints/src/map_clone.rs index 8f4fdc685ef38..a30f31f5f8939 100644 --- a/src/tools/clippy/clippy_lints/src/map_clone.rs +++ b/src/tools/clippy/clippy_lints/src/map_clone.rs @@ -1,4 +1,4 @@ -use crate::utils::paths; +use crate::utils::{paths, in_macro}; use crate::utils::{ is_copy, is_type_diagnostic_item, match_trait_method, remove_blocks, snippet_with_applicability, span_lint_and_sugg, }; @@ -44,7 +44,7 @@ declare_lint_pass!(MapClone => [MAP_CLONE]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapClone { fn check_expr(&mut self, cx: &LateContext<'_, '_>, e: &hir::Expr<'_>) { - if e.span.from_expansion() { + if in_macro(e.span) { return; } diff --git a/src/tools/clippy/clippy_lints/src/map_unit_fn.rs b/src/tools/clippy/clippy_lints/src/map_unit_fn.rs index 8f4b674c04f49..21adcb6e975af 100644 --- a/src/tools/clippy/clippy_lints/src/map_unit_fn.rs +++ b/src/tools/clippy/clippy_lints/src/map_unit_fn.rs @@ -1,4 +1,4 @@ -use crate::utils::{is_type_diagnostic_item, iter_input_pats, method_chain_args, snippet, span_lint_and_then}; +use crate::utils::{is_type_diagnostic_item, iter_input_pats, method_chain_args, snippet, span_lint_and_then, in_macro}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir as hir; @@ -260,7 +260,7 @@ fn lint_map_unit_fn(cx: &LateContext<'_, '_>, stmt: &hir::Stmt<'_>, expr: &hir:: impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapUnit { fn check_stmt(&mut self, cx: &LateContext<'_, '_>, stmt: &hir::Stmt<'_>) { - if stmt.span.from_expansion() { + if in_macro(stmt.span) { return; } diff --git a/src/tools/clippy/clippy_lints/src/matches.rs b/src/tools/clippy/clippy_lints/src/matches.rs index 6d7af45a47224..f54247f62f7fa 100644 --- a/src/tools/clippy/clippy_lints/src/matches.rs +++ b/src/tools/clippy/clippy_lints/src/matches.rs @@ -912,7 +912,7 @@ fn check_match_ref_pats(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[Arm<'_> })); span_lint_and_then(cx, MATCH_REF_PATS, expr.span, title, |diag| { - if !expr.span.from_expansion() { + if !in_macro(expr.span) { multispan_sugg(diag, msg, suggs); } }); @@ -996,7 +996,7 @@ fn check_match_single_binding<'a>(cx: &LateContext<'_, 'a>, ex: &Expr<'a>, arms: let matched_vars = ex.span; let bind_names = arms[0].pat.span; let match_body = remove_blocks(&arms[0].body); - let mut snippet_body = if match_body.span.from_expansion() { + let mut snippet_body = if in_macro(match_body.span) { Sugg::hir_with_macro_callsite(cx, match_body, "..").to_string() } else { snippet_block(cx, match_body.span, "..", Some(expr.span)).to_string() @@ -1006,7 +1006,7 @@ fn check_match_single_binding<'a>(cx: &LateContext<'_, 'a>, ex: &Expr<'a>, arms: match match_body.kind { ExprKind::Block(block, _) => { // macro + expr_ty(body) == () - if block.span.from_expansion() && cx.tables.expr_ty(&match_body).is_unit() { + if in_macro(block.span) && cx.tables.expr_ty(&match_body).is_unit() { snippet_body.push(';'); } }, diff --git a/src/tools/clippy/clippy_lints/src/methods/bind_instead_of_map.rs b/src/tools/clippy/clippy_lints/src/methods/bind_instead_of_map.rs index 32e86637569ed..edd3fec855e7f 100644 --- a/src/tools/clippy/clippy_lints/src/methods/bind_instead_of_map.rs +++ b/src/tools/clippy/clippy_lints/src/methods/bind_instead_of_map.rs @@ -95,7 +95,7 @@ pub(crate) trait BindInsteadOfMap { return false; } - let some_inner_snip = if inner_expr.span.from_expansion() { + let some_inner_snip = if in_macro(inner_expr.span) { snippet_with_macro_callsite(cx, inner_expr.span, "_") } else { snippet(cx, inner_expr.span, "_") diff --git a/src/tools/clippy/clippy_lints/src/misc.rs b/src/tools/clippy/clippy_lints/src/misc.rs index a0947608e6077..d0bccbfc49adc 100644 --- a/src/tools/clippy/clippy_lints/src/misc.rs +++ b/src/tools/clippy/clippy_lints/src/misc.rs @@ -14,11 +14,7 @@ use rustc_span::source_map::{ExpnKind, Span}; use crate::consts::{constant, Constant}; use crate::utils::sugg::Sugg; -use crate::utils::{ - get_item_name, get_parent_expr, higher, implements_trait, in_constant, is_integer_const, iter_input_pats, - last_path_segment, match_qpath, match_trait_method, paths, snippet, snippet_opt, span_lint, span_lint_and_sugg, - span_lint_and_then, span_lint_hir_and_then, walk_ptrs_ty, SpanlessEq, -}; +use crate::utils::{get_item_name, get_parent_expr, higher, implements_trait, in_constant, is_integer_const, iter_input_pats, last_path_segment, match_qpath, match_trait_method, paths, snippet, snippet_opt, span_lint, span_lint_and_sugg, span_lint_and_then, span_lint_hir_and_then, walk_ptrs_ty, SpanlessEq, in_macro}; declare_clippy_lint! { /// **What it does:** Checks for function arguments and let bindings denoted as @@ -295,7 +291,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints { if !higher::is_from_for_desugar(local); then { if an == BindingAnnotation::Ref || an == BindingAnnotation::RefMut { - let sugg_init = if init.span.from_expansion() { + let sugg_init = if in_macro(init.span) { Sugg::hir_with_macro_callsite(cx, init, "..") } else { Sugg::hir(cx, init, "..") @@ -672,7 +668,7 @@ fn is_used(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool { /// generated by `#[derive(...)]` or the like). fn in_attributes_expansion(expr: &Expr<'_>) -> bool { use rustc_span::hygiene::MacroKind; - if expr.span.from_expansion() { + if in_macro(expr.span) { let data = expr.span.ctxt().outer_expn_data(); if let ExpnKind::Macro(MacroKind::Attr, _) = data.kind { @@ -688,7 +684,7 @@ fn in_attributes_expansion(expr: &Expr<'_>) -> bool { /// Tests whether `res` is a variable defined outside a macro. fn non_macro_local(cx: &LateContext<'_, '_>, res: def::Res) -> bool { if let def::Res::Local(id) = res { - !cx.tcx.hir().span(id).from_expansion() + !in_macro(cx.tcx.hir().span(id)) } else { false } diff --git a/src/tools/clippy/clippy_lints/src/missing_doc.rs b/src/tools/clippy/clippy_lints/src/missing_doc.rs index 0fd1e87f9e415..2588a4aa507ae 100644 --- a/src/tools/clippy/clippy_lints/src/missing_doc.rs +++ b/src/tools/clippy/clippy_lints/src/missing_doc.rs @@ -5,7 +5,7 @@ // [`missing_doc`]: https://github.com/rust-lang/rust/blob/d6d05904697d89099b55da3331155392f1db9c00/src/librustc_lint/builtin.rs#L246 // -use crate::utils::span_lint; +use crate::utils::{span_lint, in_macro}; use if_chain::if_chain; use rustc_ast::ast::{self, MetaItem, MetaItemKind}; use rustc_ast::attr; @@ -87,7 +87,7 @@ impl MissingDoc { return; } - if sp.from_expansion() { + if in_macro(sp) { return; } diff --git a/src/tools/clippy/clippy_lints/src/needless_bool.rs b/src/tools/clippy/clippy_lints/src/needless_bool.rs index 15b129fa09802..20bd4dd31a9ae 100644 --- a/src/tools/clippy/clippy_lints/src/needless_bool.rs +++ b/src/tools/clippy/clippy_lints/src/needless_bool.rs @@ -3,7 +3,7 @@ //! This lint is **warn** by default use crate::utils::sugg::Sugg; -use crate::utils::{higher, parent_node_is_if_expr, snippet_with_applicability, span_lint, span_lint_and_sugg}; +use crate::utils::{higher, parent_node_is_if_expr, snippet_with_applicability, span_lint, span_lint_and_sugg, in_macro}; use if_chain::if_chain; use rustc_ast::ast::LitKind; use rustc_errors::Applicability; @@ -129,7 +129,7 @@ declare_lint_pass!(BoolComparison => [BOOL_COMPARISON]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoolComparison { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) { - if e.span.from_expansion() { + if in_macro(e.span) { return; } diff --git a/src/tools/clippy/clippy_lints/src/needless_borrow.rs b/src/tools/clippy/clippy_lints/src/needless_borrow.rs index 5880d1d610206..dda4a18394dc5 100644 --- a/src/tools/clippy/clippy_lints/src/needless_borrow.rs +++ b/src/tools/clippy/clippy_lints/src/needless_borrow.rs @@ -2,7 +2,7 @@ //! //! This lint is **warn** by default -use crate::utils::{snippet_opt, span_lint_and_then}; +use crate::utils::{snippet_opt, span_lint_and_then, in_macro}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::{BindingAnnotation, BorrowKind, Expr, ExprKind, HirId, Item, Mutability, Pat, PatKind}; @@ -42,7 +42,7 @@ impl_lint_pass!(NeedlessBorrow => [NEEDLESS_BORROW]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrow { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) { - if e.span.from_expansion() || self.derived_item.is_some() { + if in_macro(e.span) || self.derived_item.is_some() { return; } if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, ref inner) = e.kind { @@ -80,7 +80,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrow { } } fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat<'_>) { - if pat.span.from_expansion() || self.derived_item.is_some() { + if in_macro(pat.span) || self.derived_item.is_some() { return; } if_chain! { diff --git a/src/tools/clippy/clippy_lints/src/needless_borrowed_ref.rs b/src/tools/clippy/clippy_lints/src/needless_borrowed_ref.rs index e56489c6d434d..7a32b001478e8 100644 --- a/src/tools/clippy/clippy_lints/src/needless_borrowed_ref.rs +++ b/src/tools/clippy/clippy_lints/src/needless_borrowed_ref.rs @@ -2,7 +2,7 @@ //! //! This lint is **warn** by default -use crate::utils::{snippet_with_applicability, span_lint_and_then}; +use crate::utils::{snippet_with_applicability, span_lint_and_then, in_macro}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::{BindingAnnotation, Mutability, Node, Pat, PatKind}; @@ -54,7 +54,7 @@ declare_lint_pass!(NeedlessBorrowedRef => [NEEDLESS_BORROWED_REFERENCE]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrowedRef { fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat<'_>) { - if pat.span.from_expansion() { + if in_macro(pat.span) { // OK, simple enough, lints doesn't check in macro. return; } diff --git a/src/tools/clippy/clippy_lints/src/needless_continue.rs b/src/tools/clippy/clippy_lints/src/needless_continue.rs index a971d041ca661..80fa2a559db1f 100644 --- a/src/tools/clippy/clippy_lints/src/needless_continue.rs +++ b/src/tools/clippy/clippy_lints/src/needless_continue.rs @@ -39,7 +39,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::{original_sp, DUMMY_SP}; use rustc_span::Span; -use crate::utils::{indent_of, snippet, snippet_block, span_lint_and_help}; +use crate::utils::{indent_of, snippet, snippet_block, span_lint_and_help, in_macro}; declare_clippy_lint! { /// **What it does:** The lint checks for `if`-statements appearing in loops @@ -120,7 +120,7 @@ declare_lint_pass!(NeedlessContinue => [NEEDLESS_CONTINUE]); impl EarlyLintPass for NeedlessContinue { fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) { - if !expr.span.from_expansion() { + if !in_macro(expr.span) { check_and_warn(cx, expr); } } diff --git a/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs b/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs index 218b0d27f7486..1bca5704a0128 100644 --- a/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs +++ b/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs @@ -1,8 +1,5 @@ use crate::utils::ptr::get_spans; -use crate::utils::{ - get_trait_def_id, implements_trait, is_copy, is_self, is_type_diagnostic_item, multispan_sugg, paths, snippet, - snippet_opt, span_lint_and_then, -}; +use crate::utils::{get_trait_def_id, implements_trait, is_copy, is_self, is_type_diagnostic_item, multispan_sugg, paths, snippet, snippet_opt, span_lint_and_then, in_macro}; use if_chain::if_chain; use rustc_ast::ast::Attribute; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; @@ -75,7 +72,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue { span: Span, hir_id: HirId, ) { - if span.from_expansion() { + if in_macro(span) { return; } diff --git a/src/tools/clippy/clippy_lints/src/no_effect.rs b/src/tools/clippy/clippy_lints/src/no_effect.rs index 2eacd3c80c486..ddb895de1385d 100644 --- a/src/tools/clippy/clippy_lints/src/no_effect.rs +++ b/src/tools/clippy/clippy_lints/src/no_effect.rs @@ -1,4 +1,4 @@ -use crate::utils::{has_drop, qpath_res, snippet_opt, span_lint, span_lint_and_sugg}; +use crate::utils::{has_drop, qpath_res, snippet_opt, span_lint, span_lint_and_sugg, in_macro}; use rustc_errors::Applicability; use rustc_hir::def::{DefKind, Res}; use rustc_hir::{BinOpKind, BlockCheckMode, Expr, ExprKind, Stmt, StmtKind, UnsafeSource}; @@ -43,7 +43,7 @@ declare_clippy_lint! { } fn has_no_effect(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool { - if expr.span.from_expansion() { + if in_macro(expr.span) { return false; } match expr.kind { @@ -95,7 +95,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NoEffect { } else if let Some(reduced) = reduce_expression(cx, expr) { let mut snippet = String::new(); for e in reduced { - if e.span.from_expansion() { + if in_macro(e.span) { return; } if let Some(snip) = snippet_opt(cx, e.span) { @@ -120,7 +120,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NoEffect { } fn reduce_expression<'a>(cx: &LateContext<'_, '_>, expr: &'a Expr<'a>) -> Option>> { - if expr.span.from_expansion() { + if in_macro(expr.span) { return None; } match expr.kind { diff --git a/src/tools/clippy/clippy_lints/src/non_copy_const.rs b/src/tools/clippy/clippy_lints/src/non_copy_const.rs index bb257e5a542d9..d039f9dd5bb01 100644 --- a/src/tools/clippy/clippy_lints/src/non_copy_const.rs +++ b/src/tools/clippy/clippy_lints/src/non_copy_const.rs @@ -13,7 +13,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::{InnerSpan, Span, DUMMY_SP}; use rustc_typeck::hir_ty_to_ty; -use crate::utils::{in_constant, is_copy, qpath_res, span_lint_and_then}; +use crate::utils::{in_constant, is_copy, qpath_res, span_lint_and_then, in_macro}; declare_clippy_lint! { /// **What it does:** Checks for declaration of `const` items which is interior @@ -119,7 +119,7 @@ fn verify_ty_bound<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'tcx>, source: S let (lint, msg, span) = source.lint(); span_lint_and_then(cx, lint, span, msg, |diag| { - if span.from_expansion() { + if in_macro(span) { return; // Don't give suggestions into macros. } match source { diff --git a/src/tools/clippy/clippy_lints/src/non_expressive_names.rs b/src/tools/clippy/clippy_lints/src/non_expressive_names.rs index 5f14fe97afefa..d88962da694fe 100644 --- a/src/tools/clippy/clippy_lints/src/non_expressive_names.rs +++ b/src/tools/clippy/clippy_lints/src/non_expressive_names.rs @@ -1,4 +1,4 @@ -use crate::utils::{span_lint, span_lint_and_then}; +use crate::utils::{span_lint, span_lint_and_then, in_macro}; use rustc_ast::ast::{ Arm, AssocItem, AssocItemKind, Attribute, Block, FnDecl, Item, ItemKind, Local, MacCall, Pat, PatKind, }; @@ -133,7 +133,7 @@ impl<'a, 'tcx, 'b> Visitor<'tcx> for SimilarNamesNameVisitor<'a, 'tcx, 'b> { fn visit_pat(&mut self, pat: &'tcx Pat) { match pat.kind { PatKind::Ident(_, ident, _) => { - if !pat.span.from_expansion() { + if !in_macro(pat.span) { self.check_ident(ident); } }, diff --git a/src/tools/clippy/clippy_lints/src/panic_unimplemented.rs b/src/tools/clippy/clippy_lints/src/panic_unimplemented.rs index 2cd9200ddb252..1179b032c14ad 100644 --- a/src/tools/clippy/clippy_lints/src/panic_unimplemented.rs +++ b/src/tools/clippy/clippy_lints/src/panic_unimplemented.rs @@ -1,4 +1,4 @@ -use crate::utils::{is_direct_expn_of, is_expn_of, match_function_call, paths, span_lint}; +use crate::utils::{is_direct_expn_of, is_expn_of, match_function_call, paths, span_lint, in_macro}; use if_chain::if_chain; use rustc_ast::ast::LitKind; use rustc_hir::{Expr, ExprKind}; @@ -124,9 +124,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PanicUnimplemented { fn get_outer_span(expr: &Expr<'_>) -> Span { if_chain! { - if expr.span.from_expansion(); + if in_macro(expr.span); let first = expr.span.ctxt().outer_expn_data(); - if first.call_site.from_expansion(); + if in_macro(first.call_site); let second = first.call_site.ctxt().outer_expn_data(); then { second.call_site diff --git a/src/tools/clippy/clippy_lints/src/precedence.rs b/src/tools/clippy/clippy_lints/src/precedence.rs index 7dce23dd22306..3b803a319a2b8 100644 --- a/src/tools/clippy/clippy_lints/src/precedence.rs +++ b/src/tools/clippy/clippy_lints/src/precedence.rs @@ -1,4 +1,4 @@ -use crate::utils::{snippet_with_applicability, span_lint_and_sugg}; +use crate::utils::{snippet_with_applicability, span_lint_and_sugg, in_macro}; use rustc_ast::ast::{BinOpKind, Expr, ExprKind, LitKind, UnOp}; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass}; @@ -49,7 +49,7 @@ declare_lint_pass!(Precedence => [PRECEDENCE]); impl EarlyLintPass for Precedence { fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { - if expr.span.from_expansion() { + if in_macro(expr.span) { return; } diff --git a/src/tools/clippy/clippy_lints/src/ranges.rs b/src/tools/clippy/clippy_lints/src/ranges.rs index fcd02a196e7bf..6bd4d625cad94 100644 --- a/src/tools/clippy/clippy_lints/src/ranges.rs +++ b/src/tools/clippy/clippy_lints/src/ranges.rs @@ -10,7 +10,7 @@ use rustc_span::source_map::Spanned; use std::cmp::Ordering; use crate::utils::sugg::Sugg; -use crate::utils::{get_parent_expr, is_integer_const, snippet, snippet_opt, span_lint, span_lint_and_then}; +use crate::utils::{get_parent_expr, is_integer_const, snippet, snippet_opt, span_lint, span_lint_and_then, in_macro}; use crate::utils::{higher, SpanlessEq}; declare_clippy_lint! { @@ -175,7 +175,7 @@ fn check_exclusive_range_plus_one(cx: &LateContext<'_, '_>, expr: &Expr<'_>) { }) = higher::range(cx, expr); if let Some(y) = y_plus_one(cx, end); then { - let span = if expr.span.from_expansion() { + let span = if in_macro(expr.span) { expr.span .ctxt() .outer_expn_data() diff --git a/src/tools/clippy/clippy_lints/src/redundant_clone.rs b/src/tools/clippy/clippy_lints/src/redundant_clone.rs index d563eb886ae7e..ec07688301f8a 100644 --- a/src/tools/clippy/clippy_lints/src/redundant_clone.rs +++ b/src/tools/clippy/clippy_lints/src/redundant_clone.rs @@ -1,7 +1,4 @@ -use crate::utils::{ - fn_has_unsatisfiable_preds, has_drop, is_copy, is_type_diagnostic_item, match_def_path, match_type, paths, - snippet_opt, span_lint_hir, span_lint_hir_and_then, walk_ptrs_ty_depth, -}; +use crate::utils::{fn_has_unsatisfiable_preds, has_drop, is_copy, is_type_diagnostic_item, match_def_path, match_type, paths, snippet_opt, span_lint_hir, span_lint_hir_and_then, walk_ptrs_ty_depth, in_macro}; use if_chain::if_chain; use rustc_data_structures::{fx::FxHashMap, transitive_relation::TransitiveRelation}; use rustc_errors::Applicability; @@ -99,7 +96,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone { for (bb, bbdata) in mir.basic_blocks().iter_enumerated() { let terminator = bbdata.terminator(); - if terminator.source_info.span.from_expansion() { + if in_macro(terminator.source_info.span) { continue; } diff --git a/src/tools/clippy/clippy_lints/src/redundant_static_lifetimes.rs b/src/tools/clippy/clippy_lints/src/redundant_static_lifetimes.rs index c6f57298c2601..531ec0b53f5a1 100644 --- a/src/tools/clippy/clippy_lints/src/redundant_static_lifetimes.rs +++ b/src/tools/clippy/clippy_lints/src/redundant_static_lifetimes.rs @@ -1,4 +1,4 @@ -use crate::utils::{snippet, span_lint_and_then}; +use crate::utils::{snippet, span_lint_and_then, in_macro}; use rustc_ast::ast::{Item, ItemKind, Ty, TyKind}; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass}; @@ -84,7 +84,7 @@ impl RedundantStaticLifetimes { impl EarlyLintPass for RedundantStaticLifetimes { fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { - if !item.span.from_expansion() { + if !in_macro(item.span) { if let ItemKind::Const(_, ref var_type, _) = item.kind { self.visit_type(var_type, cx, "Constants have by default a `'static` lifetime"); // Don't check associated consts because `'static` cannot be elided on those (issue diff --git a/src/tools/clippy/clippy_lints/src/returns.rs b/src/tools/clippy/clippy_lints/src/returns.rs index 3c93974417356..00ccb5010acb9 100644 --- a/src/tools/clippy/clippy_lints/src/returns.rs +++ b/src/tools/clippy/clippy_lints/src/returns.rs @@ -8,7 +8,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::Span; use rustc_span::BytePos; -use crate::utils::{snippet_opt, span_lint_and_sugg, span_lint_and_then}; +use crate::utils::{snippet_opt, span_lint_and_sugg, span_lint_and_then, in_macro}; declare_clippy_lint! { /// **What it does:** Checks for return statements at the end of a block. @@ -123,7 +123,7 @@ impl Return { fn emit_return_lint(cx: &EarlyContext<'_>, ret_span: Span, inner_span: Option, replacement: RetReplacement) { match inner_span { Some(inner_span) => { - if in_external_macro(cx.sess(), inner_span) || inner_span.from_expansion() { + if in_external_macro(cx.sess(), inner_span) || in_macro(inner_span) { return; } @@ -171,7 +171,7 @@ impl EarlyLintPass for Return { if_chain! { if let ast::FnRetTy::Ty(ref ty) = kind.decl().output; if let ast::TyKind::Tup(ref vals) = ty.kind; - if vals.is_empty() && !ty.span.from_expansion() && get_def(span) == get_def(ty.span); + if vals.is_empty() && !in_macro(ty.span) && get_def(span) == get_def(ty.span); then { lint_unneeded_unit_return(cx, ty, span); } @@ -182,7 +182,7 @@ impl EarlyLintPass for Return { if_chain! { if let Some(ref stmt) = block.stmts.last(); if let ast::StmtKind::Expr(ref expr) = stmt.kind; - if is_unit_expr(expr) && !stmt.span.from_expansion(); + if is_unit_expr(expr) && !in_macro(stmt.span); then { let sp = expr.span; span_lint_and_sugg( @@ -201,7 +201,7 @@ impl EarlyLintPass for Return { fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) { match e.kind { ast::ExprKind::Ret(Some(ref expr)) | ast::ExprKind::Break(_, Some(ref expr)) => { - if is_unit_expr(expr) && !expr.span.from_expansion() { + if is_unit_expr(expr) && !in_macro(expr.span) { span_lint_and_sugg( cx, UNUSED_UNIT, @@ -241,7 +241,7 @@ fn attr_is_cfg(attr: &ast::Attribute) -> bool { // get the def site #[must_use] fn get_def(span: Span) -> Option { - if span.from_expansion() { + if in_macro(span) { Some(span.ctxt().outer_expn_data().def_site) } else { None diff --git a/src/tools/clippy/clippy_lints/src/strings.rs b/src/tools/clippy/clippy_lints/src/strings.rs index d8e4bff3d702a..37a1a98d243e6 100644 --- a/src/tools/clippy/clippy_lints/src/strings.rs +++ b/src/tools/clippy/clippy_lints/src/strings.rs @@ -8,7 +8,7 @@ use rustc_span::source_map::Spanned; use if_chain::if_chain; use crate::utils::SpanlessEq; -use crate::utils::{get_parent_expr, is_allowed, is_type_diagnostic_item, span_lint, span_lint_and_sugg, walk_ptrs_ty}; +use crate::utils::{get_parent_expr, is_allowed, is_type_diagnostic_item, span_lint, span_lint_and_sugg, walk_ptrs_ty, in_macro}; declare_clippy_lint! { /// **What it does:** Checks for string appends of the form `x = x + y` (without @@ -187,7 +187,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringLitAsBytes { ); } else if lit_content.as_str().is_ascii() && lit_content.as_str().len() <= MAX_LENGTH_BYTE_STRING_LIT - && !args[0].span.from_expansion() + && !in_macro(args[0].span) { span_lint_and_sugg( cx, diff --git a/src/tools/clippy/clippy_lints/src/trivially_copy_pass_by_ref.rs b/src/tools/clippy/clippy_lints/src/trivially_copy_pass_by_ref.rs index 8e0cb94317aff..b882a9a5b3588 100644 --- a/src/tools/clippy/clippy_lints/src/trivially_copy_pass_by_ref.rs +++ b/src/tools/clippy/clippy_lints/src/trivially_copy_pass_by_ref.rs @@ -1,6 +1,6 @@ use std::cmp; -use crate::utils::{is_copy, is_self_ty, snippet, span_lint_and_sugg}; +use crate::utils::{is_copy, is_self_ty, snippet, span_lint_and_sugg, in_macro}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir as hir; @@ -127,7 +127,7 @@ impl_lint_pass!(TriviallyCopyPassByRef => [TRIVIALLY_COPY_PASS_BY_REF]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TriviallyCopyPassByRef { fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::TraitItem<'_>) { - if item.span.from_expansion() { + if in_macro(item.span) { return; } @@ -145,7 +145,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TriviallyCopyPassByRef { span: Span, hir_id: HirId, ) { - if span.from_expansion() { + if in_macro(span) { return; } diff --git a/src/tools/clippy/clippy_lints/src/try_err.rs b/src/tools/clippy/clippy_lints/src/try_err.rs index 7018fa6804ba7..d4acf4d1f2b46 100644 --- a/src/tools/clippy/clippy_lints/src/try_err.rs +++ b/src/tools/clippy/clippy_lints/src/try_err.rs @@ -1,4 +1,4 @@ -use crate::utils::{match_qpath, paths, snippet, snippet_with_macro_callsite, span_lint_and_sugg}; +use crate::utils::{match_qpath, paths, snippet, snippet_with_macro_callsite, span_lint_and_sugg, in_macro}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::{Arm, Expr, ExprKind, MatchSource}; @@ -69,7 +69,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr { then { let err_type = cx.tables.expr_ty(err_arg); - let origin_snippet = if err_arg.span.from_expansion() { + let origin_snippet = if in_macro(err_arg.span) { snippet_with_macro_callsite(cx, err_arg.span, "_") } else { snippet(cx, err_arg.span, "_") diff --git a/src/tools/clippy/clippy_lints/src/types.rs b/src/tools/clippy/clippy_lints/src/types.rs index d59a2f1bae031..9d4e289c33dd7 100644 --- a/src/tools/clippy/clippy_lints/src/types.rs +++ b/src/tools/clippy/clippy_lints/src/types.rs @@ -27,7 +27,7 @@ use rustc_target::spec::abi::Abi; use rustc_typeck::hir_ty_to_ty; use crate::consts::{constant, Constant}; -use crate::utils::paths; +use crate::utils::{paths, in_macro}; use crate::utils::{ clip, comparisons, differing_macro_contexts, higher, in_constant, indent_of, int_bits, is_type_diagnostic_item, last_path_segment, match_def_path, match_path, method_chain_args, multispan_sugg, numeric_literal::NumericLiteral, @@ -316,7 +316,7 @@ impl Types { /// local bindings should only be checked for the `BORROWED_BOX` lint. #[allow(clippy::too_many_lines)] fn check_ty(&mut self, cx: &LateContext<'_, '_>, hir_ty: &hir::Ty<'_>, is_local: bool) { - if hir_ty.span.from_expansion() { + if in_macro(hir_ty.span) { return; } match hir_ty.kind { @@ -604,7 +604,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetUnitValue { fn check_stmt(&mut self, cx: &LateContext<'a, 'tcx>, stmt: &'tcx Stmt<'_>) { if let StmtKind::Local(ref local) = stmt.kind { if is_unit(cx.tables.pat_ty(&local.pat)) { - if in_external_macro(cx.sess(), stmt.span) || local.pat.span.from_expansion() { + if in_external_macro(cx.sess(), stmt.span) || in_macro(local.pat.span) { return; } if higher::is_from_for_desugar(local) { @@ -683,7 +683,7 @@ declare_lint_pass!(UnitCmp => [UNIT_CMP]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnitCmp { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'tcx>) { - if expr.span.from_expansion() { + if in_macro(expr.span) { if let Some(callee) = expr.span.source_callee() { if let ExpnKind::Macro(MacroKind::Bang, symbol) = callee.kind { if let ExprKind::Binary(ref cmp, ref left, _) = expr.kind { @@ -756,7 +756,7 @@ declare_lint_pass!(UnitArg => [UNIT_ARG]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnitArg { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) { - if expr.span.from_expansion() { + if in_macro(expr.span) { return; } @@ -1412,7 +1412,7 @@ fn fp_ty_mantissa_nbits(typ: Ty<'_>) -> u32 { impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Casts { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) { - if expr.span.from_expansion() { + if in_macro(expr.span) { return; } if let ExprKind::Cast(ref ex, _) = expr.kind { @@ -1698,7 +1698,7 @@ impl<'a, 'tcx> TypeComplexity { } fn check_type(&self, cx: &LateContext<'_, '_>, ty: &hir::Ty<'_>) { - if ty.span.from_expansion() { + if in_macro(ty.span) { return; } let score = { @@ -1800,7 +1800,7 @@ declare_lint_pass!(CharLitAsU8 => [CHAR_LIT_AS_U8]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CharLitAsU8 { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) { if_chain! { - if !expr.span.from_expansion(); + if !in_macro(expr.span); if let ExprKind::Cast(e, _) = &expr.kind; if let ExprKind::Lit(l) = &e.kind; if let LitKind::Char(c) = l.node; @@ -1971,7 +1971,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AbsurdExtremeComparisons { if let ExprKind::Binary(ref cmp, ref lhs, ref rhs) = expr.kind { if let Some((culprit, result)) = detect_absurd_comparison(cx, cmp.node, lhs, rhs) { - if !expr.span.from_expansion() { + if !in_macro(expr.span) { let msg = "this comparison involving the minimum or maximum element for this \ type contains a case that is always true or always false"; diff --git a/src/tools/clippy/clippy_lints/src/unused_self.rs b/src/tools/clippy/clippy_lints/src/unused_self.rs index 3d5e2f9fd2155..d58a7728243e9 100644 --- a/src/tools/clippy/clippy_lints/src/unused_self.rs +++ b/src/tools/clippy/clippy_lints/src/unused_self.rs @@ -6,7 +6,7 @@ use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::hir::map::Map; use rustc_session::{declare_lint_pass, declare_tool_lint}; -use crate::utils::span_lint_and_help; +use crate::utils::{span_lint_and_help, in_macro}; declare_clippy_lint! { /// **What it does:** Checks methods that contain a `self` argument but don't use it @@ -41,7 +41,7 @@ declare_lint_pass!(UnusedSelf => [UNUSED_SELF]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedSelf { fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &ImplItem<'_>) { - if impl_item.span.from_expansion() { + if in_macro(impl_item.span) { return; } let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id); diff --git a/src/tools/clippy/clippy_lints/src/unwrap.rs b/src/tools/clippy/clippy_lints/src/unwrap.rs index a6c7b5d405cda..85e8d7f07982f 100644 --- a/src/tools/clippy/clippy_lints/src/unwrap.rs +++ b/src/tools/clippy/clippy_lints/src/unwrap.rs @@ -1,7 +1,4 @@ -use crate::utils::{ - differing_macro_contexts, higher::if_block, is_type_diagnostic_item, span_lint_and_then, - usage::is_potentially_mutated, -}; +use crate::utils::{differing_macro_contexts, higher::if_block, is_type_diagnostic_item, span_lint_and_then, usage::is_potentially_mutated, in_macro}; use if_chain::if_chain; use rustc_hir::intravisit::{walk_expr, walk_fn, FnKind, NestedVisitorMap, Visitor}; use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, HirId, Path, QPath, UnOp}; @@ -219,7 +216,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Unwrap { span: Span, fn_id: HirId, ) { - if span.from_expansion() { + if in_macro(span) { return; } diff --git a/src/tools/clippy/clippy_lints/src/useless_conversion.rs b/src/tools/clippy/clippy_lints/src/useless_conversion.rs index 78d249482d53d..315a9f9d9f69c 100644 --- a/src/tools/clippy/clippy_lints/src/useless_conversion.rs +++ b/src/tools/clippy/clippy_lints/src/useless_conversion.rs @@ -1,7 +1,4 @@ -use crate::utils::{ - is_type_diagnostic_item, match_def_path, match_trait_method, paths, snippet, snippet_with_macro_callsite, - span_lint_and_help, span_lint_and_sugg, -}; +use crate::utils::{is_type_diagnostic_item, match_def_path, match_trait_method, paths, snippet, snippet_with_macro_callsite, span_lint_and_help, span_lint_and_sugg, in_macro}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind, HirId, MatchSource}; @@ -42,7 +39,7 @@ impl_lint_pass!(UselessConversion => [USELESS_CONVERSION]); #[allow(clippy::too_many_lines)] impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessConversion { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) { - if e.span.from_expansion() { + if in_macro(e.span) { return; } diff --git a/src/tools/clippy/clippy_lints/src/utils/mod.rs b/src/tools/clippy/clippy_lints/src/utils/mod.rs index 60ab19e71f5e4..837fe00f013f6 100644 --- a/src/tools/clippy/clippy_lints/src/utils/mod.rs +++ b/src/tools/clippy/clippy_lints/src/utils/mod.rs @@ -43,7 +43,7 @@ use rustc_infer::infer::TyCtxtInferExt; use rustc_lint::{LateContext, Level, Lint, LintContext}; use rustc_middle::hir::map::Map; use rustc_middle::ty::{self, layout::IntegerExt, subst::GenericArg, Ty, TyCtxt, TypeFoldable}; -use rustc_span::hygiene::{ExpnKind, MacroKind}; +use rustc_span::hygiene::{ExpnKind, MacroKind, ClosestAstOrMacro}; use rustc_span::source_map::original_sp; use rustc_span::symbol::{self, kw, Symbol}; use rustc_span::{BytePos, Pos, Span, DUMMY_SP}; @@ -58,7 +58,11 @@ use crate::reexport::Name; /// from a macro and one isn't). #[must_use] pub fn differing_macro_contexts(lhs: Span, rhs: Span) -> bool { - rhs.ctxt() != lhs.ctxt() + if lhs.ctxt() == rhs.ctxt() { + return false + }; + + lhs.ctxt().outer_expn().closest_ast_or_macro() != rhs.ctxt().outer_expn().closest_ast_or_macro() } /// Returns `true` if the given `NodeId` is inside a constant context @@ -101,14 +105,9 @@ pub fn in_constant(cx: &LateContext<'_, '_>, id: HirId) -> bool { /// Returns `true` if this `span` was expanded by any macro. #[must_use] pub fn in_macro(span: Span) -> bool { - if span.from_expansion() { - if let ExpnKind::Desugaring(..) = span.ctxt().outer_expn_data().kind { - false - } else { - true - } - } else { - false + match span.ctxt().outer_expn().closest_ast_or_macro() { + ClosestAstOrMacro::Expn(_) => true, + ClosestAstOrMacro::None => false } } // If the snippet is empty, it's an attribute that was inserted during macro @@ -380,7 +379,7 @@ pub fn method_calls<'tcx>( let mut current = expr; for _ in 0..max_depth { if let ExprKind::MethodCall(path, span, args, _) = ¤t.kind { - if args.iter().any(|e| e.span.from_expansion()) { + if args.iter().any(|e| in_macro(e.span)) { break; } method_names.push(path.ident.name); @@ -408,7 +407,7 @@ pub fn method_chain_args<'a>(expr: &'a Expr<'_>, methods: &[&str]) -> Option first if let ExprKind::MethodCall(ref path, _, ref args, _) = current.kind { if path.ident.name.as_str() == *method_name { - if args.iter().any(|e| e.span.from_expansion()) { + if args.iter().any(|e| in_macro(e.span)) { return None; } matched.push(&**args); // build up `matched` backwards @@ -505,7 +504,7 @@ pub fn snippet_with_applicability<'a, T: LintContext>( default: &'a str, applicability: &mut Applicability, ) -> Cow<'a, str> { - if *applicability != Applicability::Unspecified && span.from_expansion() { + if *applicability != Applicability::Unspecified && in_macro(span) { *applicability = Applicability::MaybeIncorrect; } snippet_opt(cx, span).map_or_else( @@ -661,7 +660,7 @@ pub fn expr_block<'a, T: LintContext>( ) -> Cow<'a, str> { let code = snippet_block(cx, expr.span, default, indent_relative_to); let string = option.unwrap_or_default(); - if expr.span.from_expansion() { + if in_macro(expr.span) { Cow::Owned(format!("{{ {} }}", snippet_with_macro_callsite(cx, expr.span, default))) } else if let ExprKind::Block(_, _) = expr.kind { Cow::Owned(format!("{}{}", code, string)) @@ -833,7 +832,7 @@ pub fn is_adjusted(cx: &LateContext<'_, '_>, e: &Expr<'_>) -> bool { #[must_use] pub fn is_expn_of(mut span: Span, name: &str) -> Option { loop { - if span.from_expansion() { + if in_macro(span) { let data = span.ctxt().outer_expn_data(); let new_span = data.call_site; @@ -861,7 +860,7 @@ pub fn is_expn_of(mut span: Span, name: &str) -> Option { /// `is_direct_expn_of`. #[must_use] pub fn is_direct_expn_of(span: Span, name: &str) -> Option { - if span.from_expansion() { + if in_macro(span) { let data = span.ctxt().outer_expn_data(); let new_span = data.call_site; diff --git a/src/tools/clippy/clippy_lints/src/utils/sugg.rs b/src/tools/clippy/clippy_lints/src/utils/sugg.rs index 73758b7eeb7eb..7b96fb600b69f 100644 --- a/src/tools/clippy/clippy_lints/src/utils/sugg.rs +++ b/src/tools/clippy/clippy_lints/src/utils/sugg.rs @@ -1,7 +1,7 @@ //! Contains utility functions to generate suggestions. #![deny(clippy::missing_docs_in_private_items)] -use crate::utils::{higher, snippet, snippet_opt, snippet_with_macro_callsite}; +use crate::utils::{higher, snippet, snippet_opt, snippet_with_macro_callsite, in_macro}; use rustc_ast::util::parser::AssocOp; use rustc_ast::{ast, token}; use rustc_ast_pretty::pprust::token_kind_to_string; @@ -65,7 +65,7 @@ impl<'a> Sugg<'a> { default: &'a str, applicability: &mut Applicability, ) -> Self { - if *applicability != Applicability::Unspecified && expr.span.from_expansion() { + if *applicability != Applicability::Unspecified && in_macro(expr.span) { *applicability = Applicability::MaybeIncorrect; } Self::hir_opt(cx, expr).unwrap_or_else(|| { diff --git a/src/tools/clippy/doc/adding_lints.md b/src/tools/clippy/doc/adding_lints.md index 8092be277cca0..33dca8f4855a4 100644 --- a/src/tools/clippy/doc/adding_lints.md +++ b/src/tools/clippy/doc/adding_lints.md @@ -462,7 +462,7 @@ Here are some pointers to things you are likely going to need for every lint: is already in here (`implements_trait`, `match_path`, `snippet`, etc) * [Clippy diagnostics][diagnostics] * [The `if_chain` macro][if_chain] -* [`from_expansion`][from_expansion] and [`in_external_macro`][in_external_macro] +* [`in_macro`][in_macro] and [`in_external_macro`][in_external_macro] * [`Span`][span] * [`Applicability`][applicability] * [Common tools for writing lints](common_tools_writing_lints.md) helps with common operations @@ -487,7 +487,7 @@ don't hesitate to ask on [Discord] or in the issue/PR. [utils]: https://github.com/rust-lang/rust-clippy/blob/master/clippy_lints/src/utils/mod.rs [if_chain]: https://docs.rs/if_chain/*/if_chain/ -[from_expansion]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/struct.Span.html#method.from_expansion +[in_macro]: https://github.com/rust-lang/rust-clippy/blob/742706511c9f33c6a0d4380392e513e5249057e3/clippy_lints/src/utils/mod.rs#L101-L113 [in_external_macro]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/lint/fn.in_external_macro.html [span]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/struct.Span.html [applicability]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/enum.Applicability.html