diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 7769f59210d56..2a0901e0243b6 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -4076,8 +4076,9 @@ declare_lint! { /// ### Example /// /// ```rust,ignore (needs CLI args, platform-specific) - /// #[warn(linker_messages)] - /// extern "C" { + /// #![warn(linker_messages)] + /// + /// unsafe extern "C" { /// fn foo(); /// } /// fn main () { unsafe { foo(); } } @@ -4112,7 +4113,8 @@ declare_lint! { // Linker messages don't live up to the high standard people expect of rustc's errors. // Prevent `-D warnings` from applying to it. // It's still possible to pass `-D linker-messages` specifically. - ignore_deny_warnings + ignore_deny_warnings, + crate_level_only } declare_lint! { @@ -4121,7 +4123,8 @@ declare_lint! { /// ### Example /// /// ```rust,ignore (needs CLI args, platform-specific) - /// #[warn(linker_info)] + /// #![warn(linker_info)] + /// /// fn main () {} /// ``` /// @@ -4146,7 +4149,8 @@ declare_lint! { /// . pub LINKER_INFO, Allow, - "linker warnings known to be informational-only and not indicative of a problem" + "linker warnings known to be informational-only and not indicative of a problem", + crate_level_only } declare_lint! { diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 597c920976ede..a9232aa83f408 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -9,7 +9,7 @@ use std::cell::Cell; use std::slice; use rustc_abi::ExternAbi; -use rustc_ast::{AttrStyle, MetaItemKind, ast}; +use rustc_ast::MetaItemKind; use rustc_attr_parsing::AttributeParser; use rustc_data_structures::thin_vec::ThinVec; use rustc_errors::{DiagCtxtHandle, IntoDiagArg, MultiSpan, msg}; @@ -138,9 +138,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> { match attr { Attribute::Parsed(attr_kind) => { self.check_one_parsed_attribute(hir_id, span, target, item, attr_kind); - self.check_unused_attribute(hir_id, attr, None); + self.check_unused_attribute(hir_id, attr); } - Attribute::Unparsed(attr_item) => { + Attribute::Unparsed(_) => { match attr.path().as_slice() { // ok [sym::allow | sym::expect | sym::warn | sym::deny | sym::forbid, ..] => {} @@ -167,7 +167,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { [] => unreachable!(), } - self.check_unused_attribute(hir_id, attr, Some(attr_item.style)); + self.check_unused_attribute(hir_id, attr); } } } @@ -275,7 +275,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { AttributeKind::LinkName { .. } => (), AttributeKind::LinkOrdinal { .. } => (), AttributeKind::LinkSection { .. } => (), - AttributeKind::LoopMatch(..) => {} + AttributeKind::LoopMatch(..) => (), AttributeKind::MacroEscape => (), AttributeKind::MacroUse { .. } => (), AttributeKind::Marker => (), @@ -1316,7 +1316,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { } } - fn check_unused_attribute(&self, hir_id: HirId, attr: &Attribute, style: Option) { + fn check_unused_attribute(&self, hir_id: HirId, attr: &Attribute) { // Warn on useless empty attributes. // FIXME(jdonszelmann): this lint should be moved to attribute parsing, see `AcceptContext::warn_empty_attribute` let note = @@ -1351,34 +1351,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> { }) { if hir_id != CRATE_HIR_ID { - match style { - Some(ast::AttrStyle::Outer) => { - let attr_span = attr.span(); - let bang_position = self - .tcx - .sess - .source_map() - .span_until_char(attr_span, '[') - .shrink_to_hi(); - - self.tcx.emit_node_span_lint( - UNUSED_ATTRIBUTES, - hir_id, - attr_span, - diagnostics::OuterCrateLevelAttr { - suggestion: diagnostics::OuterCrateLevelAttrSuggestion { - bang_position, - }, - }, - ) - } - Some(ast::AttrStyle::Inner) | None => self.tcx.emit_node_span_lint( - UNUSED_ATTRIBUTES, - hir_id, - attr.span(), - diagnostics::InnerCrateLevelAttr, - ), - }; return; } else { let never_needs_link = self @@ -1401,8 +1373,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> { && !self.tcx.crate_types().contains(&CrateType::Executable) { diagnostics::UnusedNote::NoEffectDeadCodePubInBinary - } else if attr.has_name(sym::default_method_body_is_const) { - diagnostics::UnusedNote::DefaultMethodBodyConst } else { return; }; diff --git a/compiler/rustc_passes/src/diagnostics.rs b/compiler/rustc_passes/src/diagnostics.rs index 6ea0dc2c8f244..55f8ac9a585de 100644 --- a/compiler/rustc_passes/src/diagnostics.rs +++ b/compiler/rustc_passes/src/diagnostics.rs @@ -28,24 +28,6 @@ pub(crate) struct MixedExportNameAndNoMangle { pub export_name_attr: &'static str, } -#[derive(Diagnostic)] -#[diag("crate-level attribute should be an inner attribute")] -pub(crate) struct OuterCrateLevelAttr { - #[subdiagnostic] - pub suggestion: OuterCrateLevelAttrSuggestion, -} - -#[derive(Subdiagnostic)] -#[multipart_suggestion("add a `!`", style = "verbose")] -pub(crate) struct OuterCrateLevelAttrSuggestion { - #[suggestion_part(code = "!")] - pub bang_position: Span, -} - -#[derive(Diagnostic)] -#[diag("crate-level attribute should be in the root module")] -pub(crate) struct InnerCrateLevelAttr; - #[derive(Diagnostic)] #[diag("`#[doc(alias = \"...\")]` isn't allowed on {$location}")] pub(crate) struct DocAliasBadLocation<'a> { @@ -237,8 +219,6 @@ pub(crate) enum UnusedNote { EmptyList { name: Symbol }, #[note("attribute `{$name}` without any lints has no effect")] NoLints { name: Symbol }, - #[note("`default_method_body_is_const` has been replaced with `const` on traits")] - DefaultMethodBodyConst, #[note( "the `linker_messages` and `linker_info` lints can only be controlled at the root of a crate that needs to be linked" )] diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 4c1b9c78b7963..c07d84cec0481 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -805,7 +805,6 @@ symbols! { default_field_values, default_fn, default_lib_allocator, - default_method_body_is_const, // -------------------------- // Lang items which are used only for experiments with auto traits with default bounds. // These lang items are not actually defined in core/std. Experiment is a part of diff --git a/tests/ui/lint/linker-warning.rs b/tests/ui/lint/linker-warning.rs index 10e3f56ab9590..02c9decd5607c 100644 --- a/tests/ui/lint/linker-warning.rs +++ b/tests/ui/lint/linker-warning.rs @@ -2,8 +2,8 @@ #![crate_type = "lib"] #![warn(unused_attributes)] #![allow(linker_messages)] -//~^ WARNING unused attribute +//~^ WARN unused attribute #[allow(linker_messages)] -//~^ WARNING should be an inner attribute +//~^ WARN allow(linker_messages) is ignored unless specified at crate level fn foo() {} diff --git a/tests/ui/lint/linker-warning.stderr b/tests/ui/lint/linker-warning.stderr index 54994c48ea43a..39e6522a45f66 100644 --- a/tests/ui/lint/linker-warning.stderr +++ b/tests/ui/lint/linker-warning.stderr @@ -1,18 +1,14 @@ -warning: crate-level attribute should be an inner attribute - --> $DIR/linker-warning.rs:7:1 +warning: allow(linker_messages) is ignored unless specified at crate level + --> $DIR/linker-warning.rs:7:9 | LL | #[allow(linker_messages)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^ | note: the lint level is defined here --> $DIR/linker-warning.rs:3:9 | LL | #![warn(unused_attributes)] | ^^^^^^^^^^^^^^^^^ -help: add a `!` - | -LL | #![allow(linker_messages)] - | + warning: unused attribute --> $DIR/linker-warning.rs:4:1 diff --git a/tests/ui/lint/pre-expansion-parsing.rs b/tests/ui/lint/pre-expansion-parsing.rs new file mode 100644 index 0000000000000..3df56775c6c5f --- /dev/null +++ b/tests/ui/lint/pre-expansion-parsing.rs @@ -0,0 +1,11 @@ +#![crate_type = "lib"] + +#[expect] // OK +#[expect[wut]] // OK +#[expect(expect)] // OK +#[expect(expect(expect))] //~ ERROR malformed lint attribute input + //~| ERROR malformed lint attribute input +#[deny(({!}))] // OK +#[expect[helix::]] // OK +#[cfg(false)] +const _: () = (); diff --git a/tests/ui/lint/pre-expansion-parsing.stderr b/tests/ui/lint/pre-expansion-parsing.stderr new file mode 100644 index 0000000000000..c0c4e792b676f --- /dev/null +++ b/tests/ui/lint/pre-expansion-parsing.stderr @@ -0,0 +1,17 @@ +error[E0452]: malformed lint attribute input + --> $DIR/pre-expansion-parsing.rs:6:10 + | +LL | #[expect(expect(expect))] + | ^^^^^^^^^^^^^^ bad attribute argument + +error[E0452]: malformed lint attribute input + --> $DIR/pre-expansion-parsing.rs:6:10 + | +LL | #[expect(expect(expect))] + | ^^^^^^^^^^^^^^ bad attribute argument + | + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0452`.