From 7eb8018554cc0b8e3438b995f70cdd2e30151526 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sat, 11 May 2019 20:40:05 -0700 Subject: [PATCH 1/2] Rename in_macro to in_macro_or_desugar --- clippy_lints/src/assertions_on_constants.rs | 10 +++++----- clippy_lints/src/attrs.rs | 4 ++-- clippy_lints/src/block_in_if_condition.rs | 6 +++--- clippy_lints/src/booleans.rs | 7 ++++--- clippy_lints/src/cognitive_complexity.rs | 4 ++-- clippy_lints/src/collapsible_if.rs | 8 +++++--- clippy_lints/src/const_static_lifetime.rs | 4 ++-- clippy_lints/src/copies.rs | 4 ++-- clippy_lints/src/double_parens.rs | 4 ++-- clippy_lints/src/enum_variants.rs | 4 ++-- clippy_lints/src/eq_op.rs | 4 ++-- clippy_lints/src/erasing_op.rs | 4 ++-- clippy_lints/src/format.rs | 5 +++-- clippy_lints/src/formatting.rs | 8 ++++---- clippy_lints/src/identity_conversion.rs | 6 ++++-- clippy_lints/src/identity_op.rs | 4 ++-- clippy_lints/src/implicit_return.rs | 4 ++-- clippy_lints/src/items_after_statements.rs | 6 +++--- clippy_lints/src/len_zero.rs | 8 +++++--- clippy_lints/src/loops.rs | 6 +++--- clippy_lints/src/map_clone.rs | 5 +++-- clippy_lints/src/map_unit_fn.rs | 4 ++-- clippy_lints/src/matches.rs | 6 +++--- clippy_lints/src/methods/mod.rs | 4 ++-- clippy_lints/src/misc.rs | 6 +++--- clippy_lints/src/missing_doc.rs | 4 ++-- clippy_lints/src/needless_bool.rs | 4 ++-- clippy_lints/src/needless_borrow.rs | 6 +++--- clippy_lints/src/needless_borrowed_ref.rs | 4 ++-- clippy_lints/src/needless_continue.rs | 4 ++-- clippy_lints/src/needless_pass_by_value.rs | 6 +++--- clippy_lints/src/no_effect.rs | 8 ++++---- clippy_lints/src/non_copy_const.rs | 4 ++-- clippy_lints/src/precedence.rs | 4 ++-- clippy_lints/src/redundant_clone.rs | 4 ++-- clippy_lints/src/returns.rs | 10 +++++----- clippy_lints/src/strings.rs | 4 ++-- clippy_lints/src/trivially_copy_pass_by_ref.rs | 6 +++--- clippy_lints/src/types.rs | 18 +++++++++--------- clippy_lints/src/unused_label.rs | 4 ++-- clippy_lints/src/unwrap.rs | 6 ++++-- clippy_lints/src/utils/mod.rs | 10 +++++----- clippy_lints/src/utils/sugg.rs | 4 ++-- doc/adding_lints.md | 4 ++-- tests/ui/used_underscore_binding.rs | 4 ++-- 45 files changed, 132 insertions(+), 121 deletions(-) diff --git a/clippy_lints/src/assertions_on_constants.rs b/clippy_lints/src/assertions_on_constants.rs index 36ed835e2581..0f0c0dbc15d0 100644 --- a/clippy_lints/src/assertions_on_constants.rs +++ b/clippy_lints/src/assertions_on_constants.rs @@ -5,7 +5,7 @@ use rustc::{declare_lint_pass, declare_tool_lint}; use syntax_pos::Span; use crate::consts::{constant, Constant}; -use crate::utils::{in_macro, is_direct_expn_of, span_help_and_lint}; +use crate::utils::{in_macro_or_desugar, is_direct_expn_of, span_help_and_lint}; declare_clippy_lint! { /// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls. @@ -34,16 +34,16 @@ declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) { let mut is_debug_assert = false; - let debug_assert_not_in_macro = |span: Span| { + let debug_assert_not_in_macro_or_desugar = |span: Span| { is_debug_assert = true; // Check that `debug_assert!` itself is not inside a macro - !in_macro(span) + !in_macro_or_desugar(span) }; if_chain! { if let Some(assert_span) = is_direct_expn_of(e.span, "assert"); - if !in_macro(assert_span) + if !in_macro_or_desugar(assert_span) || is_direct_expn_of(assert_span, "debug_assert") - .map_or(false, debug_assert_not_in_macro); + .map_or(false, debug_assert_not_in_macro_or_desugar); if let ExprKind::Unary(_, ref lit) = e.node; if let Some(bool_const) = constant(cx, cx.tables, lit); then { diff --git a/clippy_lints/src/attrs.rs b/clippy_lints/src/attrs.rs index 58cd2bca9597..9a9279a409ae 100644 --- a/clippy_lints/src/attrs.rs +++ b/clippy_lints/src/attrs.rs @@ -2,7 +2,7 @@ use crate::reexport::*; use crate::utils::{ - in_macro, is_present_in_source, last_line_of_span, paths, snippet_opt, span_lint, span_lint_and_sugg, + in_macro_or_desugar, is_present_in_source, last_line_of_span, paths, snippet_opt, span_lint, span_lint_and_sugg, span_lint_and_then, without_block_comments, }; use if_chain::if_chain; @@ -408,7 +408,7 @@ fn is_relevant_expr(cx: &LateContext<'_, '_>, tables: &ty::TypeckTables<'_>, exp } fn check_attrs(cx: &LateContext<'_, '_>, span: Span, name: Name, attrs: &[Attribute]) { - if in_macro(span) { + if in_macro_or_desugar(span) { return; } diff --git a/clippy_lints/src/block_in_if_condition.rs b/clippy_lints/src/block_in_if_condition.rs index 05d3ea6b2748..786cba9fc7d8 100644 --- a/clippy_lints/src/block_in_if_condition.rs +++ b/clippy_lints/src/block_in_if_condition.rs @@ -54,7 +54,7 @@ impl<'a, 'tcx: 'a> Visitor<'tcx> for ExVisitor<'a, 'tcx> { if let ExprKind::Closure(_, _, eid, _, _) = expr.node { let body = self.cx.tcx.hir().body(eid); let ex = &body.value; - if matches!(ex.node, ExprKind::Block(_, _)) && !in_macro(body.value.span) { + if matches!(ex.node, ExprKind::Block(_, _)) && !in_macro_or_desugar(body.value.span) { self.found_block = Some(ex); return; } @@ -79,7 +79,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition { if let Some(ex) = &block.expr { // don't dig into the expression here, just suggest that they remove // the block - if in_macro(expr.span) || differing_macro_contexts(expr.span, ex.span) { + if in_macro_or_desugar(expr.span) || differing_macro_contexts(expr.span, ex.span) { return; } span_help_and_lint( @@ -96,7 +96,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition { } } else { let span = block.expr.as_ref().map_or_else(|| block.stmts[0].span, |e| e.span); - if in_macro(span) || differing_macro_contexts(expr.span, span) { + if in_macro_or_desugar(span) || differing_macro_contexts(expr.span, span) { return; } // move block higher diff --git a/clippy_lints/src/booleans.rs b/clippy_lints/src/booleans.rs index 779cb3cf90f8..6dedc7d8775d 100644 --- a/clippy_lints/src/booleans.rs +++ b/clippy_lints/src/booleans.rs @@ -1,5 +1,6 @@ use crate::utils::{ - get_trait_def_id, implements_trait, in_macro, match_type, paths, snippet_opt, span_lint_and_then, SpanlessEq, + get_trait_def_id, implements_trait, in_macro_or_desugar, match_type, paths, snippet_opt, span_lint_and_then, + SpanlessEq, }; use rustc::hir::intravisit::*; use rustc::hir::*; @@ -93,7 +94,7 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> { fn run(&mut self, e: &'v Expr) -> Result { // prevent folding of `cfg!` macros and the like - if !in_macro(e.span) { + if !in_macro_or_desugar(e.span) { match &e.node { ExprKind::Unary(UnNot, inner) => return Ok(Bool::Not(box self.run(inner)?)), ExprKind::Binary(binop, lhs, rhs) => match &binop.node { @@ -441,7 +442,7 @@ impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> { impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> { fn visit_expr(&mut self, e: &'tcx Expr) { - if in_macro(e.span) { + if in_macro_or_desugar(e.span) { return; } match &e.node { diff --git a/clippy_lints/src/cognitive_complexity.rs b/clippy_lints/src/cognitive_complexity.rs index ccf6d1830062..9374e04bc11e 100644 --- a/clippy_lints/src/cognitive_complexity.rs +++ b/clippy_lints/src/cognitive_complexity.rs @@ -9,7 +9,7 @@ use rustc::{declare_tool_lint, impl_lint_pass}; use syntax::ast::Attribute; use syntax::source_map::Span; -use crate::utils::{in_macro, is_allowed, match_type, paths, span_help_and_lint, LimitStack}; +use crate::utils::{in_macro_or_desugar, is_allowed, match_type, paths, span_help_and_lint, LimitStack}; declare_clippy_lint! { /// **What it does:** Checks for methods with high cognitive complexity. @@ -42,7 +42,7 @@ impl_lint_pass!(CognitiveComplexity => [COGNITIVE_COMPLEXITY]); impl CognitiveComplexity { fn check<'a, 'tcx: 'a>(&mut self, cx: &'a LateContext<'a, 'tcx>, body: &'tcx Body, span: Span) { - if in_macro(span) { + if in_macro_or_desugar(span) { return; } diff --git a/clippy_lints/src/collapsible_if.rs b/clippy_lints/src/collapsible_if.rs index c1d39716083e..00d7bf3b5a9e 100644 --- a/clippy_lints/src/collapsible_if.rs +++ b/clippy_lints/src/collapsible_if.rs @@ -18,7 +18,9 @@ use rustc::{declare_lint_pass, declare_tool_lint}; use syntax::ast; use crate::utils::sugg::Sugg; -use crate::utils::{in_macro, snippet_block, snippet_block_with_applicability, span_lint_and_sugg, span_lint_and_then}; +use crate::utils::{ + in_macro_or_desugar, snippet_block, snippet_block_with_applicability, span_lint_and_sugg, span_lint_and_then, +}; use rustc_errors::Applicability; declare_clippy_lint! { @@ -75,7 +77,7 @@ declare_lint_pass!(CollapsibleIf => [COLLAPSIBLE_IF]); impl EarlyLintPass for CollapsibleIf { fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) { - if !in_macro(expr.span) { + if !in_macro_or_desugar(expr.span) { check_if(cx, expr) } } @@ -110,7 +112,7 @@ fn check_collapsible_maybe_if_let(cx: &EarlyContext<'_>, else_: &ast::Expr) { if let ast::ExprKind::Block(ref block, _) = else_.node; if !block_starts_with_comment(cx, block); if let Some(else_) = expr_block(block); - if !in_macro(else_.span); + if !in_macro_or_desugar(else_.span); then { match else_.node { ast::ExprKind::If(..) | ast::ExprKind::IfLet(..) => { diff --git a/clippy_lints/src/const_static_lifetime.rs b/clippy_lints/src/const_static_lifetime.rs index 50e1781d2cb5..a69771b8b809 100644 --- a/clippy_lints/src/const_static_lifetime.rs +++ b/clippy_lints/src/const_static_lifetime.rs @@ -1,4 +1,4 @@ -use crate::utils::{in_macro, snippet, span_lint_and_then}; +use crate::utils::{in_macro_or_desugar, snippet, span_lint_and_then}; use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; use rustc::{declare_lint_pass, declare_tool_lint}; use rustc_errors::Applicability; @@ -81,7 +81,7 @@ impl StaticConst { impl EarlyLintPass for StaticConst { fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { - if !in_macro(item.span) { + if !in_macro_or_desugar(item.span) { // Match only constants... if let ItemKind::Const(ref var_type, _) = item.node { self.visit_type(var_type, cx); diff --git a/clippy_lints/src/copies.rs b/clippy_lints/src/copies.rs index c08d6851aa01..d85cf97a6bdd 100644 --- a/clippy_lints/src/copies.rs +++ b/clippy_lints/src/copies.rs @@ -1,4 +1,4 @@ -use crate::utils::{get_parent_expr, higher, in_macro, snippet, span_lint_and_then, span_note_and_lint}; +use crate::utils::{get_parent_expr, higher, in_macro_or_desugar, snippet, span_lint_and_then, span_note_and_lint}; use crate::utils::{SpanlessEq, SpanlessHash}; use rustc::hir::*; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; @@ -107,7 +107,7 @@ declare_lint_pass!(CopyAndPaste => [IFS_SAME_COND, IF_SAME_THEN_ELSE, MATCH_SAME impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CopyAndPaste { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { - if !in_macro(expr.span) { + if !in_macro_or_desugar(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/clippy_lints/src/double_parens.rs b/clippy_lints/src/double_parens.rs index 12f67b84ca28..f39ea6a3cc5a 100644 --- a/clippy_lints/src/double_parens.rs +++ b/clippy_lints/src/double_parens.rs @@ -1,4 +1,4 @@ -use crate::utils::{in_macro, span_lint}; +use crate::utils::{in_macro_or_desugar, span_lint}; use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; use rustc::{declare_lint_pass, declare_tool_lint}; use syntax::ast::*; @@ -26,7 +26,7 @@ declare_lint_pass!(DoubleParens => [DOUBLE_PARENS]); impl EarlyLintPass for DoubleParens { fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { - if in_macro(expr.span) { + if in_macro_or_desugar(expr.span) { return; } diff --git a/clippy_lints/src/enum_variants.rs b/clippy_lints/src/enum_variants.rs index 27229710641d..4479349aaf4a 100644 --- a/clippy_lints/src/enum_variants.rs +++ b/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, in_macro, is_present_in_source}; +use crate::utils::{camel_case, in_macro_or_desugar, is_present_in_source}; use crate::utils::{span_help_and_lint, span_lint}; use rustc::lint::{EarlyContext, EarlyLintPass, Lint, LintArray, LintPass}; use rustc::{declare_tool_lint, impl_lint_pass}; @@ -244,7 +244,7 @@ impl EarlyLintPass for EnumVariantNames { let item_name = item.ident.as_str(); let item_name_chars = item_name.chars().count(); let item_camel = to_camel_case(&item_name); - if !in_macro(item.span) && is_present_in_source(cx, item.span) { + if !in_macro_or_desugar(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/clippy_lints/src/eq_op.rs b/clippy_lints/src/eq_op.rs index ea09f82642f7..08c0b36855e2 100644 --- a/clippy_lints/src/eq_op.rs +++ b/clippy_lints/src/eq_op.rs @@ -1,5 +1,5 @@ use crate::utils::{ - implements_trait, in_macro, is_copy, multispan_sugg, snippet, span_lint, span_lint_and_then, SpanlessEq, + implements_trait, in_macro_or_desugar, is_copy, multispan_sugg, snippet, span_lint, span_lint_and_then, SpanlessEq, }; use rustc::hir::*; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; @@ -52,7 +52,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.node { - if in_macro(e.span) { + if in_macro_or_desugar(e.span) { return; } if is_valid_operator(op) && SpanlessEq::new(cx).ignore_fn().eq_expr(left, right) { diff --git a/clippy_lints/src/erasing_op.rs b/clippy_lints/src/erasing_op.rs index 7f3549be64bb..3ac2f90ce01b 100644 --- a/clippy_lints/src/erasing_op.rs +++ b/clippy_lints/src/erasing_op.rs @@ -4,7 +4,7 @@ use rustc::{declare_lint_pass, declare_tool_lint}; use syntax::source_map::Span; use crate::consts::{constant_simple, Constant}; -use crate::utils::{in_macro, span_lint}; +use crate::utils::{in_macro_or_desugar, span_lint}; 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 in_macro(e.span) { + if in_macro_or_desugar(e.span) { return; } if let ExprKind::Binary(ref cmp, ref left, ref right) = e.node { diff --git a/clippy_lints/src/format.rs b/clippy_lints/src/format.rs index 95389fa3c81e..ace059f84288 100644 --- a/clippy_lints/src/format.rs +++ b/clippy_lints/src/format.rs @@ -1,6 +1,7 @@ use crate::utils::paths; use crate::utils::{ - in_macro, is_expn_of, last_path_segment, match_type, resolve_node, snippet, span_lint_and_then, walk_ptrs_ty, + in_macro_or_desugar, is_expn_of, last_path_segment, match_type, resolve_node, snippet, span_lint_and_then, + walk_ptrs_ty, }; use if_chain::if_chain; use rustc::hir::*; @@ -38,7 +39,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) { if let Some(span) = is_expn_of(expr.span, "format") { - if in_macro(span) { + if in_macro_or_desugar(span) { return; } match expr.node { diff --git a/clippy_lints/src/formatting.rs b/clippy_lints/src/formatting.rs index e29a8c3702b1..5916a0659500 100644 --- a/clippy_lints/src/formatting.rs +++ b/clippy_lints/src/formatting.rs @@ -1,4 +1,4 @@ -use crate::utils::{differing_macro_contexts, in_macro, snippet_opt, span_note_and_lint}; +use crate::utils::{differing_macro_contexts, in_macro_or_desugar, snippet_opt, span_note_and_lint}; use if_chain::if_chain; use rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintPass}; use rustc::{declare_lint_pass, declare_tool_lint}; @@ -108,7 +108,7 @@ impl EarlyLintPass for Formatting { /// Implementation of the `SUSPICIOUS_ASSIGNMENT_FORMATTING` lint. fn check_assign(cx: &EarlyContext<'_>, expr: &ast::Expr) { if let ast::ExprKind::Assign(ref lhs, ref rhs) = expr.node { - if !differing_macro_contexts(lhs.span, rhs.span) && !in_macro(lhs.span) { + if !differing_macro_contexts(lhs.span, rhs.span) && !in_macro_or_desugar(lhs.span) { let eq_span = lhs.span.between(rhs.span); if let ast::ExprKind::Unary(op, ref sub_rhs) = rhs.node { if let Some(eq_snippet) = snippet_opt(cx, eq_span) { @@ -140,7 +140,7 @@ fn check_else(cx: &EarlyContext<'_>, expr: &ast::Expr) { if let Some((then, &Some(ref else_))) = unsugar_if(expr); if is_block(else_) || unsugar_if(else_).is_some(); if !differing_macro_contexts(then.span, else_.span); - if !in_macro(then.span) && !in_external_macro(cx.sess, expr.span); + if !in_macro_or_desugar(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; @@ -206,7 +206,7 @@ fn check_array(cx: &EarlyContext<'_>, expr: &ast::Expr) { fn check_missing_else(cx: &EarlyContext<'_>, first: &ast::Expr, second: &ast::Expr) { if !differing_macro_contexts(first.span, second.span) - && !in_macro(first.span) + && !in_macro_or_desugar(first.span) && unsugar_if(first).is_some() && (is_block(second) || unsugar_if(second).is_some()) { diff --git a/clippy_lints/src/identity_conversion.rs b/clippy_lints/src/identity_conversion.rs index 0941ed513fc4..61d287fbea62 100644 --- a/clippy_lints/src/identity_conversion.rs +++ b/clippy_lints/src/identity_conversion.rs @@ -1,4 +1,6 @@ -use crate::utils::{in_macro, match_trait_method, same_tys, snippet, snippet_with_macro_callsite, span_lint_and_then}; +use crate::utils::{ + in_macro_or_desugar, match_trait_method, same_tys, snippet, snippet_with_macro_callsite, span_lint_and_then, +}; use crate::utils::{paths, resolve_node}; use rustc::hir::*; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; @@ -31,7 +33,7 @@ impl_lint_pass!(IdentityConversion => [IDENTITY_CONVERSION]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) { - if in_macro(e.span) { + if in_macro_or_desugar(e.span) { return; } diff --git a/clippy_lints/src/identity_op.rs b/clippy_lints/src/identity_op.rs index cc023a5a4598..b3ab46781adc 100644 --- a/clippy_lints/src/identity_op.rs +++ b/clippy_lints/src/identity_op.rs @@ -5,7 +5,7 @@ use rustc::{declare_lint_pass, declare_tool_lint}; use syntax::source_map::Span; use crate::consts::{constant_simple, Constant}; -use crate::utils::{clip, in_macro, snippet, span_lint, unsext}; +use crate::utils::{clip, in_macro_or_desugar, snippet, span_lint, unsext}; declare_clippy_lint! { /// **What it does:** Checks for identity operations, e.g., `x + 0`. @@ -28,7 +28,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 in_macro(e.span) { + if in_macro_or_desugar(e.span) { return; } if let ExprKind::Binary(ref cmp, ref left, ref right) = e.node { diff --git a/clippy_lints/src/implicit_return.rs b/clippy_lints/src/implicit_return.rs index 8e0fa1bf2e26..dc5a3a6b2f73 100644 --- a/clippy_lints/src/implicit_return.rs +++ b/clippy_lints/src/implicit_return.rs @@ -1,4 +1,4 @@ -use crate::utils::{in_macro, is_expn_of, snippet_opt, span_lint_and_then}; +use crate::utils::{in_macro_or_desugar, is_expn_of, snippet_opt, span_lint_and_then}; use rustc::hir::{intravisit::FnKind, Body, ExprKind, FnDecl, HirId, MatchSource}; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_lint_pass, declare_tool_lint}; @@ -118,7 +118,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() && !in_macro(span) { + if !mir.return_ty().is_unit() && !in_macro_or_desugar(span) { Self::expr_match(cx, &body.value); } } diff --git a/clippy_lints/src/items_after_statements.rs b/clippy_lints/src/items_after_statements.rs index 779bac30e0fa..226809a2e28b 100644 --- a/clippy_lints/src/items_after_statements.rs +++ b/clippy_lints/src/items_after_statements.rs @@ -1,6 +1,6 @@ //! lint when items are used after statements -use crate::utils::{in_macro, span_lint}; +use crate::utils::{in_macro_or_desugar, span_lint}; use matches::matches; use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; use rustc::{declare_lint_pass, declare_tool_lint}; @@ -38,7 +38,7 @@ declare_lint_pass!(ItemsAfterStatements => [ITEMS_AFTER_STATEMENTS]); impl EarlyLintPass for ItemsAfterStatements { fn check_block(&mut self, cx: &EarlyContext<'_>, item: &Block) { - if in_macro(item.span) { + if in_macro_or_desugar(item.span) { return; } @@ -52,7 +52,7 @@ impl EarlyLintPass for ItemsAfterStatements { // lint on all further items for stmt in stmts { if let StmtKind::Item(ref it) = *stmt { - if in_macro(it.span) { + if in_macro_or_desugar(it.span) { return; } if let ItemKind::MacroDef(..) = it.node { diff --git a/clippy_lints/src/len_zero.rs b/clippy_lints/src/len_zero.rs index 191d27a1b8cf..f464ebd03ddf 100644 --- a/clippy_lints/src/len_zero.rs +++ b/clippy_lints/src/len_zero.rs @@ -1,4 +1,6 @@ -use crate::utils::{get_item_name, in_macro, snippet_with_applicability, span_lint, span_lint_and_sugg, walk_ptrs_ty}; +use crate::utils::{ + get_item_name, in_macro_or_desugar, snippet_with_applicability, span_lint, span_lint_and_sugg, walk_ptrs_ty, +}; use rustc::hir::def_id::DefId; use rustc::hir::*; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; @@ -73,7 +75,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 in_macro(item.span) { + if in_macro_or_desugar(item.span) { return; } @@ -85,7 +87,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LenZero { } fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { - if in_macro(expr.span) { + if in_macro_or_desugar(expr.span) { return; } diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index c67edc00e6b3..f47d2e45d6fa 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -11,7 +11,7 @@ use rustc::{declare_lint_pass, declare_tool_lint}; // use rustc::middle::region::CodeExtent; use crate::consts::{constant, Constant}; use crate::utils::usage::mutated_variables; -use crate::utils::{in_macro, sext, sugg}; +use crate::utils::{in_macro_or_desugar, sext, sugg}; use rustc::middle::expr_use_visitor::*; use rustc::middle::mem_categorization::cmt_; use rustc::middle::mem_categorization::Categorization; @@ -464,7 +464,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Loops { #[allow(clippy::too_many_lines)] fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { // we don't want to check expanded macros - if in_macro(expr.span) { + if in_macro_or_desugar(expr.span) { return; } @@ -1057,7 +1057,7 @@ fn check_for_loop_range<'a, 'tcx>( body: &'tcx Expr, expr: &'tcx Expr, ) { - if in_macro(expr.span) { + if in_macro_or_desugar(expr.span) { return; } diff --git a/clippy_lints/src/map_clone.rs b/clippy_lints/src/map_clone.rs index d84036d459ae..df10336d6d97 100644 --- a/clippy_lints/src/map_clone.rs +++ b/clippy_lints/src/map_clone.rs @@ -1,6 +1,7 @@ use crate::utils::paths; use crate::utils::{ - in_macro, is_copy, match_trait_method, match_type, remove_blocks, snippet_with_applicability, span_lint_and_sugg, + in_macro_or_desugar, is_copy, match_trait_method, match_type, remove_blocks, snippet_with_applicability, + span_lint_and_sugg, }; use if_chain::if_chain; use rustc::hir; @@ -43,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 in_macro(e.span) { + if in_macro_or_desugar(e.span) { return; } diff --git a/clippy_lints/src/map_unit_fn.rs b/clippy_lints/src/map_unit_fn.rs index eae43cfa8240..2e97f44b317f 100644 --- a/clippy_lints/src/map_unit_fn.rs +++ b/clippy_lints/src/map_unit_fn.rs @@ -1,5 +1,5 @@ use crate::utils::paths; -use crate::utils::{in_macro, iter_input_pats, match_type, method_chain_args, snippet, span_lint_and_then}; +use crate::utils::{in_macro_or_desugar, iter_input_pats, match_type, method_chain_args, snippet, span_lint_and_then}; use if_chain::if_chain; use rustc::hir; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; @@ -240,7 +240,7 @@ fn lint_map_unit_fn(cx: &LateContext<'_, '_>, stmt: &hir::Stmt, expr: &hir::Expr impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapUnit { fn check_stmt(&mut self, cx: &LateContext<'_, '_>, stmt: &hir::Stmt) { - if in_macro(stmt.span) { + if in_macro_or_desugar(stmt.span) { return; } diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index 21c3aded6c6b..cb33ad4d974b 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -2,8 +2,8 @@ use crate::consts::{constant, Constant}; use crate::utils::paths; use crate::utils::sugg::Sugg; use crate::utils::{ - expr_block, in_macro, is_allowed, is_expn_of, match_qpath, match_type, multispan_sugg, remove_blocks, snippet, - snippet_with_applicability, span_lint_and_sugg, span_lint_and_then, span_note_and_lint, walk_ptrs_ty, + expr_block, in_macro_or_desugar, is_allowed, is_expn_of, match_qpath, match_type, multispan_sugg, remove_blocks, + snippet, snippet_with_applicability, span_lint_and_sugg, span_lint_and_then, span_note_and_lint, walk_ptrs_ty, }; use if_chain::if_chain; use rustc::hir::def::CtorKind; @@ -589,7 +589,7 @@ fn check_match_ref_pats(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr: })); span_lint_and_then(cx, MATCH_REF_PATS, expr.span, title, |db| { - if !in_macro(expr.span) { + if !in_macro_or_desugar(expr.span) { multispan_sugg(db, msg.to_owned(), suggs); } }); diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index f8cde663d899..c96d05cef2f3 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -20,7 +20,7 @@ use syntax::symbol::LocalInternedString; use crate::utils::paths; use crate::utils::sugg; use crate::utils::{ - get_arg_name, get_parent_expr, get_trait_def_id, has_iter_method, implements_trait, in_macro, is_copy, + get_arg_name, get_parent_expr, get_trait_def_id, has_iter_method, implements_trait, in_macro_or_desugar, is_copy, is_ctor_function, is_expn_of, is_self, is_self_ty, iter_input_pats, last_path_segment, match_path, match_qpath, match_trait_method, match_type, match_var, method_calls, method_chain_args, remove_blocks, return_ty, same_tys, single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite, span_lint, @@ -859,7 +859,7 @@ declare_lint_pass!(Methods => [ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods { #[allow(clippy::cognitive_complexity)] fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) { - if in_macro(expr.span) { + if in_macro_or_desugar(expr.span) { return; } diff --git a/clippy_lints/src/misc.rs b/clippy_lints/src/misc.rs index fed23bba9a83..b7479122ea9c 100644 --- a/clippy_lints/src/misc.rs +++ b/clippy_lints/src/misc.rs @@ -12,8 +12,8 @@ use syntax::source_map::{ExpnFormat, Span}; use crate::consts::{constant, Constant}; use crate::utils::sugg::Sugg; use crate::utils::{ - get_item_name, get_parent_expr, implements_trait, in_constant, in_macro, is_integer_literal, iter_input_pats, - last_path_segment, match_qpath, match_trait_method, paths, snippet, span_lint, span_lint_and_then, + get_item_name, get_parent_expr, implements_trait, in_constant, in_macro_or_desugar, is_integer_literal, + iter_input_pats, last_path_segment, match_qpath, match_trait_method, paths, snippet, span_lint, span_lint_and_then, span_lint_hir_and_then, walk_ptrs_ty, SpanlessEq, }; @@ -602,7 +602,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 { match res { - def::Res::Local(id) | def::Res::Upvar(id, ..) => !in_macro(cx.tcx.hir().span_by_hir_id(id)), + def::Res::Local(id) | def::Res::Upvar(id, ..) => !in_macro_or_desugar(cx.tcx.hir().span_by_hir_id(id)), _ => false, } } diff --git a/clippy_lints/src/missing_doc.rs b/clippy_lints/src/missing_doc.rs index 5d51f41aab67..0cce9ddbc7b6 100644 --- a/clippy_lints/src/missing_doc.rs +++ b/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::{in_macro, span_lint}; +use crate::utils::{in_macro_or_desugar, span_lint}; use if_chain::if_chain; use rustc::hir; use rustc::lint::{LateContext, LateLintPass, LintArray, LintContext, LintPass}; @@ -85,7 +85,7 @@ impl MissingDoc { return; } - if in_macro(sp) { + if in_macro_or_desugar(sp) { return; } diff --git a/clippy_lints/src/needless_bool.rs b/clippy_lints/src/needless_bool.rs index 0404bc41570f..43237d82053c 100644 --- a/clippy_lints/src/needless_bool.rs +++ b/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, in_macro, span_lint, span_lint_and_sugg}; +use crate::utils::{higher, in_macro_or_desugar, span_lint, span_lint_and_sugg}; use rustc::hir::*; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_lint_pass, declare_tool_lint}; @@ -131,7 +131,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 in_macro(e.span) { + if in_macro_or_desugar(e.span) { return; } diff --git a/clippy_lints/src/needless_borrow.rs b/clippy_lints/src/needless_borrow.rs index f32304478bb6..294a17d80bbb 100644 --- a/clippy_lints/src/needless_borrow.rs +++ b/clippy_lints/src/needless_borrow.rs @@ -2,7 +2,7 @@ //! //! This lint is **warn** by default -use crate::utils::{in_macro, snippet_opt, span_lint_and_then}; +use crate::utils::{in_macro_or_desugar, snippet_opt, span_lint_and_then}; use if_chain::if_chain; use rustc::hir::{BindingAnnotation, Expr, ExprKind, HirId, Item, MutImmutable, Pat, PatKind}; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; @@ -38,7 +38,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 in_macro(e.span) || self.derived_item.is_some() { + if in_macro_or_desugar(e.span) || self.derived_item.is_some() { return; } if let ExprKind::AddrOf(MutImmutable, ref inner) = e.node { @@ -76,7 +76,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBorrow { } } fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat) { - if in_macro(pat.span) || self.derived_item.is_some() { + if in_macro_or_desugar(pat.span) || self.derived_item.is_some() { return; } if_chain! { diff --git a/clippy_lints/src/needless_borrowed_ref.rs b/clippy_lints/src/needless_borrowed_ref.rs index 9de5e1a03890..b6104f6cc9a5 100644 --- a/clippy_lints/src/needless_borrowed_ref.rs +++ b/clippy_lints/src/needless_borrowed_ref.rs @@ -2,7 +2,7 @@ //! //! This lint is **warn** by default -use crate::utils::{in_macro, snippet, span_lint_and_then}; +use crate::utils::{in_macro_or_desugar, snippet, span_lint_and_then}; use if_chain::if_chain; use rustc::hir::{BindingAnnotation, MutImmutable, Pat, PatKind}; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; @@ -55,7 +55,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 in_macro(pat.span) { + if in_macro_or_desugar(pat.span) { // OK, simple enough, lints doesn't check in macro. return; } diff --git a/clippy_lints/src/needless_continue.rs b/clippy_lints/src/needless_continue.rs index 71eb89ae90c9..8835158155b6 100644 --- a/clippy_lints/src/needless_continue.rs +++ b/clippy_lints/src/needless_continue.rs @@ -39,7 +39,7 @@ use std::borrow::Cow; use syntax::ast; use syntax::source_map::{original_sp, DUMMY_SP}; -use crate::utils::{in_macro, snippet, snippet_block, span_help_and_lint, trim_multiline}; +use crate::utils::{in_macro_or_desugar, snippet, snippet_block, span_help_and_lint, trim_multiline}; declare_clippy_lint! { /// **What it does:** The lint checks for `if`-statements appearing in loops @@ -110,7 +110,7 @@ declare_lint_pass!(NeedlessContinue => [NEEDLESS_CONTINUE]); impl EarlyLintPass for NeedlessContinue { fn check_expr(&mut self, ctx: &EarlyContext<'_>, expr: &ast::Expr) { - if !in_macro(expr.span) { + if !in_macro_or_desugar(expr.span) { check_and_warn(ctx, expr); } } diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs index dee93f70819e..ce8f1a0ec552 100644 --- a/clippy_lints/src/needless_pass_by_value.rs +++ b/clippy_lints/src/needless_pass_by_value.rs @@ -1,7 +1,7 @@ use crate::utils::ptr::get_spans; use crate::utils::{ - get_trait_def_id, implements_trait, in_macro, is_copy, is_self, match_type, multispan_sugg, paths, snippet, - snippet_opt, span_lint_and_then, + get_trait_def_id, implements_trait, in_macro_or_desugar, is_copy, is_self, match_type, multispan_sugg, paths, + snippet, snippet_opt, span_lint_and_then, }; use if_chain::if_chain; use matches::matches; @@ -73,7 +73,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue { span: Span, hir_id: HirId, ) { - if in_macro(span) { + if in_macro_or_desugar(span) { return; } diff --git a/clippy_lints/src/no_effect.rs b/clippy_lints/src/no_effect.rs index ec4912634462..8c5ddaf4140f 100644 --- a/clippy_lints/src/no_effect.rs +++ b/clippy_lints/src/no_effect.rs @@ -1,4 +1,4 @@ -use crate::utils::{has_drop, in_macro, snippet_opt, span_lint, span_lint_and_sugg}; +use crate::utils::{has_drop, in_macro_or_desugar, snippet_opt, span_lint, span_lint_and_sugg}; use rustc::hir::def::{DefKind, Res}; use rustc::hir::{BinOpKind, BlockCheckMode, Expr, ExprKind, Stmt, StmtKind, UnsafeSource}; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; @@ -43,7 +43,7 @@ declare_clippy_lint! { } fn has_no_effect(cx: &LateContext<'_, '_>, expr: &Expr) -> bool { - if in_macro(expr.span) { + if in_macro_or_desugar(expr.span) { return false; } match expr.node { @@ -103,7 +103,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 in_macro(e.span) { + if in_macro_or_desugar(e.span) { return; } if let Some(snip) = snippet_opt(cx, e.span) { @@ -128,7 +128,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NoEffect { } fn reduce_expression<'a>(cx: &LateContext<'_, '_>, expr: &'a Expr) -> Option> { - if in_macro(expr.span) { + if in_macro_or_desugar(expr.span) { return None; } match expr.node { diff --git a/clippy_lints/src/non_copy_const.rs b/clippy_lints/src/non_copy_const.rs index 79ebf3bd46b3..2d56b4ee8b4e 100644 --- a/clippy_lints/src/non_copy_const.rs +++ b/clippy_lints/src/non_copy_const.rs @@ -14,7 +14,7 @@ use rustc_errors::Applicability; use rustc_typeck::hir_ty_to_ty; use syntax_pos::{Span, DUMMY_SP}; -use crate::utils::{in_constant, in_macro, is_copy, span_lint_and_then}; +use crate::utils::{in_constant, in_macro_or_desugar, is_copy, span_lint_and_then}; declare_clippy_lint! { /// **What it does:** Checks for declaration of `const` items which is interior @@ -118,7 +118,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, |db| { - if in_macro(span) { + if in_macro_or_desugar(span) { return; // Don't give suggestions into macros. } match source { diff --git a/clippy_lints/src/precedence.rs b/clippy_lints/src/precedence.rs index 52c11cacaac0..c52b59653706 100644 --- a/clippy_lints/src/precedence.rs +++ b/clippy_lints/src/precedence.rs @@ -1,4 +1,4 @@ -use crate::utils::{in_macro, snippet_with_applicability, span_lint_and_sugg}; +use crate::utils::{in_macro_or_desugar, snippet_with_applicability, span_lint_and_sugg}; use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; use rustc::{declare_lint_pass, declare_tool_lint}; use rustc_errors::Applicability; @@ -32,7 +32,7 @@ declare_lint_pass!(Precedence => [PRECEDENCE]); impl EarlyLintPass for Precedence { fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { - if in_macro(expr.span) { + if in_macro_or_desugar(expr.span) { return; } diff --git a/clippy_lints/src/redundant_clone.rs b/clippy_lints/src/redundant_clone.rs index db25b74cd081..8c86c27befdf 100644 --- a/clippy_lints/src/redundant_clone.rs +++ b/clippy_lints/src/redundant_clone.rs @@ -1,5 +1,5 @@ use crate::utils::{ - has_drop, in_macro, is_copy, match_type, paths, snippet_opt, span_lint_hir, span_lint_hir_and_then, + has_drop, in_macro_or_desugar, is_copy, match_type, paths, snippet_opt, span_lint_hir, span_lint_hir_and_then, walk_ptrs_ty_depth, }; use if_chain::if_chain; @@ -83,7 +83,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone { for (bb, bbdata) in mir.basic_blocks().iter_enumerated() { let terminator = bbdata.terminator(); - if in_macro(terminator.source_info.span) { + if in_macro_or_desugar(terminator.source_info.span) { continue; } diff --git a/clippy_lints/src/returns.rs b/clippy_lints/src/returns.rs index 7f298faddbd2..8d1873b8a18d 100644 --- a/clippy_lints/src/returns.rs +++ b/clippy_lints/src/returns.rs @@ -7,7 +7,7 @@ use syntax::source_map::Span; use syntax::visit::FnKind; use syntax_pos::BytePos; -use crate::utils::{in_macro, match_path_ast, snippet_opt, span_lint_and_then, span_note_and_lint}; +use crate::utils::{in_macro_or_desugar, match_path_ast, snippet_opt, span_lint_and_then, span_note_and_lint}; declare_clippy_lint! { /// **What it does:** Checks for return statements at the end of a block. @@ -130,7 +130,7 @@ impl Return { } fn emit_return_lint(&mut self, cx: &EarlyContext<'_>, ret_span: Span, inner_span: Span) { - if in_external_macro(cx.sess(), inner_span) || in_macro(inner_span) { + if in_external_macro(cx.sess(), inner_span) || in_macro_or_desugar(inner_span) { return; } span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded return statement", |db| { @@ -185,7 +185,7 @@ impl EarlyLintPass for Return { if_chain! { if let ast::FunctionRetTy::Ty(ref ty) = decl.output; if let ast::TyKind::Tup(ref vals) = ty.node; - if vals.is_empty() && !in_macro(ty.span) && get_def(span) == get_def(ty.span); + if vals.is_empty() && !in_macro_or_desugar(ty.span) && get_def(span) == get_def(ty.span); then { let (rspan, appl) = if let Ok(fn_source) = cx.sess().source_map() @@ -217,7 +217,7 @@ impl EarlyLintPass for Return { if_chain! { if let Some(ref stmt) = block.stmts.last(); if let ast::StmtKind::Expr(ref expr) = stmt.node; - if is_unit_expr(expr) && !in_macro(expr.span); + if is_unit_expr(expr) && !in_macro_or_desugar(expr.span); then { let sp = expr.span; span_lint_and_then(cx, UNUSED_UNIT, sp, "unneeded unit expression", |db| { @@ -235,7 +235,7 @@ impl EarlyLintPass for Return { fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) { match e.node { ast::ExprKind::Ret(Some(ref expr)) | ast::ExprKind::Break(_, Some(ref expr)) => { - if is_unit_expr(expr) && !in_macro(expr.span) { + if is_unit_expr(expr) && !in_macro_or_desugar(expr.span) { span_lint_and_then(cx, UNUSED_UNIT, expr.span, "unneeded `()`", |db| { db.span_suggestion( expr.span, diff --git a/clippy_lints/src/strings.rs b/clippy_lints/src/strings.rs index f1f006206ffc..0adeec93e69c 100644 --- a/clippy_lints/src/strings.rs +++ b/clippy_lints/src/strings.rs @@ -142,7 +142,7 @@ declare_lint_pass!(StringLitAsBytes => [STRING_LIT_AS_BYTES]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringLitAsBytes { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) { - use crate::utils::{in_macro, snippet, snippet_with_applicability}; + use crate::utils::{in_macro_or_desugar, snippet, snippet_with_applicability}; use syntax::ast::{LitKind, StrStyle}; if let ExprKind::MethodCall(ref path, _, ref args) = e.node { @@ -173,7 +173,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringLitAsBytes { ); } else if callsite == expanded && lit_content.as_str().chars().all(|c| c.is_ascii()) - && !in_macro(args[0].span) + && !in_macro_or_desugar(args[0].span) { span_lint_and_sugg( cx, diff --git a/clippy_lints/src/trivially_copy_pass_by_ref.rs b/clippy_lints/src/trivially_copy_pass_by_ref.rs index 36dba74615a4..8205851d7757 100644 --- a/clippy_lints/src/trivially_copy_pass_by_ref.rs +++ b/clippy_lints/src/trivially_copy_pass_by_ref.rs @@ -1,6 +1,6 @@ use std::cmp; -use crate::utils::{in_macro, is_copy, is_self_ty, snippet, span_lint_and_sugg}; +use crate::utils::{in_macro_or_desugar, is_copy, is_self_ty, snippet, span_lint_and_sugg}; use if_chain::if_chain; use matches::matches; use rustc::hir; @@ -141,7 +141,7 @@ impl_lint_pass!(TriviallyCopyPassByRef => [TRIVIALLY_COPY_PASS_BY_REF]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TriviallyCopyPassByRef { fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) { - if in_macro(item.span) { + if in_macro_or_desugar(item.span) { return; } if let ItemKind::Trait(_, _, _, _, ref trait_items) = item.node { @@ -158,7 +158,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TriviallyCopyPassByRef { span: Span, hir_id: HirId, ) { - if in_macro(span) { + if in_macro_or_desugar(span) { return; } diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs index 03f329264278..4aa3b75dec2e 100644 --- a/clippy_lints/src/types.rs +++ b/clippy_lints/src/types.rs @@ -22,7 +22,7 @@ use syntax::source_map::Span; use crate::consts::{constant, Constant}; use crate::utils::paths; use crate::utils::{ - clip, comparisons, differing_macro_contexts, higher, in_constant, in_macro, int_bits, last_path_segment, + clip, comparisons, differing_macro_contexts, higher, in_constant, in_macro_or_desugar, int_bits, last_path_segment, match_path, multispan_sugg, same_tys, sext, snippet, snippet_opt, snippet_with_applicability, span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then, unsext, }; @@ -232,7 +232,7 @@ fn match_type_parameter(cx: &LateContext<'_, '_>, qpath: &QPath, path: &[&str]) /// local bindings should only be checked for the `BORROWED_BOX` lint. #[allow(clippy::too_many_lines)] fn check_ty(cx: &LateContext<'_, '_>, hir_ty: &hir::Ty, is_local: bool) { - if in_macro(hir_ty.span) { + if in_macro_or_desugar(hir_ty.span) { return; } match hir_ty.node { @@ -460,7 +460,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.node { if is_unit(cx.tables.pat_ty(&local.pat)) { - if in_external_macro(cx.sess(), stmt.span) || in_macro(local.pat.span) { + if in_external_macro(cx.sess(), stmt.span) || in_macro_or_desugar(local.pat.span) { return; } if higher::is_from_for_desugar(local) { @@ -522,7 +522,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) { - if in_macro(expr.span) { + if in_macro_or_desugar(expr.span) { return; } if let ExprKind::Binary(ref cmp, ref left, _) = expr.node { @@ -571,7 +571,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 in_macro(expr.span) { + if in_macro_or_desugar(expr.span) { return; } @@ -1110,7 +1110,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 in_macro(expr.span) { + if in_macro_or_desugar(expr.span) { return; } if let ExprKind::Cast(ref ex, _) = expr.node { @@ -1367,7 +1367,7 @@ impl<'a, 'tcx> TypeComplexity { } fn check_type(&self, cx: &LateContext<'_, '_>, ty: &hir::Ty) { - if in_macro(ty.span) { + if in_macro_or_desugar(ty.span) { return; } let score = { @@ -1471,7 +1471,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CharLitAsU8 { if let ExprKind::Cast(ref e, _) = expr.node { if let ExprKind::Lit(ref l) = e.node { if let LitKind::Char(_) = l.node { - if ty::Uint(UintTy::U8) == cx.tables.expr_ty(expr).sty && !in_macro(expr.span) { + if ty::Uint(UintTy::U8) == cx.tables.expr_ty(expr).sty && !in_macro_or_desugar(expr.span) { let msg = "casting character literal to u8. `char`s \ are 4 bytes wide in rust, so casting to u8 \ truncates them"; @@ -1632,7 +1632,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AbsurdExtremeComparisons { if let ExprKind::Binary(ref cmp, ref lhs, ref rhs) = expr.node { if let Some((culprit, result)) = detect_absurd_comparison(cx, cmp.node, lhs, rhs) { - if !in_macro(expr.span) { + if !in_macro_or_desugar(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/clippy_lints/src/unused_label.rs b/clippy_lints/src/unused_label.rs index 73934adfb33f..4b41a02de974 100644 --- a/clippy_lints/src/unused_label.rs +++ b/clippy_lints/src/unused_label.rs @@ -1,4 +1,4 @@ -use crate::utils::{in_macro, span_lint}; +use crate::utils::{in_macro_or_desugar, span_lint}; use rustc::hir; use rustc::hir::intravisit::{walk_expr, walk_fn, FnKind, NestedVisitorMap, Visitor}; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; @@ -44,7 +44,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedLabel { span: Span, fn_id: hir::HirId, ) { - if in_macro(span) { + if in_macro_or_desugar(span) { return; } diff --git a/clippy_lints/src/unwrap.rs b/clippy_lints/src/unwrap.rs index 348579f75d18..0ec7256d4b3a 100644 --- a/clippy_lints/src/unwrap.rs +++ b/clippy_lints/src/unwrap.rs @@ -2,7 +2,9 @@ use if_chain::if_chain; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_lint_pass, declare_tool_lint}; -use crate::utils::{higher::if_block, in_macro, match_type, paths, span_lint_and_then, usage::is_potentially_mutated}; +use crate::utils::{ + higher::if_block, in_macro_or_desugar, match_type, paths, span_lint_and_then, usage::is_potentially_mutated, +}; use rustc::hir::intravisit::*; use rustc::hir::*; use syntax::source_map::Span; @@ -189,7 +191,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Unwrap { span: Span, fn_id: HirId, ) { - if in_macro(span) { + if in_macro_or_desugar(span) { return; } diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index 7ebaeb0951f7..34280e641db0 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -90,7 +90,7 @@ pub fn in_constant(cx: &LateContext<'_, '_>, id: HirId) -> bool { } /// Returns `true` if this `expn_info` was expanded by any macro. -pub fn in_macro(span: Span) -> bool { +pub fn in_macro_or_desugar(span: Span) -> bool { if let Some(info) = span.ctxt().outer().expn_info() { if let ExpnFormat::CompilerDesugaring(..) = info.format { false @@ -339,7 +339,7 @@ pub fn method_calls<'a>(expr: &'a Expr, max_depth: usize) -> (Vec, Vec<& let mut current = expr; for _ in 0..max_depth { if let ExprKind::MethodCall(path, _, args) = ¤t.node { - if args.iter().any(|e| in_macro(e.span)) { + if args.iter().any(|e| in_macro_or_desugar(e.span)) { break; } method_names.push(path.ident.name); @@ -366,7 +366,7 @@ pub fn method_chain_args<'a>(expr: &'a Expr, methods: &[&str]) -> Option first if let ExprKind::MethodCall(ref path, _, ref args) = current.node { if path.ident.name == *method_name { - if args.iter().any(|e| in_macro(e.span)) { + if args.iter().any(|e| in_macro_or_desugar(e.span)) { return None; } matched.push(&**args); // build up `matched` backwards @@ -461,7 +461,7 @@ pub fn snippet_with_applicability<'a, 'b, T: LintContext<'b>>( default: &'a str, applicability: &mut Applicability, ) -> Cow<'a, str> { - if *applicability != Applicability::Unspecified && in_macro(span) { + if *applicability != Applicability::Unspecified && in_macro_or_desugar(span) { *applicability = Applicability::MaybeIncorrect; } snippet_opt(cx, span).map_or_else( @@ -531,7 +531,7 @@ pub fn expr_block<'a, 'b, T: LintContext<'b>>( ) -> Cow<'a, str> { let code = snippet_block(cx, expr.span, default); let string = option.unwrap_or_default(); - if in_macro(expr.span) { + if in_macro_or_desugar(expr.span) { Cow::Owned(format!("{{ {} }}", snippet_with_macro_callsite(cx, expr.span, default))) } else if let ExprKind::Block(_, _) = expr.node { Cow::Owned(format!("{}{}", code, string)) diff --git a/clippy_lints/src/utils/sugg.rs b/clippy_lints/src/utils/sugg.rs index 7d029d0f61e2..4d87373ec6a9 100644 --- a/clippy_lints/src/utils/sugg.rs +++ b/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, in_macro, snippet, snippet_opt, snippet_with_macro_callsite}; +use crate::utils::{higher, in_macro_or_desugar, snippet, snippet_opt, snippet_with_macro_callsite}; use matches::matches; use rustc::hir; use rustc::lint::{EarlyContext, LateContext, LintContext}; @@ -69,7 +69,7 @@ impl<'a> Sugg<'a> { default: &'a str, applicability: &mut Applicability, ) -> Self { - if *applicability != Applicability::Unspecified && in_macro(expr.span) { + if *applicability != Applicability::Unspecified && in_macro_or_desugar(expr.span) { *applicability = Applicability::MaybeIncorrect; } Self::hir_opt(cx, expr).unwrap_or_else(|| { diff --git a/doc/adding_lints.md b/doc/adding_lints.md index 5781ab7f9999..a80ee9f54355 100644 --- a/doc/adding_lints.md +++ b/doc/adding_lints.md @@ -381,7 +381,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] -* [`in_macro`][in_macro] and [`in_external_macro`][in_external_macro] +* [`in_macro_or_desugar`][in_macro_or_desugar] and [`in_external_macro`][in_external_macro] * [`Span`][span] * [`Applicability`][applicability] * [The rustc guide][rustc_guide] explains a lot of internal compiler concepts @@ -421,7 +421,7 @@ don't hesitate to ask on Discord, IRC or in the issue/PR. [if_chain]: https://docs.rs/if_chain/0.1.2/if_chain/ [ty]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/sty/index.html [ast]: https://doc.rust-lang.org/nightly/nightly-rustc/syntax/ast/index.html -[in_macro]: https://github.com/rust-lang/rust-clippy/blob/d0717d1f9531a03d154aaeb0cad94c243915a146/clippy_lints/src/utils/mod.rs#L94 +[in_macro_or_desugar]: https://github.com/rust-lang/rust-clippy/blob/d0717d1f9531a03d154aaeb0cad94c243915a146/clippy_lints/src/utils/mod.rs#L94 [in_external_macro]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/lint/fn.in_external_macro.html [play]: https://play.rust-lang.org [author_example]: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f093b986e80ad62f3b67a1f24f5e66e2 diff --git a/tests/ui/used_underscore_binding.rs b/tests/ui/used_underscore_binding.rs index 87542252e1fb..5a02ff5d193d 100644 --- a/tests/ui/used_underscore_binding.rs +++ b/tests/ui/used_underscore_binding.rs @@ -25,7 +25,7 @@ fn prefix_underscore(_foo: u32) -> u32 { } /// Tests that we lint if we use a `_`-variable defined outside within a macro expansion -fn in_macro(_foo: u32) { +fn in_macro_or_desugar(_foo: u32) { println!("{}", _foo); assert_eq!(_foo, _foo); @@ -90,7 +90,7 @@ fn main() { let foo = 0u32; // tests of unused_underscore lint let _ = prefix_underscore(foo); - in_macro(foo); + in_macro_or_desugar(foo); in_struct_field(); // possible false positives let _ = non_prefix_underscore(foo); From abf6481f877d2f5f29ab21cb7039be86dcd1bb10 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sat, 11 May 2019 20:41:30 -0700 Subject: [PATCH 2/2] Add in_macro again --- clippy_lints/src/booleans.rs | 6 +++--- clippy_lints/src/methods/mod.rs | 4 ++-- clippy_lints/src/utils/mod.rs | 8 ++++++-- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/clippy_lints/src/booleans.rs b/clippy_lints/src/booleans.rs index 6dedc7d8775d..5c19e232bc98 100644 --- a/clippy_lints/src/booleans.rs +++ b/clippy_lints/src/booleans.rs @@ -1,6 +1,6 @@ use crate::utils::{ - get_trait_def_id, implements_trait, in_macro_or_desugar, match_type, paths, snippet_opt, span_lint_and_then, - SpanlessEq, + get_trait_def_id, implements_trait, in_macro, in_macro_or_desugar, match_type, paths, snippet_opt, + span_lint_and_then, SpanlessEq, }; use rustc::hir::intravisit::*; use rustc::hir::*; @@ -442,7 +442,7 @@ impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> { impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> { fn visit_expr(&mut self, e: &'tcx Expr) { - if in_macro_or_desugar(e.span) { + if in_macro(e.span) { return; } match &e.node { diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index c96d05cef2f3..f8cde663d899 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -20,7 +20,7 @@ use syntax::symbol::LocalInternedString; use crate::utils::paths; use crate::utils::sugg; use crate::utils::{ - get_arg_name, get_parent_expr, get_trait_def_id, has_iter_method, implements_trait, in_macro_or_desugar, is_copy, + get_arg_name, get_parent_expr, get_trait_def_id, has_iter_method, implements_trait, in_macro, is_copy, is_ctor_function, is_expn_of, is_self, is_self_ty, iter_input_pats, last_path_segment, match_path, match_qpath, match_trait_method, match_type, match_var, method_calls, method_chain_args, remove_blocks, return_ty, same_tys, single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite, span_lint, @@ -859,7 +859,7 @@ declare_lint_pass!(Methods => [ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods { #[allow(clippy::cognitive_complexity)] fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) { - if in_macro_or_desugar(expr.span) { + if in_macro(expr.span) { return; } diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index 34280e641db0..bfc1233f9eba 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -89,8 +89,13 @@ pub fn in_constant(cx: &LateContext<'_, '_>, id: HirId) -> bool { } } -/// Returns `true` if this `expn_info` was expanded by any macro. +/// Returns `true` if this `expn_info` was expanded by any macro or desugaring pub fn in_macro_or_desugar(span: Span) -> bool { + span.ctxt().outer().expn_info().is_some() +} + +/// Returns `true` if this `expn_info` was expanded by any macro. +pub fn in_macro(span: Span) -> bool { if let Some(info) = span.ctxt().outer().expn_info() { if let ExpnFormat::CompilerDesugaring(..) = info.format { false @@ -101,7 +106,6 @@ pub fn in_macro_or_desugar(span: Span) -> bool { false } } - // If the snippet is empty, it's an attribute that was inserted during macro // expansion and we want to ignore those, because they could come from external // sources that the user has no control over.