diff --git a/clippy_lints/src/attrs/mixed_attributes_style.rs b/clippy_lints/src/attrs/mixed_attributes_style.rs index 22bc7b65cb8c..c35918cc3111 100644 --- a/clippy_lints/src/attrs/mixed_attributes_style.rs +++ b/clippy_lints/src/attrs/mixed_attributes_style.rs @@ -3,7 +3,8 @@ use clippy_utils::diagnostics::span_lint; use rustc_ast::{AttrKind, AttrStyle, Attribute}; use rustc_data_structures::fx::FxHashSet; use rustc_lint::{LateContext, LintContext}; -use rustc_span::{Span, Symbol}; +use rustc_span::source_map::SourceMap; +use rustc_span::{SourceFile, Span, Symbol}; use std::sync::Arc; #[derive(Hash, PartialEq, Eq)] @@ -35,8 +36,11 @@ pub(super) fn check(cx: &LateContext<'_>, item_span: Span, attrs: &[Attribute]) let mut inner_attr_kind: FxHashSet = FxHashSet::default(); let mut outer_attr_kind: FxHashSet = FxHashSet::default(); + let source_map = cx.sess().source_map(); + let item_src = source_map.lookup_source_file(item_span.lo()); + for attr in attrs { - if attr.span.from_expansion() || !attr_in_same_src_as_item(cx, attr.span, item_span) { + if attr.span.from_expansion() || !attr_in_same_src_as_item(source_map, &item_src, item_span) { continue; } @@ -75,9 +79,7 @@ fn lint_mixed_attrs(cx: &LateContext<'_>, attrs: &[Attribute]) { ); } -fn attr_in_same_src_as_item(cx: &LateContext<'_>, attr_span: Span, item_span: Span) -> bool { - let source_map = cx.sess().source_map(); - let item_src = source_map.lookup_source_file(item_span.lo()); +fn attr_in_same_src_as_item(source_map: &SourceMap, item_src: &Arc, attr_span: Span) -> bool { let attr_src = source_map.lookup_source_file(attr_span.lo()); - Arc::ptr_eq(&attr_src, &item_src) + Arc::ptr_eq(item_src, &attr_src) } diff --git a/tests/ui/mixed_attributes_style.rs b/tests/ui/mixed_attributes_style.rs index 0b1d10dce16f..1a646c265223 100644 --- a/tests/ui/mixed_attributes_style.rs +++ b/tests/ui/mixed_attributes_style.rs @@ -1,9 +1,11 @@ //@aux-build:proc_macro_attr.rs +//@compile-flags: --test --cfg dummy_cfg #![feature(custom_inner_attributes)] #![warn(clippy::mixed_attributes_style)] #![allow(clippy::duplicated_attributes)] -use proc_macro_attr::dummy; +#[macro_use] +extern crate proc_macro_attr; #[allow(unused)] //~ ERROR: item has both inner and outer attributes fn foo1() { @@ -58,44 +60,41 @@ mod quz { #![allow(unused)] } -// issue #12530, don't lint different attributes entirely -#[cfg(test)] -mod tests { - #![allow(clippy::unreadable_literal)] -} -#[cfg(unix)] -mod another_mod { - #![allow(clippy::question_mark)] -} -/// Nested mod - Good -mod nested_mod { - #[allow(dead_code)] //~ ERROR: item has both inner and outer attributes - mod inner_mod { - #![allow(dead_code)] - } -} -/// Nested mod - Good //~ ERROR: item has both inner and outer attributes -#[allow(unused)] -mod nest_mod_2 { - #![allow(unused)] +mod issue_12530 { + // don't lint different attributes entirely + #[cfg(test)] + mod tests { + #![allow(clippy::unreadable_literal)] - #[allow(dead_code)] //~ ERROR: item has both inner and outer attributes - mod inner_mod { - #![allow(dead_code)] + #[allow(dead_code)] //~ ERROR: item has both inner and outer attributes + mod inner_mod { + #![allow(dead_code)] + } } -} -/// Nested mod - Possible FN -#[cfg(test)] -mod nested_mod_false_nagative { - #![allow(unused)] + #[cfg(dummy_cfg)] + mod another_mod { + #![allow(clippy::question_mark)] + } + /// Nested mod + mod nested_mod { + #[allow(dead_code)] //~ ERROR: item has both inner and outer attributes + mod inner_mod { + #![allow(dead_code)] + } + } + /// Nested mod //~ ERROR: item has both inner and outer attributes + #[allow(unused)] + mod nest_mod_2 { + #![allow(unused)] - #[allow(dead_code)] // This should lint but it does not, removing the `#[cfg(test)]` solves the problem. - mod inner_mod { - #![allow(dead_code)] + #[allow(dead_code)] //~ ERROR: item has both inner and outer attributes + mod inner_mod { + #![allow(dead_code)] + } + } + // Different path symbols - Known FN + #[dummy] + fn use_dummy() { + #![proc_macro_attr::dummy] } -} -// Different path symbols - Known FN -#[dummy] -fn use_dummy() { - #![proc_macro_attr::dummy] } diff --git a/tests/ui/mixed_attributes_style.stderr b/tests/ui/mixed_attributes_style.stderr index bc5517088abb..a1d3fc430f6c 100644 --- a/tests/ui/mixed_attributes_style.stderr +++ b/tests/ui/mixed_attributes_style.stderr @@ -1,5 +1,5 @@ error: item has both inner and outer attributes - --> tests/ui/mixed_attributes_style.rs:8:1 + --> tests/ui/mixed_attributes_style.rs:10:1 | LL | / #[allow(unused)] LL | | fn foo1() { @@ -10,7 +10,7 @@ LL | | #![allow(unused)] = help: to override `-D warnings` add `#[allow(clippy::mixed_attributes_style)]` error: item has both inner and outer attributes - --> tests/ui/mixed_attributes_style.rs:22:1 + --> tests/ui/mixed_attributes_style.rs:24:1 | LL | / /// linux LL | | @@ -19,7 +19,7 @@ LL | | //! windows | |_______________^ error: item has both inner and outer attributes - --> tests/ui/mixed_attributes_style.rs:37:1 + --> tests/ui/mixed_attributes_style.rs:39:1 | LL | / #[allow(unused)] LL | | mod bar { @@ -27,29 +27,37 @@ LL | | #![allow(unused)] | |_____________________^ error: item has both inner and outer attributes - --> tests/ui/mixed_attributes_style.rs:72:5 + --> tests/ui/mixed_attributes_style.rs:69:9 | -LL | / #[allow(dead_code)] -LL | | mod inner_mod { -LL | | #![allow(dead_code)] - | |____________________________^ +LL | / #[allow(dead_code)] +LL | | mod inner_mod { +LL | | #![allow(dead_code)] + | |________________________________^ error: item has both inner and outer attributes - --> tests/ui/mixed_attributes_style.rs:77:1 + --> tests/ui/mixed_attributes_style.rs:80:9 | -LL | / /// Nested mod - Good -LL | | #[allow(unused)] -LL | | mod nest_mod_2 { -LL | | #![allow(unused)] - | |_____________________^ +LL | / #[allow(dead_code)] +LL | | mod inner_mod { +LL | | #![allow(dead_code)] + | |________________________________^ + +error: item has both inner and outer attributes + --> tests/ui/mixed_attributes_style.rs:85:5 + | +LL | / /// Nested mod +LL | | #[allow(unused)] +LL | | mod nest_mod_2 { +LL | | #![allow(unused)] + | |_________________________^ error: item has both inner and outer attributes - --> tests/ui/mixed_attributes_style.rs:82:5 + --> tests/ui/mixed_attributes_style.rs:90:9 | -LL | / #[allow(dead_code)] -LL | | mod inner_mod { -LL | | #![allow(dead_code)] - | |____________________________^ +LL | / #[allow(dead_code)] +LL | | mod inner_mod { +LL | | #![allow(dead_code)] + | |________________________________^ -error: aborting due to 6 previous errors +error: aborting due to 7 previous errors diff --git a/tests/ui/mixed_attributes_style/mod_declaration.stderr b/tests/ui/mixed_attributes_style/mod_declaration.stderr index 968c537c7e44..aec1e0b2cfef 100644 --- a/tests/ui/mixed_attributes_style/mod_declaration.stderr +++ b/tests/ui/mixed_attributes_style/mod_declaration.stderr @@ -1,3 +1,14 @@ +error: item has both inner and outer attributes + --> tests/ui/mixed_attributes_style/mod_declaration.rs:1:1 + | +LL | / #[path = "auxiliary/submodule.rs"] // don't lint. +LL | | /// This doc comment should not lint, it could be used to add context to the original module doc +LL | | mod submodule; + | |____________________^ + | + = note: `-D clippy::mixed-attributes-style` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::mixed_attributes_style)]` + error: item has both inner and outer attributes --> tests/ui/mixed_attributes_style/auxiliary/submodule.rs:5:1 | @@ -6,9 +17,6 @@ LL | | LL | | mod foo { LL | | #![allow(dead_code)] | |________________________^ - | - = note: `-D clippy::mixed-attributes-style` implied by `-D warnings` - = help: to override `-D warnings` add `#[allow(clippy::mixed_attributes_style)]` -error: aborting due to 1 previous error +error: aborting due to 2 previous errors