@@ -1835,43 +1835,56 @@ impl EarlyLintPass for EllipsisInclusiveRangePatterns {
18351835}
18361836
18371837declare_lint ! {
1838- UNNAMEABLE_TEST_FUNCTIONS ,
1838+ UNNAMEABLE_TEST_ITEMS ,
18391839 Warn ,
1840- "detects an function that cannot be named being marked as #[test]"
1840+ "detects an item that cannot be named being marked as #[test_case]" ,
1841+ report_in_external_macro: true
1842+ }
1843+
1844+ pub struct UnnameableTestItems {
1845+ boundary : ast:: NodeId , // NodeId of the item under which things are not nameable
1846+ items_nameable : bool ,
18411847}
18421848
1843- pub struct UnnameableTestFunctions ;
1849+ impl UnnameableTestItems {
1850+ pub fn new ( ) -> Self {
1851+ Self {
1852+ boundary : ast:: DUMMY_NODE_ID ,
1853+ items_nameable : true
1854+ }
1855+ }
1856+ }
18441857
1845- impl LintPass for UnnameableTestFunctions {
1858+ impl LintPass for UnnameableTestItems {
18461859 fn get_lints ( & self ) -> LintArray {
1847- lint_array ! ( UNNAMEABLE_TEST_FUNCTIONS )
1860+ lint_array ! ( UNNAMEABLE_TEST_ITEMS )
18481861 }
18491862}
18501863
1851- impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for UnnameableTestFunctions {
1864+ impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for UnnameableTestItems {
18521865 fn check_item ( & mut self , cx : & LateContext , it : & hir:: Item ) {
1853- match it. node {
1854- hir:: ItemKind :: Fn ( ..) => {
1855- for attr in & it. attrs {
1856- if attr. name ( ) == "test" {
1857- let parent = cx. tcx . hir . get_parent ( it. id ) ;
1858- match cx. tcx . hir . find ( parent) {
1859- Some ( Node :: Item ( hir:: Item { node : hir:: ItemKind :: Mod ( _) , ..} ) ) |
1860- None => { }
1861- _ => {
1862- cx. struct_span_lint (
1863- UNNAMEABLE_TEST_FUNCTIONS ,
1864- attr. span ,
1865- "cannot test inner function" ,
1866- ) . emit ( ) ;
1867- }
1868- }
1869- break ;
1870- }
1871- }
1866+ if self . items_nameable {
1867+ if let hir:: ItemKind :: Mod ( ..) = it. node { }
1868+ else {
1869+ self . items_nameable = false ;
1870+ self . boundary = it. id ;
18721871 }
1873- _ => return ,
1874- } ;
1872+ return ;
1873+ }
1874+
1875+ if let Some ( attr) = attr:: find_by_name ( & it. attrs , "rustc_test_marker" ) {
1876+ cx. struct_span_lint (
1877+ UNNAMEABLE_TEST_ITEMS ,
1878+ attr. span ,
1879+ "cannot test inner items" ,
1880+ ) . emit ( ) ;
1881+ }
1882+ }
1883+
1884+ fn check_item_post ( & mut self , _cx : & LateContext , it : & hir:: Item ) {
1885+ if !self . items_nameable && self . boundary == it. id {
1886+ self . items_nameable = true ;
1887+ }
18751888 }
18761889}
18771890
0 commit comments