Skip to content

Commit a1d4ad1

Browse files
committed
Suggest using NameValue syntax for malformed deprecated attribute
1 parent cced03b commit a1d4ad1

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/deprecation.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ impl SingleAttributeParser for DeprecatedParser {
7676
// ok
7777
}
7878
ArgParser::List(list) => {
79+
// If the argument list contains a single string literal, suggest using NameValue syntax
80+
if let Some(elem) = list.as_single() && let Some(lit) = elem.as_lit() && lit.kind.is_str() {
81+
let mut adcx = cx.adcx();
82+
if let Some(span) = args.span() {
83+
adcx.push_suggestion(String::from("try using `=` instead"), span, format!(" = {}", lit.kind));
84+
}
85+
adcx.expected_not_literal(elem.span());
86+
return None;
87+
}
88+
7989
for param in list.mixed() {
8090
let Some(param) = param.meta_item() else {
8191
cx.adcx().expected_not_literal(param.span());

tests/ui/deprecation/deprecation-sanity.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ LL | #[deprecated("test")]
5555
| ^^^^^^^^^^^^^------^^
5656
| |
5757
| didn't expect a literal here
58+
|
59+
help: try using `=` instead
60+
|
61+
LL - #[deprecated("test")]
62+
LL + #[deprecated = "test"]
63+
|
5864

5965
error: multiple `deprecated` attributes
6066
--> $DIR/deprecation-sanity.rs:29:1

tests/ui/error-codes/E0565-1.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ LL | #[deprecated("since")]
55
| ^^^^^^^^^^^^^-------^^
66
| |
77
| didn't expect a literal here
8+
|
9+
help: try using `=` instead
10+
|
11+
LL - #[deprecated("since")]
12+
LL + #[deprecated = "since"]
13+
|
814

915
error: aborting due to 1 previous error
1016

0 commit comments

Comments
 (0)