Skip to content

Commit

Permalink
add @compile-flags to test file;
Browse files Browse the repository at this point in the history
turns out not linting in test mod is not a FN.
  • Loading branch information
J-ZhengLi committed Mar 23, 2024
1 parent 1c71163 commit e93658b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 67 deletions.
14 changes: 8 additions & 6 deletions clippy_lints/src/attrs/mixed_attributes_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -35,8 +36,11 @@ pub(super) fn check(cx: &LateContext<'_>, item_span: Span, attrs: &[Attribute])
let mut inner_attr_kind: FxHashSet<SimpleAttrKind> = FxHashSet::default();
let mut outer_attr_kind: FxHashSet<SimpleAttrKind> = 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;
}

Expand Down Expand Up @@ -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<SourceFile>, 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)
}
73 changes: 36 additions & 37 deletions tests/ui/mixed_attributes_style.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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 {
// 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]
}
48 changes: 28 additions & 20 deletions tests/ui/mixed_attributes_style.stderr
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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 | |
Expand All @@ -19,37 +19,45 @@ 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 {
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

16 changes: 12 additions & 4 deletions tests/ui/mixed_attributes_style/mod_declaration.stderr
Original file line number Diff line number Diff line change
@@ -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
|
Expand All @@ -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

0 comments on commit e93658b

Please sign in to comment.