For now, ignore target checking for doc attributes in attr_parsing

This commit is contained in:
Guillaume Gomez 2025-12-10 20:18:42 +01:00
parent 9fdec8194e
commit 2bc2a0db69
9 changed files with 53 additions and 162 deletions

View file

@ -7,7 +7,7 @@ use rustc_hir::lints::AttributeLintKind;
use rustc_span::{Span, Symbol, edition, sym};
use thin_vec::ThinVec;
use super::prelude::{Allow, AllowedTargets, Error, MethodKind, Target};
use super::prelude::{ALL_TARGETS, AllowedTargets};
use super::{AcceptMapping, AttributeParser};
use crate::context::{AcceptContext, FinalizeContext, Stage};
use crate::parser::{ArgParser, MetaItemOrLitParser, MetaItemParser, PathParser};
@ -583,37 +583,39 @@ impl<S: Stage> AttributeParser<S> for DocParser {
this.accept_single_doc_attr(cx, args);
},
)];
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowListWarnRest(&[
Allow(Target::ExternCrate),
Allow(Target::Use),
Allow(Target::Static),
Allow(Target::Const),
Allow(Target::Fn),
Allow(Target::Mod),
Allow(Target::ForeignMod),
Allow(Target::TyAlias),
Allow(Target::Enum),
Allow(Target::Variant),
Allow(Target::Struct),
Allow(Target::Field),
Allow(Target::Union),
Allow(Target::Trait),
Allow(Target::TraitAlias),
Allow(Target::Impl { of_trait: true }),
Allow(Target::Impl { of_trait: false }),
Allow(Target::AssocConst),
Allow(Target::Method(MethodKind::Inherent)),
Allow(Target::Method(MethodKind::Trait { body: true })),
Allow(Target::Method(MethodKind::Trait { body: false })),
Allow(Target::Method(MethodKind::TraitImpl)),
Allow(Target::AssocTy),
Allow(Target::ForeignFn),
Allow(Target::ForeignStatic),
Allow(Target::ForeignTy),
Allow(Target::MacroDef),
Allow(Target::Crate),
Error(Target::WherePredicate),
]);
// FIXME: Currently emitted from 2 different places, generating duplicated warnings.
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
// const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowListWarnRest(&[
// Allow(Target::ExternCrate),
// Allow(Target::Use),
// Allow(Target::Static),
// Allow(Target::Const),
// Allow(Target::Fn),
// Allow(Target::Mod),
// Allow(Target::ForeignMod),
// Allow(Target::TyAlias),
// Allow(Target::Enum),
// Allow(Target::Variant),
// Allow(Target::Struct),
// Allow(Target::Field),
// Allow(Target::Union),
// Allow(Target::Trait),
// Allow(Target::TraitAlias),
// Allow(Target::Impl { of_trait: true }),
// Allow(Target::Impl { of_trait: false }),
// Allow(Target::AssocConst),
// Allow(Target::Method(MethodKind::Inherent)),
// Allow(Target::Method(MethodKind::Trait { body: true })),
// Allow(Target::Method(MethodKind::Trait { body: false })),
// Allow(Target::Method(MethodKind::TraitImpl)),
// Allow(Target::AssocTy),
// Allow(Target::ForeignFn),
// Allow(Target::ForeignStatic),
// Allow(Target::ForeignTy),
// Allow(Target::MacroDef),
// Allow(Target::Crate),
// Error(Target::WherePredicate),
// ]);
fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
if self.nb_doc_attrs != 0 {

View file

@ -12,8 +12,6 @@ struct X {
fn main() {
let _ = X {
#[doc(alias = "StructItem")]
//~^ WARN: attribute cannot be used on struct fields
//~| WARN: this was previously accepted by the compiler but is being phased out
foo: 123,
};
}

View file

@ -1,12 +0,0 @@
warning: `#[doc]` attribute cannot be used on struct fields
--> $DIR/issue-115264-expr-field.rs:14:9
|
LL | #[doc(alias = "StructItem")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= help: `#[doc]` can be applied to associated consts, associated types, constants, crates, data types, enum variants, extern crates, foreign modules, foreign statics, functions, impl blocks, macro defs, modules, statics, struct fields, trait aliases, traits, type aliases, unions, and use statements
= note: requested on the command line with `-W unused-attributes`
warning: 1 warning emitted

View file

@ -12,8 +12,6 @@ struct X {
fn main() {
let X {
#[doc(alias = "StructItem")]
//~^ WARN: attribute cannot be used on pattern fields
//~| WARN: this was previously accepted by the compiler but is being phased out
foo
} = X {
foo: 123

View file

@ -1,12 +0,0 @@
warning: `#[doc]` attribute cannot be used on pattern fields
--> $DIR/issue-115264-pat-field.rs:14:9
|
LL | #[doc(alias = "StructItem")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= help: `#[doc]` can be applied to associated consts, associated types, constants, crates, data types, enum variants, extern crates, foreign modules, foreign statics, functions, impl blocks, macro defs, modules, statics, struct fields, trait aliases, traits, type aliases, unions, and use statements
= note: requested on the command line with `-W unused-attributes`
warning: 1 warning emitted

View file

@ -18,8 +18,6 @@ fn foo() {
/// a //~ ERROR unused doc comment
#[doc(test(attr(allow(dead_code))))]
//~^ ERROR unused doc comment
//~| ERROR `#[doc]` attribute cannot be used on statements
//~| WARN this was previously accepted by the compiler
let x = 12;
/// multi-line //~ ERROR unused doc comment
@ -30,8 +28,6 @@ fn foo() {
1 => {},
#[doc(test(attr(allow(dead_code))))]
//~^ ERROR unused doc comment
//~| ERROR `#[doc]` attribute cannot be used on match arms [unused_attributes]
//~| WARN this was previously accepted by the compiler
_ => {}
}
@ -47,8 +43,6 @@ fn foo() {
#[doc(test(attr(allow(dead_code))))]
//~^ ERROR unused doc comment
//~| ERROR `#[doc]` attribute cannot be used on statements
//~| WARN this was previously accepted by the compiler
let x = /** comment */ 47; //~ ERROR unused doc comment
/// dox //~ ERROR unused doc comment

View file

@ -33,7 +33,7 @@ LL | unsafe extern "C" { }
= help: use `//` for a plain comment
error: unused doc comment
--> $DIR/useless-comment.rs:45:5
--> $DIR/useless-comment.rs:41:5
|
LL | /// bar
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ rustdoc does not generate documentation for macro invocations
@ -56,14 +56,14 @@ error: unused doc comment
|
LL | #[doc(test(attr(allow(dead_code))))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
LL |
LL | let x = 12;
| ----------- rustdoc does not generate documentation for statements
|
= help: use `//` for a plain comment
error: unused doc comment
--> $DIR/useless-comment.rs:25:5
--> $DIR/useless-comment.rs:23:5
|
LL | / /// multi-line
LL | | /// doc comment
@ -73,7 +73,7 @@ LL | / match x {
LL | | /// c
LL | | 1 => {},
LL | | #[doc(test(attr(allow(dead_code))))]
... |
LL | |
LL | | _ => {}
LL | | }
| |_____- rustdoc does not generate documentation for expressions
@ -81,7 +81,7 @@ LL | | }
= help: use `//` for a plain comment
error: unused doc comment
--> $DIR/useless-comment.rs:29:9
--> $DIR/useless-comment.rs:27:9
|
LL | /// c
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -91,18 +91,18 @@ LL | 1 => {},
= help: use `//` for a plain comment
error: unused doc comment
--> $DIR/useless-comment.rs:31:9
--> $DIR/useless-comment.rs:29:9
|
LL | #[doc(test(attr(allow(dead_code))))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
LL |
LL | _ => {}
| ------- rustdoc does not generate documentation for match arms
|
= help: use `//` for a plain comment
error: unused doc comment
--> $DIR/useless-comment.rs:38:5
--> $DIR/useless-comment.rs:34:5
|
LL | /// foo
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -112,7 +112,7 @@ LL | unsafe {}
= help: use `//` for a plain comment
error: unused doc comment
--> $DIR/useless-comment.rs:41:5
--> $DIR/useless-comment.rs:37:5
|
LL | #[doc = "foo"]
| ^^^^^^^^^^^^^^
@ -123,7 +123,7 @@ LL | 3;
= help: use `//` for a plain comment
error: unused doc comment
--> $DIR/useless-comment.rs:42:5
--> $DIR/useless-comment.rs:38:5
|
LL | #[doc = "bar"]
| ^^^^^^^^^^^^^^
@ -133,18 +133,18 @@ LL | 3;
= help: use `//` for a plain comment
error: unused doc comment
--> $DIR/useless-comment.rs:48:5
--> $DIR/useless-comment.rs:44:5
|
LL | #[doc(test(attr(allow(dead_code))))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
LL |
LL | let x = /** comment */ 47;
| -------------------------- rustdoc does not generate documentation for statements
|
= help: use `//` for a plain comment
error: unused doc comment
--> $DIR/useless-comment.rs:52:13
--> $DIR/useless-comment.rs:46:13
|
LL | let x = /** comment */ 47;
| ^^^^^^^^^^^^^^ -- rustdoc does not generate documentation for expressions
@ -152,7 +152,7 @@ LL | let x = /** comment */ 47;
= help: use `/* */` for a plain comment
error: unused doc comment
--> $DIR/useless-comment.rs:54:5
--> $DIR/useless-comment.rs:48:5
|
LL | /// dox
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -163,37 +163,5 @@ LL | | }
|
= help: use `//` for a plain comment
error: `#[doc]` attribute cannot be used on statements
--> $DIR/useless-comment.rs:19:5
|
LL | #[doc(test(attr(allow(dead_code))))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= help: `#[doc]` can be applied to associated consts, associated types, constants, crates, data types, enum variants, extern crates, foreign modules, foreign statics, functions, impl blocks, macro defs, modules, statics, struct fields, trait aliases, traits, type aliases, unions, and use statements
note: the lint level is defined here
--> $DIR/useless-comment.rs:4:9
|
LL | #![deny(unused_attributes)]
| ^^^^^^^^^^^^^^^^^
error: `#[doc]` attribute cannot be used on match arms
--> $DIR/useless-comment.rs:31:9
|
LL | #[doc(test(attr(allow(dead_code))))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= help: `#[doc]` can be applied to associated consts, associated types, constants, crates, data types, enum variants, extern crates, foreign modules, foreign statics, functions, impl blocks, macro defs, modules, statics, struct fields, trait aliases, traits, type aliases, unions, and use statements
error: `#[doc]` attribute cannot be used on statements
--> $DIR/useless-comment.rs:48:5
|
LL | #[doc(test(attr(allow(dead_code))))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= help: `#[doc]` can be applied to associated consts, associated types, constants, crates, data types, enum variants, extern crates, foreign modules, foreign statics, functions, impl blocks, macro defs, modules, statics, struct fields, trait aliases, traits, type aliases, unions, and use statements
error: aborting due to 18 previous errors
error: aborting due to 15 previous errors

View file

@ -21,22 +21,14 @@ impl Foo for Bar {
type X = i32;
fn foo(#[doc(alias = "qux")] _x: u32) -> Self::X {
//~^ ERROR
//~| WARN `#[doc]` attribute cannot be used on function params
//~| WARN: this was previously accepted by the compiler
#[doc(alias = "stmt")]
//~^ ERROR
//~| WARN `#[doc]` attribute cannot be used on statements
//~| WARN: this was previously accepted by the compiler
let x = 0;
#[doc(alias = "expr")]
//~^ ERROR
//~| WARN `#[doc]` attribute cannot be used on expressions
//~| WARN: this was previously accepted by the compiler
match x {
#[doc(alias = "arm")]
//~^ ERROR
//~| WARN `#[doc]` attribute cannot be used on match arms
//~| WARN: this was previously accepted by the compiler
_ => 0
}
}

View file

@ -29,59 +29,22 @@ LL | #[doc(alias = "assoc")]
| ^^^^^^^
error: `#[doc(alias = "...")]` isn't allowed on statement
--> $DIR/check-doc-alias-attr-location.rs:26:23
--> $DIR/check-doc-alias-attr-location.rs:24:23
|
LL | #[doc(alias = "stmt")]
| ^^^^^^
error: `#[doc(alias = "...")]` isn't allowed on expression
--> $DIR/check-doc-alias-attr-location.rs:31:23
--> $DIR/check-doc-alias-attr-location.rs:27:23
|
LL | #[doc(alias = "expr")]
| ^^^^^^
error: `#[doc(alias = "...")]` isn't allowed on match arm
--> $DIR/check-doc-alias-attr-location.rs:36:27
--> $DIR/check-doc-alias-attr-location.rs:30:27
|
LL | #[doc(alias = "arm")]
| ^^^^^
warning: `#[doc]` attribute cannot be used on function params
--> $DIR/check-doc-alias-attr-location.rs:22:12
|
LL | fn foo(#[doc(alias = "qux")] _x: u32) -> Self::X {
| ^^^^^^^^^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= help: `#[doc]` can be applied to associated consts, associated types, constants, crates, data types, enum variants, extern crates, foreign modules, foreign statics, functions, impl blocks, macro defs, modules, statics, struct fields, trait aliases, traits, type aliases, unions, and use statements
= note: requested on the command line with `-W unused-attributes`
warning: `#[doc]` attribute cannot be used on statements
--> $DIR/check-doc-alias-attr-location.rs:26:9
|
LL | #[doc(alias = "stmt")]
| ^^^^^^^^^^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= help: `#[doc]` can be applied to associated consts, associated types, constants, crates, data types, enum variants, extern crates, foreign modules, foreign statics, functions, impl blocks, macro defs, modules, statics, struct fields, trait aliases, traits, type aliases, unions, and use statements
warning: `#[doc]` attribute cannot be used on expressions
--> $DIR/check-doc-alias-attr-location.rs:31:9
|
LL | #[doc(alias = "expr")]
| ^^^^^^^^^^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= help: `#[doc]` can be applied to associated consts, associated types, constants, crates, data types, enum variants, extern crates, foreign modules, foreign statics, functions, impl blocks, macro defs, modules, statics, struct fields, trait aliases, traits, type aliases, unions, and use statements
warning: `#[doc]` attribute cannot be used on match arms
--> $DIR/check-doc-alias-attr-location.rs:36:13
|
LL | #[doc(alias = "arm")]
| ^^^^^^^^^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= help: `#[doc]` can be applied to associated consts, associated types, constants, crates, data types, enum variants, extern crates, foreign modules, foreign statics, functions, impl blocks, macro defs, modules, statics, struct fields, trait aliases, traits, type aliases, unions, and use statements
error: aborting due to 8 previous errors; 4 warnings emitted
error: aborting due to 8 previous errors