Skip to content

Commit feb18c3

Browse files
committed
Auto merge of #4082 - Manishearth:macro-check-split, r=oli-obk
Make most macro checks also check for desugarings We should audit the macro checks one by one and re-add `in_macro`. I suspect it's applicable to most of them. fixes #4081
2 parents 3710ec5 + abf6481 commit feb18c3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+133
-118
lines changed

clippy_lints/src/assertions_on_constants.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc::{declare_lint_pass, declare_tool_lint};
55
use syntax_pos::Span;
66

77
use crate::consts::{constant, Constant};
8-
use crate::utils::{in_macro, is_direct_expn_of, span_help_and_lint};
8+
use crate::utils::{in_macro_or_desugar, is_direct_expn_of, span_help_and_lint};
99

1010
declare_clippy_lint! {
1111
/// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls.
@@ -34,16 +34,16 @@ declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
3434
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
3535
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
3636
let mut is_debug_assert = false;
37-
let debug_assert_not_in_macro = |span: Span| {
37+
let debug_assert_not_in_macro_or_desugar = |span: Span| {
3838
is_debug_assert = true;
3939
// Check that `debug_assert!` itself is not inside a macro
40-
!in_macro(span)
40+
!in_macro_or_desugar(span)
4141
};
4242
if_chain! {
4343
if let Some(assert_span) = is_direct_expn_of(e.span, "assert");
44-
if !in_macro(assert_span)
44+
if !in_macro_or_desugar(assert_span)
4545
|| is_direct_expn_of(assert_span, "debug_assert")
46-
.map_or(false, debug_assert_not_in_macro);
46+
.map_or(false, debug_assert_not_in_macro_or_desugar);
4747
if let ExprKind::Unary(_, ref lit) = e.node;
4848
if let Some(bool_const) = constant(cx, cx.tables, lit);
4949
then {

clippy_lints/src/attrs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::reexport::*;
44
use crate::utils::{
5-
in_macro, is_present_in_source, last_line_of_span, paths, snippet_opt, span_lint, span_lint_and_sugg,
5+
in_macro_or_desugar, is_present_in_source, last_line_of_span, paths, snippet_opt, span_lint, span_lint_and_sugg,
66
span_lint_and_then, without_block_comments,
77
};
88
use if_chain::if_chain;
@@ -408,7 +408,7 @@ fn is_relevant_expr(cx: &LateContext<'_, '_>, tables: &ty::TypeckTables<'_>, exp
408408
}
409409

410410
fn check_attrs(cx: &LateContext<'_, '_>, span: Span, name: Name, attrs: &[Attribute]) {
411-
if in_macro(span) {
411+
if in_macro_or_desugar(span) {
412412
return;
413413
}
414414

clippy_lints/src/block_in_if_condition.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl<'a, 'tcx: 'a> Visitor<'tcx> for ExVisitor<'a, 'tcx> {
5454
if let ExprKind::Closure(_, _, eid, _, _) = expr.node {
5555
let body = self.cx.tcx.hir().body(eid);
5656
let ex = &body.value;
57-
if matches!(ex.node, ExprKind::Block(_, _)) && !in_macro(body.value.span) {
57+
if matches!(ex.node, ExprKind::Block(_, _)) && !in_macro_or_desugar(body.value.span) {
5858
self.found_block = Some(ex);
5959
return;
6060
}
@@ -79,7 +79,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
7979
if let Some(ex) = &block.expr {
8080
// don't dig into the expression here, just suggest that they remove
8181
// the block
82-
if in_macro(expr.span) || differing_macro_contexts(expr.span, ex.span) {
82+
if in_macro_or_desugar(expr.span) || differing_macro_contexts(expr.span, ex.span) {
8383
return;
8484
}
8585
span_help_and_lint(
@@ -96,7 +96,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
9696
}
9797
} else {
9898
let span = block.expr.as_ref().map_or_else(|| block.stmts[0].span, |e| e.span);
99-
if in_macro(span) || differing_macro_contexts(expr.span, span) {
99+
if in_macro_or_desugar(span) || differing_macro_contexts(expr.span, span) {
100100
return;
101101
}
102102
// move block higher

clippy_lints/src/booleans.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::utils::{
2-
get_trait_def_id, implements_trait, in_macro, match_type, paths, snippet_opt, span_lint_and_then, SpanlessEq,
2+
get_trait_def_id, implements_trait, in_macro, in_macro_or_desugar, match_type, paths, snippet_opt,
3+
span_lint_and_then, SpanlessEq,
34
};
45
use rustc::hir::intravisit::*;
56
use rustc::hir::*;
@@ -93,7 +94,7 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
9394

9495
fn run(&mut self, e: &'v Expr) -> Result<Bool, String> {
9596
// prevent folding of `cfg!` macros and the like
96-
if !in_macro(e.span) {
97+
if !in_macro_or_desugar(e.span) {
9798
match &e.node {
9899
ExprKind::Unary(UnNot, inner) => return Ok(Bool::Not(box self.run(inner)?)),
99100
ExprKind::Binary(binop, lhs, rhs) => match &binop.node {

clippy_lints/src/cognitive_complexity.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc::{declare_tool_lint, impl_lint_pass};
99
use syntax::ast::Attribute;
1010
use syntax::source_map::Span;
1111

12-
use crate::utils::{in_macro, is_allowed, match_type, paths, span_help_and_lint, LimitStack};
12+
use crate::utils::{in_macro_or_desugar, is_allowed, match_type, paths, span_help_and_lint, LimitStack};
1313

1414
declare_clippy_lint! {
1515
/// **What it does:** Checks for methods with high cognitive complexity.
@@ -42,7 +42,7 @@ impl_lint_pass!(CognitiveComplexity => [COGNITIVE_COMPLEXITY]);
4242

4343
impl CognitiveComplexity {
4444
fn check<'a, 'tcx: 'a>(&mut self, cx: &'a LateContext<'a, 'tcx>, body: &'tcx Body, span: Span) {
45-
if in_macro(span) {
45+
if in_macro_or_desugar(span) {
4646
return;
4747
}
4848

clippy_lints/src/collapsible_if.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use rustc::{declare_lint_pass, declare_tool_lint};
1818
use syntax::ast;
1919

2020
use crate::utils::sugg::Sugg;
21-
use crate::utils::{in_macro, snippet_block, snippet_block_with_applicability, span_lint_and_sugg, span_lint_and_then};
21+
use crate::utils::{
22+
in_macro_or_desugar, snippet_block, snippet_block_with_applicability, span_lint_and_sugg, span_lint_and_then,
23+
};
2224
use rustc_errors::Applicability;
2325

2426
declare_clippy_lint! {
@@ -75,7 +77,7 @@ declare_lint_pass!(CollapsibleIf => [COLLAPSIBLE_IF]);
7577

7678
impl EarlyLintPass for CollapsibleIf {
7779
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
78-
if !in_macro(expr.span) {
80+
if !in_macro_or_desugar(expr.span) {
7981
check_if(cx, expr)
8082
}
8183
}
@@ -110,7 +112,7 @@ fn check_collapsible_maybe_if_let(cx: &EarlyContext<'_>, else_: &ast::Expr) {
110112
if let ast::ExprKind::Block(ref block, _) = else_.node;
111113
if !block_starts_with_comment(cx, block);
112114
if let Some(else_) = expr_block(block);
113-
if !in_macro(else_.span);
115+
if !in_macro_or_desugar(else_.span);
114116
then {
115117
match else_.node {
116118
ast::ExprKind::If(..) | ast::ExprKind::IfLet(..) => {

clippy_lints/src/const_static_lifetime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{in_macro, snippet, span_lint_and_then};
1+
use crate::utils::{in_macro_or_desugar, snippet, span_lint_and_then};
22
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
33
use rustc::{declare_lint_pass, declare_tool_lint};
44
use rustc_errors::Applicability;
@@ -81,7 +81,7 @@ impl StaticConst {
8181

8282
impl EarlyLintPass for StaticConst {
8383
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
84-
if !in_macro(item.span) {
84+
if !in_macro_or_desugar(item.span) {
8585
// Match only constants...
8686
if let ItemKind::Const(ref var_type, _) = item.node {
8787
self.visit_type(var_type, cx);

clippy_lints/src/copies.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{get_parent_expr, higher, in_macro, snippet, span_lint_and_then, span_note_and_lint};
1+
use crate::utils::{get_parent_expr, higher, in_macro_or_desugar, snippet, span_lint_and_then, span_note_and_lint};
22
use crate::utils::{SpanlessEq, SpanlessHash};
33
use rustc::hir::*;
44
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@@ -107,7 +107,7 @@ declare_lint_pass!(CopyAndPaste => [IFS_SAME_COND, IF_SAME_THEN_ELSE, MATCH_SAME
107107

108108
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CopyAndPaste {
109109
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
110-
if !in_macro(expr.span) {
110+
if !in_macro_or_desugar(expr.span) {
111111
// skip ifs directly in else, it will be checked in the parent if
112112
if let Some(expr) = get_parent_expr(cx, expr) {
113113
if let Some((_, _, Some(ref else_expr))) = higher::if_block(&expr) {

clippy_lints/src/double_parens.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{in_macro, span_lint};
1+
use crate::utils::{in_macro_or_desugar, span_lint};
22
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
33
use rustc::{declare_lint_pass, declare_tool_lint};
44
use syntax::ast::*;
@@ -26,7 +26,7 @@ declare_lint_pass!(DoubleParens => [DOUBLE_PARENS]);
2626

2727
impl EarlyLintPass for DoubleParens {
2828
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
29-
if in_macro(expr.span) {
29+
if in_macro_or_desugar(expr.span) {
3030
return;
3131
}
3232

clippy_lints/src/enum_variants.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! lint on enum variants that are prefixed or suffixed by the same characters
22
3-
use crate::utils::{camel_case, in_macro, is_present_in_source};
3+
use crate::utils::{camel_case, in_macro_or_desugar, is_present_in_source};
44
use crate::utils::{span_help_and_lint, span_lint};
55
use rustc::lint::{EarlyContext, EarlyLintPass, Lint, LintArray, LintPass};
66
use rustc::{declare_tool_lint, impl_lint_pass};
@@ -244,7 +244,7 @@ impl EarlyLintPass for EnumVariantNames {
244244
let item_name = item.ident.as_str();
245245
let item_name_chars = item_name.chars().count();
246246
let item_camel = to_camel_case(&item_name);
247-
if !in_macro(item.span) && is_present_in_source(cx, item.span) {
247+
if !in_macro_or_desugar(item.span) && is_present_in_source(cx, item.span) {
248248
if let Some(&(ref mod_name, ref mod_camel)) = self.modules.last() {
249249
// constants don't have surrounding modules
250250
if !mod_camel.is_empty() {

0 commit comments

Comments
 (0)