Skip to content

Commit 149bdde

Browse files
Fix #[must_use = 1] not giving an error
Signed-off-by: Jonathan Brouwer <[email protected]>
1 parent 86b54d5 commit 149bdde

File tree

5 files changed

+81
-44
lines changed

5 files changed

+81
-44
lines changed

compiler/rustc_attr_parsing/src/attributes/must_use.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,16 @@ impl<S: Stage> SingleAttributeParser<S> for MustUseParser {
2121
span: cx.attr_span,
2222
reason: match args {
2323
ArgParser::NoArgs => None,
24-
ArgParser::NameValue(name_value) => name_value.value_as_str(),
24+
ArgParser::NameValue(name_value) => {
25+
let Some(value_str) = name_value.value_as_str() else {
26+
cx.expected_string_literal(
27+
name_value.value_span,
28+
Some(&name_value.value_as_lit()),
29+
);
30+
return None;
31+
};
32+
Some(value_str)
33+
}
2534
ArgParser::List(_) => {
2635
let suggestions =
2736
<Self as SingleAttributeParser<S>>::TEMPLATE.suggestions(false, "must_use");

tests/ui/attributes/malformed-attrs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ fn test() {
115115
#[proc_macro_attribute = 19]
116116
//~^ ERROR malformed
117117
//~| ERROR the `#[proc_macro_attribute]` attribute is only usable with crates of the `proc-macro` crate type
118+
#[must_use = 1]
119+
//~^ ERROR malformed
118120
fn test2() { }
119121

120122
#[proc_macro_derive]

tests/ui/attributes/malformed-attrs.stderr

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ LL | #[cfg_attr(condition, attribute, other_attribute, ...)]
1717
| ++++++++++++++++++++++++++++++++++++++++++++
1818

1919
error[E0463]: can't find crate for `wloop`
20-
--> $DIR/malformed-attrs.rs:208:1
20+
--> $DIR/malformed-attrs.rs:210:1
2121
|
2222
LL | extern crate wloop;
2323
| ^^^^^^^^^^^^^^^^^^^ can't find crate
@@ -120,25 +120,25 @@ LL | #[proc_macro_attribute = 19]
120120
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[proc_macro_attribute]`
121121

122122
error: malformed `proc_macro_derive` attribute input
123-
--> $DIR/malformed-attrs.rs:120:1
123+
--> $DIR/malformed-attrs.rs:122:1
124124
|
125125
LL | #[proc_macro_derive]
126126
| ^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[proc_macro_derive(TraitName, /*opt*/ attributes(name1, name2, ...))]`
127127

128128
error: malformed `rustc_layout_scalar_valid_range_start` attribute input
129-
--> $DIR/malformed-attrs.rs:125:1
129+
--> $DIR/malformed-attrs.rs:127:1
130130
|
131131
LL | #[rustc_layout_scalar_valid_range_start]
132132
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[rustc_layout_scalar_valid_range_start(value)]`
133133

134134
error: malformed `rustc_layout_scalar_valid_range_end` attribute input
135-
--> $DIR/malformed-attrs.rs:127:1
135+
--> $DIR/malformed-attrs.rs:129:1
136136
|
137137
LL | #[rustc_layout_scalar_valid_range_end]
138138
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[rustc_layout_scalar_valid_range_end(value)]`
139139

140140
error: malformed `must_not_suspend` attribute input
141-
--> $DIR/malformed-attrs.rs:129:1
141+
--> $DIR/malformed-attrs.rs:131:1
142142
|
143143
LL | #[must_not_suspend()]
144144
| ^^^^^^^^^^^^^^^^^^^^^
@@ -153,115 +153,115 @@ LL + #[must_not_suspend]
153153
|
154154

155155
error: malformed `cfi_encoding` attribute input
156-
--> $DIR/malformed-attrs.rs:131:1
156+
--> $DIR/malformed-attrs.rs:133:1
157157
|
158158
LL | #[cfi_encoding]
159159
| ^^^^^^^^^^^^^^^ help: must be of the form: `#[cfi_encoding = "encoding"]`
160160

161161
error: malformed `type_const` attribute input
162-
--> $DIR/malformed-attrs.rs:140:5
162+
--> $DIR/malformed-attrs.rs:142:5
163163
|
164164
LL | #[type_const = 1]
165165
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#[type_const]`
166166

167167
error: malformed `marker` attribute input
168-
--> $DIR/malformed-attrs.rs:152:1
168+
--> $DIR/malformed-attrs.rs:154:1
169169
|
170170
LL | #[marker = 3]
171171
| ^^^^^^^^^^^^^ help: must be of the form: `#[marker]`
172172

173173
error: malformed `fundamental` attribute input
174-
--> $DIR/malformed-attrs.rs:154:1
174+
--> $DIR/malformed-attrs.rs:156:1
175175
|
176176
LL | #[fundamental()]
177177
| ^^^^^^^^^^^^^^^^ help: must be of the form: `#[fundamental]`
178178

179179
error: malformed `ffi_pure` attribute input
180-
--> $DIR/malformed-attrs.rs:162:5
180+
--> $DIR/malformed-attrs.rs:164:5
181181
|
182182
LL | #[unsafe(ffi_pure = 1)]
183183
| ^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[ffi_pure]`
184184

185185
error: malformed `link_ordinal` attribute input
186-
--> $DIR/malformed-attrs.rs:164:5
186+
--> $DIR/malformed-attrs.rs:166:5
187187
|
188188
LL | #[link_ordinal]
189189
| ^^^^^^^^^^^^^^^ help: must be of the form: `#[link_ordinal(ordinal)]`
190190

191191
error: malformed `ffi_const` attribute input
192-
--> $DIR/malformed-attrs.rs:168:5
192+
--> $DIR/malformed-attrs.rs:170:5
193193
|
194194
LL | #[unsafe(ffi_const = 1)]
195195
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[ffi_const]`
196196

197197
error: malformed `linkage` attribute input
198-
--> $DIR/malformed-attrs.rs:170:5
198+
--> $DIR/malformed-attrs.rs:172:5
199199
|
200200
LL | #[linkage]
201201
| ^^^^^^^^^^ help: must be of the form: `#[linkage = "external|internal|..."]`
202202

203203
error: malformed `allow` attribute input
204-
--> $DIR/malformed-attrs.rs:175:1
204+
--> $DIR/malformed-attrs.rs:177:1
205205
|
206206
LL | #[allow]
207207
| ^^^^^^^^ help: must be of the form: `#[allow(lint1, lint2, ..., /*opt*/ reason = "...")]`
208208

209209
error: malformed `expect` attribute input
210-
--> $DIR/malformed-attrs.rs:177:1
210+
--> $DIR/malformed-attrs.rs:179:1
211211
|
212212
LL | #[expect]
213213
| ^^^^^^^^^ help: must be of the form: `#[expect(lint1, lint2, ..., /*opt*/ reason = "...")]`
214214

215215
error: malformed `warn` attribute input
216-
--> $DIR/malformed-attrs.rs:179:1
216+
--> $DIR/malformed-attrs.rs:181:1
217217
|
218218
LL | #[warn]
219219
| ^^^^^^^ help: must be of the form: `#[warn(lint1, lint2, ..., /*opt*/ reason = "...")]`
220220

221221
error: malformed `deny` attribute input
222-
--> $DIR/malformed-attrs.rs:181:1
222+
--> $DIR/malformed-attrs.rs:183:1
223223
|
224224
LL | #[deny]
225225
| ^^^^^^^ help: must be of the form: `#[deny(lint1, lint2, ..., /*opt*/ reason = "...")]`
226226

227227
error: malformed `forbid` attribute input
228-
--> $DIR/malformed-attrs.rs:183:1
228+
--> $DIR/malformed-attrs.rs:185:1
229229
|
230230
LL | #[forbid]
231231
| ^^^^^^^^^ help: must be of the form: `#[forbid(lint1, lint2, ..., /*opt*/ reason = "...")]`
232232

233233
error: malformed `debugger_visualizer` attribute input
234-
--> $DIR/malformed-attrs.rs:185:1
234+
--> $DIR/malformed-attrs.rs:187:1
235235
|
236236
LL | #[debugger_visualizer]
237237
| ^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[debugger_visualizer(natvis_file = "...", gdb_script_file = "...")]`
238238

239239
error: malformed `automatically_derived` attribute input
240-
--> $DIR/malformed-attrs.rs:188:1
240+
--> $DIR/malformed-attrs.rs:190:1
241241
|
242242
LL | #[automatically_derived = 18]
243243
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[automatically_derived]`
244244

245245
error: malformed `non_exhaustive` attribute input
246-
--> $DIR/malformed-attrs.rs:194:1
246+
--> $DIR/malformed-attrs.rs:196:1
247247
|
248248
LL | #[non_exhaustive = 1]
249249
| ^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[non_exhaustive]`
250250

251251
error: malformed `thread_local` attribute input
252-
--> $DIR/malformed-attrs.rs:200:1
252+
--> $DIR/malformed-attrs.rs:202:1
253253
|
254254
LL | #[thread_local()]
255255
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#[thread_local]`
256256

257257
error: malformed `no_link` attribute input
258-
--> $DIR/malformed-attrs.rs:204:1
258+
--> $DIR/malformed-attrs.rs:206:1
259259
|
260260
LL | #[no_link()]
261261
| ^^^^^^^^^^^^ help: must be of the form: `#[no_link]`
262262

263263
error: malformed `macro_use` attribute input
264-
--> $DIR/malformed-attrs.rs:206:1
264+
--> $DIR/malformed-attrs.rs:208:1
265265
|
266266
LL | #[macro_use = 1]
267267
| ^^^^^^^^^^^^^^^^
@@ -276,7 +276,7 @@ LL + #[macro_use]
276276
|
277277

278278
error: malformed `macro_export` attribute input
279-
--> $DIR/malformed-attrs.rs:211:1
279+
--> $DIR/malformed-attrs.rs:213:1
280280
|
281281
LL | #[macro_export = 18]
282282
| ^^^^^^^^^^^^^^^^^^^^
@@ -291,7 +291,7 @@ LL + #[macro_export]
291291
|
292292

293293
error: malformed `allow_internal_unsafe` attribute input
294-
--> $DIR/malformed-attrs.rs:213:1
294+
--> $DIR/malformed-attrs.rs:215:1
295295
|
296296
LL | #[allow_internal_unsafe = 1]
297297
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[allow_internal_unsafe]`
@@ -309,13 +309,13 @@ LL | #[proc_macro_attribute = 19]
309309
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
310310

311311
error: the `#[proc_macro_derive]` attribute is only usable with crates of the `proc-macro` crate type
312-
--> $DIR/malformed-attrs.rs:120:1
312+
--> $DIR/malformed-attrs.rs:122:1
313313
|
314314
LL | #[proc_macro_derive]
315315
| ^^^^^^^^^^^^^^^^^^^^
316316

317317
error[E0658]: allow_internal_unsafe side-steps the unsafe_code lint
318-
--> $DIR/malformed-attrs.rs:213:1
318+
--> $DIR/malformed-attrs.rs:215:1
319319
|
320320
LL | #[allow_internal_unsafe = 1]
321321
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -361,7 +361,7 @@ LL | #[ignore()]
361361
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
362362

363363
error: invalid argument
364-
--> $DIR/malformed-attrs.rs:185:1
364+
--> $DIR/malformed-attrs.rs:187:1
365365
|
366366
LL | #[debugger_visualizer]
367367
| ^^^^^^^^^^^^^^^^^^^^^^
@@ -541,24 +541,41 @@ LL | #[link_name]
541541
| expected this to be of the form `link_name = "..."`
542542
| help: must be of the form: `#[link_name = "name"]`
543543

544+
error[E0539]: malformed `must_use` attribute input
545+
--> $DIR/malformed-attrs.rs:118:1
546+
|
547+
LL | #[must_use = 1]
548+
| ^^^^^^^^^^^^^-^
549+
| |
550+
| expected a string literal here
551+
|
552+
help: try changing it to one of the following valid forms of the attribute
553+
|
554+
LL - #[must_use = 1]
555+
LL + #[must_use = "reason"]
556+
|
557+
LL - #[must_use = 1]
558+
LL + #[must_use]
559+
|
560+
544561
warning: `#[diagnostic::do_not_recommend]` does not expect any arguments
545-
--> $DIR/malformed-attrs.rs:146:1
562+
--> $DIR/malformed-attrs.rs:148:1
546563
|
547564
LL | #[diagnostic::do_not_recommend()]
548565
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
549566
|
550567
= note: `#[warn(unknown_or_malformed_diagnostic_attributes)]` on by default
551568

552569
warning: missing options for `on_unimplemented` attribute
553-
--> $DIR/malformed-attrs.rs:135:1
570+
--> $DIR/malformed-attrs.rs:137:1
554571
|
555572
LL | #[diagnostic::on_unimplemented]
556573
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
557574
|
558575
= help: at least one of the `message`, `note` and `label` options are expected
559576

560577
warning: malformed `on_unimplemented` attribute
561-
--> $DIR/malformed-attrs.rs:137:1
578+
--> $DIR/malformed-attrs.rs:139:1
562579
|
563580
LL | #[diagnostic::on_unimplemented = 1]
564581
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid option found here
@@ -585,7 +602,7 @@ LL | #[coroutine = 63] || {}
585602
= note: expected unit type `()`
586603
found coroutine `{coroutine@$DIR/malformed-attrs.rs:110:23: 110:25}`
587604

588-
error: aborting due to 72 previous errors; 3 warnings emitted
605+
error: aborting due to 73 previous errors; 3 warnings emitted
589606

590607
Some errors have detailed explanations: E0308, E0463, E0539, E0565, E0658, E0805.
591608
For more information about an error, try `rustc --explain E0308`.

tests/ui/parser/bad-lit-suffixes.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ fn f() {}
3333

3434
#[must_use = "string"suffix]
3535
//~^ ERROR suffixes on string literals are invalid
36+
//~| ERROR malformed `must_use` attribute input
3637
fn g() {}
3738

3839
#[link(name = "string"suffix)]

tests/ui/parser/bad-lit-suffixes.stderr

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ LL | #[must_use = "string"suffix]
2323
| ^^^^^^^^^^^^^^ invalid suffix `suffix`
2424

2525
error: suffixes on string literals are invalid
26-
--> $DIR/bad-lit-suffixes.rs:38:15
26+
--> $DIR/bad-lit-suffixes.rs:39:15
2727
|
2828
LL | #[link(name = "string"suffix)]
2929
| ^^^^^^^^^^^^^^ invalid suffix `suffix`
3030

3131
error: invalid suffix `suffix` for number literal
32-
--> $DIR/bad-lit-suffixes.rs:42:41
32+
--> $DIR/bad-lit-suffixes.rs:43:41
3333
|
3434
LL | #[rustc_layout_scalar_valid_range_start(0suffix)]
3535
| ^^^^^^^ invalid suffix `suffix`
@@ -150,15 +150,23 @@ LL | 1.0e10suffix;
150150
|
151151
= help: valid suffixes are `f32` and `f64`
152152

153-
error[E0805]: malformed `rustc_layout_scalar_valid_range_start` attribute input
154-
--> $DIR/bad-lit-suffixes.rs:42:1
153+
error[E0539]: malformed `must_use` attribute input
154+
--> $DIR/bad-lit-suffixes.rs:34:1
155+
|
156+
LL | #[must_use = "string"suffix]
157+
| ^^^^^^^^^^^^^--------------^
158+
| |
159+
| expected a string literal here
160+
|
161+
help: try changing it to one of the following valid forms of the attribute
162+
|
163+
LL - #[must_use = "string"suffix]
164+
LL + #[must_use = "reason"]
165+
|
166+
LL - #[must_use = "string"suffix]
167+
LL + #[must_use]
155168
|
156-
LL | #[rustc_layout_scalar_valid_range_start(0suffix)]
157-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------^
158-
| | |
159-
| | expected a single argument here
160-
| help: must be of the form: `#[rustc_layout_scalar_valid_range_start(start)]`
161169

162170
error: aborting due to 21 previous errors; 2 warnings emitted
163171

164-
For more information about this error, try `rustc --explain E0805`.
172+
For more information about this error, try `rustc --explain E0539`.

0 commit comments

Comments
 (0)