From: Jayant Chauhan <[email protected]>
Emit a diagnostic when lint attributes (allow, deny, warn, forbid) are
used without arguments. Previously, these attributes were accepted silently
if malformed, which could lead to confusion or ignored lints. This ensures
users are informed of the expected form: #[allow(lint_name)].
Fixes Rust-GCC#4358
gcc/rust/ChangeLog:
* util/rust-attributes.cc (check_lint_attribute): New helper.
(AttributeChecker::visit): Call helper for lint attributes on functions.
gcc/testsuite/ChangeLog:
* rust/compile/issue-4225.rs: New test.
Signed-off-by: Jayant Chauhan <[email protected]>
---
gcc/rust/util/rust-attributes.cc | 17 +++++++++++++++++
gcc/testsuite/rust/compile/issue-4225.rs | 10 ++++++++++
2 files changed, 27 insertions(+)
create mode 100644 gcc/testsuite/rust/compile/issue-4225.rs
diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc
index 1f465f2bda1..e4429d8a550 100644
--- a/gcc/rust/util/rust-attributes.cc
+++ b/gcc/rust/util/rust-attributes.cc
@@ -503,6 +503,18 @@ check_export_name_attribute (const AST::Attribute
&attribute)
}
}
+static void
+check_lint_attribute (const AST::Attribute &attribute, const char *name)
+{
+ if (!attribute.has_attr_input ())
+ {
+ rust_error_at (attribute.get_locus (), "malformed %qs attribute input",
+ name);
+ rust_inform (attribute.get_locus (),
+ "must be of the form: %<#[%s(lint1, lint2, ...)]%>", name);
+ }
+}
+
void
AttributeChecker::check_attribute (const AST::Attribute &attribute)
{
@@ -977,6 +989,11 @@ AttributeChecker::visit (AST::Function &fun)
{
check_export_name_attribute (attribute);
}
+ else if (result.name == Attrs::ALLOW || result.name == "deny"
+ || result.name == "warn" || result.name == "forbid")
+ {
+ check_lint_attribute (attribute, name);
+ }
else if (result.name == Attrs::LINK_NAME)
{
if (!attribute.has_attr_input ())
diff --git a/gcc/testsuite/rust/compile/issue-4225.rs
b/gcc/testsuite/rust/compile/issue-4225.rs
new file mode 100644
index 00000000000..af928db1654
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4225.rs
@@ -0,0 +1,10 @@
+#![feature(no_core)]
+#![no_core]
+// { dg-options "-w" }
+
+#[allow] // { dg-error "malformed .allow. attribute input" }
+fn foo() {}
+// { dg-note "must be of the form" "" { target *-*-* } .-2 }
+
+#[allow(dead_code)] // This is a correctly formed attribute
+fn bar() {}
--
2.50.1