From 049918420150fb42a7b774c1f4680ee7ab7f7627 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Fri, 10 May 2019 23:19:35 -0700 Subject: [PATCH] Ignore desugarings in macro checks --- clippy_lints/src/utils/mod.rs | 11 ++++++++++- tests/ui/into_iter_on_ref.fixed | 2 +- tests/ui/into_iter_on_ref.stderr | 14 ++++++++++---- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index 568371098..7ebaeb095 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -40,6 +40,7 @@ use rustc_data_structures::sync::Lrc; use rustc_errors::Applicability; use syntax::ast::{self, LitKind}; use syntax::attr; +use syntax::ext::hygiene::ExpnFormat; use syntax::source_map::{Span, DUMMY_SP}; use syntax::symbol::{keywords, Symbol}; @@ -90,7 +91,15 @@ 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 { - span.ctxt().outer().expn_info().is_some() + if let Some(info) = span.ctxt().outer().expn_info() { + if let ExpnFormat::CompilerDesugaring(..) = info.format { + false + } else { + true + } + } else { + false + } } // If the snippet is empty, it's an attribute that was inserted during macro diff --git a/tests/ui/into_iter_on_ref.fixed b/tests/ui/into_iter_on_ref.fixed index 659fd56f9..f5342be63 100644 --- a/tests/ui/into_iter_on_ref.fixed +++ b/tests/ui/into_iter_on_ref.fixed @@ -10,7 +10,7 @@ fn main() { for _ in &[1, 2, 3] {} for _ in vec![X, X] {} for _ in &vec![X, X] {} - for _ in [1, 2, 3].into_iter() {} //~ ERROR equivalent to .iter() + for _ in [1, 2, 3].iter() {} //~ ERROR equivalent to .iter() let _ = [1, 2, 3].iter(); //~ ERROR equivalent to .iter() let _ = vec![1, 2, 3].into_iter(); diff --git a/tests/ui/into_iter_on_ref.stderr b/tests/ui/into_iter_on_ref.stderr index c3e5c8561..931e4880f 100644 --- a/tests/ui/into_iter_on_ref.stderr +++ b/tests/ui/into_iter_on_ref.stderr @@ -1,8 +1,8 @@ error: this .into_iter() call is equivalent to .iter() and will not move the array - --> $DIR/into_iter_on_ref.rs:15:23 + --> $DIR/into_iter_on_ref.rs:13:24 | -LL | let _ = [1, 2, 3].into_iter(); //~ ERROR equivalent to .iter() - | ^^^^^^^^^ help: call directly: `iter` +LL | for _ in [1, 2, 3].into_iter() {} //~ ERROR equivalent to .iter() + | ^^^^^^^^^ help: call directly: `iter` | note: lint level defined here --> $DIR/into_iter_on_ref.rs:4:9 @@ -10,6 +10,12 @@ note: lint level defined here LL | #![deny(clippy::into_iter_on_array)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +error: this .into_iter() call is equivalent to .iter() and will not move the array + --> $DIR/into_iter_on_ref.rs:15:23 + | +LL | let _ = [1, 2, 3].into_iter(); //~ ERROR equivalent to .iter() + | ^^^^^^^^^ help: call directly: `iter` + error: this .into_iter() call is equivalent to .iter() and will not move the Vec --> $DIR/into_iter_on_ref.rs:17:30 | @@ -168,5 +174,5 @@ error: this .into_iter() call is equivalent to .iter() and will not move the Pat LL | let _ = std::path::PathBuf::from("12/34").into_iter(); //~ ERROR equivalent to .iter() | ^^^^^^^^^ help: call directly: `iter` -error: aborting due to 27 previous errors +error: aborting due to 28 previous errors