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]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.
Commit on github:
https://github.com/Rust-GCC/gccrs/commit/b98bdc90724f24e7458b7209499c5a7261afe258
The commit has been mentioned in the following pull-request(s):
- https://github.com/Rust-GCC/gccrs/pull/4358
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 1f465f2bd..e4429d8a5 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 000000000..af928db16
--- /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() {}
base-commit: 90ca1f6c6de2f374d6f74ee3e5154021f2dd27e5
--
2.52.0