From 6f32e3e187cfdf29ef7c785c644c95aee2897364 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 3 Mar 2021 23:00:17 +0100 Subject: [PATCH 1/5] Add new edition 2021 link: INVALID_DOC_ATTRIBUTE --- compiler/rustc_lint_defs/src/builtin.rs | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 12d849e3b9466..11d973dd1d816 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -3059,3 +3059,30 @@ declare_lint! { Allow, "No declared ABI for extern declaration" } + +declare_lint! { + /// The `invalid_doc_attributes` lint detects when the `#[doc(...)]` is + /// misused. + /// + /// ### Example + /// + /// ```rust,compile_fail + /// #![deny(warnings)] + /// #[doc(test(no_crate_inject))] + /// ``` + /// + /// {{produces}} + /// + /// ### Explanation + /// + /// Previously, there were very like checks being performed on `#[doc(..)]` + /// unlike the other attributes. It'll now catch all the issues that it + /// silently ignored previously. + pub INVALID_DOC_ATTRIBUTE, + Warn, + "detects invalid `#[doc(...)]` attributes", + @future_incompatible = FutureIncompatibleInfo { + reference: "issue #82730 ", + edition: Some(Edition::Edition2021), + }; +} From a66bf524c298ff177c3d3046ff8f2d02a81e4bc9 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 2 Mar 2021 21:10:05 +0100 Subject: [PATCH 2/5] Add extra check for #[doc(test(...)] attribute --- compiler/rustc_lint_defs/src/builtin.rs | 9 ++++++--- compiler/rustc_passes/src/check_attr.rs | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 11d973dd1d816..b099dcf0300dd 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -3061,14 +3061,17 @@ declare_lint! { } declare_lint! { - /// The `invalid_doc_attributes` lint detects when the `#[doc(...)]` is + /// The `invalid_doc_attribute` lint detects when the `#[doc(...)]` is /// misused. /// /// ### Example /// /// ```rust,compile_fail /// #![deny(warnings)] - /// #[doc(test(no_crate_inject))] + /// + /// pub mod submodule { + /// #![doc(test(no_crate_inject))] + /// } /// ``` /// /// {{produces}} @@ -3083,6 +3086,6 @@ declare_lint! { "detects invalid `#[doc(...)]` attributes", @future_incompatible = FutureIncompatibleInfo { reference: "issue #82730 ", - edition: Some(Edition::Edition2021), + edition: None, }; } diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 39245ea77e52c..c84d29428ad35 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -17,7 +17,9 @@ use rustc_hir::{ self, FnSig, ForeignItem, ForeignItemKind, HirId, Item, ItemKind, TraitItem, CRATE_HIR_ID, }; use rustc_hir::{MethodKind, Target}; -use rustc_session::lint::builtin::{CONFLICTING_REPR_HINTS, UNUSED_ATTRIBUTES}; +use rustc_session::lint::builtin::{ + CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTE, UNUSED_ATTRIBUTES, +}; use rustc_session::parse::feature_err; use rustc_span::symbol::{sym, Symbol}; use rustc_span::{Span, DUMMY_SP}; @@ -544,6 +546,21 @@ impl CheckAttrVisitor<'tcx> { { return false; } + } else if meta.has_name(sym::test) { + if CRATE_HIR_ID != hir_id { + self.tcx.struct_span_lint_hir( + INVALID_DOC_ATTRIBUTE, + hir_id, + meta.span(), + |lint| { + lint.build( + "`#![doc(test(...)]` is only allowed as a crate level attribute" + ) + .emit(); + }, + ); + return false; + } } else if let Some(i_meta) = meta.meta_item() { if ![ sym::cfg, From 85c3d102bb1e03838e0fcccb820b47d1896b2455 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 2 Mar 2021 21:10:30 +0100 Subject: [PATCH 3/5] Add tests for #[doc(test(...)] check --- src/test/rustdoc-ui/doc-attr2.rs | 11 +++++++++++ src/test/rustdoc-ui/doc-attr2.stderr | 26 +++++++++++++++++++++++++ src/test/ui/attributes/doc-attr2.rs | 11 +++++++++++ src/test/ui/attributes/doc-attr2.stderr | 26 +++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 src/test/rustdoc-ui/doc-attr2.rs create mode 100644 src/test/rustdoc-ui/doc-attr2.stderr create mode 100644 src/test/ui/attributes/doc-attr2.rs create mode 100644 src/test/ui/attributes/doc-attr2.stderr diff --git a/src/test/rustdoc-ui/doc-attr2.rs b/src/test/rustdoc-ui/doc-attr2.rs new file mode 100644 index 0000000000000..3fb484644d7fa --- /dev/null +++ b/src/test/rustdoc-ui/doc-attr2.rs @@ -0,0 +1,11 @@ +#![crate_type = "lib"] +#![deny(warnings)] + +#[doc(test(no_crate_inject))] //~ ERROR +//~^ WARN +pub fn foo() {} + +pub mod bar { + #![doc(test(no_crate_inject))] //~ ERROR + //~^ WARN +} diff --git a/src/test/rustdoc-ui/doc-attr2.stderr b/src/test/rustdoc-ui/doc-attr2.stderr new file mode 100644 index 0000000000000..7bfc37817afe6 --- /dev/null +++ b/src/test/rustdoc-ui/doc-attr2.stderr @@ -0,0 +1,26 @@ +error: `#![doc(test(...)]` is only allowed as a crate level attribute + --> $DIR/doc-attr2.rs:4:7 + | +LL | #[doc(test(no_crate_inject))] + | ^^^^^^^^^^^^^^^^^^^^^ + | +note: the lint level is defined here + --> $DIR/doc-attr2.rs:2:9 + | +LL | #![deny(warnings)] + | ^^^^^^^^ + = note: `#[deny(invalid_doc_attribute)]` implied by `#[deny(warnings)]` + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #82730 + +error: `#![doc(test(...)]` is only allowed as a crate level attribute + --> $DIR/doc-attr2.rs:9:12 + | +LL | #![doc(test(no_crate_inject))] + | ^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #82730 + +error: aborting due to 2 previous errors + diff --git a/src/test/ui/attributes/doc-attr2.rs b/src/test/ui/attributes/doc-attr2.rs new file mode 100644 index 0000000000000..3fb484644d7fa --- /dev/null +++ b/src/test/ui/attributes/doc-attr2.rs @@ -0,0 +1,11 @@ +#![crate_type = "lib"] +#![deny(warnings)] + +#[doc(test(no_crate_inject))] //~ ERROR +//~^ WARN +pub fn foo() {} + +pub mod bar { + #![doc(test(no_crate_inject))] //~ ERROR + //~^ WARN +} diff --git a/src/test/ui/attributes/doc-attr2.stderr b/src/test/ui/attributes/doc-attr2.stderr new file mode 100644 index 0000000000000..7bfc37817afe6 --- /dev/null +++ b/src/test/ui/attributes/doc-attr2.stderr @@ -0,0 +1,26 @@ +error: `#![doc(test(...)]` is only allowed as a crate level attribute + --> $DIR/doc-attr2.rs:4:7 + | +LL | #[doc(test(no_crate_inject))] + | ^^^^^^^^^^^^^^^^^^^^^ + | +note: the lint level is defined here + --> $DIR/doc-attr2.rs:2:9 + | +LL | #![deny(warnings)] + | ^^^^^^^^ + = note: `#[deny(invalid_doc_attribute)]` implied by `#[deny(warnings)]` + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #82730 + +error: `#![doc(test(...)]` is only allowed as a crate level attribute + --> $DIR/doc-attr2.rs:9:12 + | +LL | #![doc(test(no_crate_inject))] + | ^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #82730 + +error: aborting due to 2 previous errors + From 55cec9079deb3aec02bc8158de94284a42a0ee79 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 4 Mar 2021 21:48:07 +0100 Subject: [PATCH 4/5] Also use INVALID_DOC_ATTRIBUTE for "unknown doc attribute" warnings --- compiler/rustc_passes/src/check_attr.rs | 7 +------ src/test/rustdoc-ui/doc-attr.rs | 7 +++---- src/test/rustdoc-ui/doc-attr.stderr | 11 +++++++---- src/test/ui/attributes/doc-attr.rs | 7 +++---- src/test/ui/attributes/doc-attr.stderr | 11 +++++++---- 5 files changed, 21 insertions(+), 22 deletions(-) diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index c84d29428ad35..a868802b4a6aa 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -585,7 +585,7 @@ impl CheckAttrVisitor<'tcx> { .any(|m| i_meta.has_name(*m)) { self.tcx.struct_span_lint_hir( - UNUSED_ATTRIBUTES, + INVALID_DOC_ATTRIBUTE, hir_id, i_meta.span, |lint| { @@ -593,11 +593,6 @@ impl CheckAttrVisitor<'tcx> { "unknown `doc` attribute `{}`", i_meta.name_or_empty() )) - .warn( - "this was previously accepted by the compiler but is \ - being phased out; it will become a hard error in \ - a future release!", - ) .emit(); }, ); diff --git a/src/test/rustdoc-ui/doc-attr.rs b/src/test/rustdoc-ui/doc-attr.rs index 3519b5707b375..3a5841129734d 100644 --- a/src/test/rustdoc-ui/doc-attr.rs +++ b/src/test/rustdoc-ui/doc-attr.rs @@ -1,11 +1,10 @@ #![crate_type = "lib"] -#![deny(unused_attributes)] -//~^ NOTE lint level is defined here +#![deny(warnings)] #![doc(as_ptr)] //~^ ERROR unknown `doc` attribute -//~| WARNING will become a hard error in a future release +//~^^ WARN #[doc(as_ptr)] //~^ ERROR unknown `doc` attribute -//~| WARNING will become a hard error in a future release +//~^^ WARN pub fn foo() {} diff --git a/src/test/rustdoc-ui/doc-attr.stderr b/src/test/rustdoc-ui/doc-attr.stderr index 9666db2b10e01..251af7c1502d5 100644 --- a/src/test/rustdoc-ui/doc-attr.stderr +++ b/src/test/rustdoc-ui/doc-attr.stderr @@ -1,5 +1,5 @@ error: unknown `doc` attribute `as_ptr` - --> $DIR/doc-attr.rs:8:7 + --> $DIR/doc-attr.rs:7:7 | LL | #[doc(as_ptr)] | ^^^^^^ @@ -7,17 +7,20 @@ LL | #[doc(as_ptr)] note: the lint level is defined here --> $DIR/doc-attr.rs:2:9 | -LL | #![deny(unused_attributes)] - | ^^^^^^^^^^^^^^^^^ +LL | #![deny(warnings)] + | ^^^^^^^^ + = note: `#[deny(invalid_doc_attribute)]` implied by `#[deny(warnings)]` = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #82730 error: unknown `doc` attribute `as_ptr` - --> $DIR/doc-attr.rs:4:8 + --> $DIR/doc-attr.rs:3:8 | LL | #![doc(as_ptr)] | ^^^^^^ | = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #82730 error: aborting due to 2 previous errors diff --git a/src/test/ui/attributes/doc-attr.rs b/src/test/ui/attributes/doc-attr.rs index 3519b5707b375..3a5841129734d 100644 --- a/src/test/ui/attributes/doc-attr.rs +++ b/src/test/ui/attributes/doc-attr.rs @@ -1,11 +1,10 @@ #![crate_type = "lib"] -#![deny(unused_attributes)] -//~^ NOTE lint level is defined here +#![deny(warnings)] #![doc(as_ptr)] //~^ ERROR unknown `doc` attribute -//~| WARNING will become a hard error in a future release +//~^^ WARN #[doc(as_ptr)] //~^ ERROR unknown `doc` attribute -//~| WARNING will become a hard error in a future release +//~^^ WARN pub fn foo() {} diff --git a/src/test/ui/attributes/doc-attr.stderr b/src/test/ui/attributes/doc-attr.stderr index 9666db2b10e01..251af7c1502d5 100644 --- a/src/test/ui/attributes/doc-attr.stderr +++ b/src/test/ui/attributes/doc-attr.stderr @@ -1,5 +1,5 @@ error: unknown `doc` attribute `as_ptr` - --> $DIR/doc-attr.rs:8:7 + --> $DIR/doc-attr.rs:7:7 | LL | #[doc(as_ptr)] | ^^^^^^ @@ -7,17 +7,20 @@ LL | #[doc(as_ptr)] note: the lint level is defined here --> $DIR/doc-attr.rs:2:9 | -LL | #![deny(unused_attributes)] - | ^^^^^^^^^^^^^^^^^ +LL | #![deny(warnings)] + | ^^^^^^^^ + = note: `#[deny(invalid_doc_attribute)]` implied by `#[deny(warnings)]` = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #82730 error: unknown `doc` attribute `as_ptr` - --> $DIR/doc-attr.rs:4:8 + --> $DIR/doc-attr.rs:3:8 | LL | #![doc(as_ptr)] | ^^^^^^ | = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #82730 error: aborting due to 2 previous errors From a11e87e74d3ad5f6b9f628faba85739f067108a6 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 5 Mar 2021 14:44:31 +0100 Subject: [PATCH 5/5] Make invalid_doc_attribute lint plural --- compiler/rustc_lint_defs/src/builtin.rs | 4 ++-- compiler/rustc_passes/src/check_attr.rs | 6 +++--- src/test/rustdoc-ui/doc-attr.stderr | 2 +- src/test/rustdoc-ui/doc-attr2.stderr | 2 +- src/test/ui/attributes/doc-attr.stderr | 2 +- src/test/ui/attributes/doc-attr2.stderr | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index b099dcf0300dd..e6f81ce828ffa 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -3061,7 +3061,7 @@ declare_lint! { } declare_lint! { - /// The `invalid_doc_attribute` lint detects when the `#[doc(...)]` is + /// The `invalid_doc_attributes` lint detects when the `#[doc(...)]` is /// misused. /// /// ### Example @@ -3081,7 +3081,7 @@ declare_lint! { /// Previously, there were very like checks being performed on `#[doc(..)]` /// unlike the other attributes. It'll now catch all the issues that it /// silently ignored previously. - pub INVALID_DOC_ATTRIBUTE, + pub INVALID_DOC_ATTRIBUTES, Warn, "detects invalid `#[doc(...)]` attributes", @future_incompatible = FutureIncompatibleInfo { diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index a868802b4a6aa..ed971951b0f76 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -18,7 +18,7 @@ use rustc_hir::{ }; use rustc_hir::{MethodKind, Target}; use rustc_session::lint::builtin::{ - CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTE, UNUSED_ATTRIBUTES, + CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTES, UNUSED_ATTRIBUTES, }; use rustc_session::parse::feature_err; use rustc_span::symbol::{sym, Symbol}; @@ -549,7 +549,7 @@ impl CheckAttrVisitor<'tcx> { } else if meta.has_name(sym::test) { if CRATE_HIR_ID != hir_id { self.tcx.struct_span_lint_hir( - INVALID_DOC_ATTRIBUTE, + INVALID_DOC_ATTRIBUTES, hir_id, meta.span(), |lint| { @@ -585,7 +585,7 @@ impl CheckAttrVisitor<'tcx> { .any(|m| i_meta.has_name(*m)) { self.tcx.struct_span_lint_hir( - INVALID_DOC_ATTRIBUTE, + INVALID_DOC_ATTRIBUTES, hir_id, i_meta.span, |lint| { diff --git a/src/test/rustdoc-ui/doc-attr.stderr b/src/test/rustdoc-ui/doc-attr.stderr index 251af7c1502d5..21479d25fc274 100644 --- a/src/test/rustdoc-ui/doc-attr.stderr +++ b/src/test/rustdoc-ui/doc-attr.stderr @@ -9,7 +9,7 @@ note: the lint level is defined here | LL | #![deny(warnings)] | ^^^^^^^^ - = note: `#[deny(invalid_doc_attribute)]` implied by `#[deny(warnings)]` + = note: `#[deny(invalid_doc_attributes)]` implied by `#[deny(warnings)]` = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 diff --git a/src/test/rustdoc-ui/doc-attr2.stderr b/src/test/rustdoc-ui/doc-attr2.stderr index 7bfc37817afe6..eeb2c2be08551 100644 --- a/src/test/rustdoc-ui/doc-attr2.stderr +++ b/src/test/rustdoc-ui/doc-attr2.stderr @@ -9,7 +9,7 @@ note: the lint level is defined here | LL | #![deny(warnings)] | ^^^^^^^^ - = note: `#[deny(invalid_doc_attribute)]` implied by `#[deny(warnings)]` + = note: `#[deny(invalid_doc_attributes)]` implied by `#[deny(warnings)]` = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 diff --git a/src/test/ui/attributes/doc-attr.stderr b/src/test/ui/attributes/doc-attr.stderr index 251af7c1502d5..21479d25fc274 100644 --- a/src/test/ui/attributes/doc-attr.stderr +++ b/src/test/ui/attributes/doc-attr.stderr @@ -9,7 +9,7 @@ note: the lint level is defined here | LL | #![deny(warnings)] | ^^^^^^^^ - = note: `#[deny(invalid_doc_attribute)]` implied by `#[deny(warnings)]` + = note: `#[deny(invalid_doc_attributes)]` implied by `#[deny(warnings)]` = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 diff --git a/src/test/ui/attributes/doc-attr2.stderr b/src/test/ui/attributes/doc-attr2.stderr index 7bfc37817afe6..eeb2c2be08551 100644 --- a/src/test/ui/attributes/doc-attr2.stderr +++ b/src/test/ui/attributes/doc-attr2.stderr @@ -9,7 +9,7 @@ note: the lint level is defined here | LL | #![deny(warnings)] | ^^^^^^^^ - = note: `#[deny(invalid_doc_attribute)]` implied by `#[deny(warnings)]` + = note: `#[deny(invalid_doc_attributes)]` implied by `#[deny(warnings)]` = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730