Skip to content

Commit 928c776

Browse files
Fix #[must_use = 1] not giving an error
Signed-off-by: Jonathan Brouwer <[email protected]>
1 parent c87ace7 commit 928c776

File tree

5 files changed

+84
-37
lines changed

5 files changed

+84
-37
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
@@ -126,25 +126,25 @@ LL | #[proc_macro_attribute = 19]
126126
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[proc_macro_attribute]`
127127

128128
error: malformed `proc_macro_derive` attribute input
129-
--> $DIR/malformed-attrs.rs:120:1
129+
--> $DIR/malformed-attrs.rs:122:1
130130
|
131131
LL | #[proc_macro_derive]
132132
| ^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[proc_macro_derive(TraitName, /*opt*/ attributes(name1, name2, ...))]`
133133

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

140140
error: malformed `rustc_layout_scalar_valid_range_end` attribute input
141-
--> $DIR/malformed-attrs.rs:127:1
141+
--> $DIR/malformed-attrs.rs:129:1
142142
|
143143
LL | #[rustc_layout_scalar_valid_range_end]
144144
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[rustc_layout_scalar_valid_range_end(value)]`
145145

146146
error: malformed `must_not_suspend` attribute input
147-
--> $DIR/malformed-attrs.rs:129:1
147+
--> $DIR/malformed-attrs.rs:131:1
148148
|
149149
LL | #[must_not_suspend()]
150150
| ^^^^^^^^^^^^^^^^^^^^^
@@ -159,115 +159,115 @@ LL + #[must_not_suspend]
159159
|
160160

161161
error: malformed `cfi_encoding` attribute input
162-
--> $DIR/malformed-attrs.rs:131:1
162+
--> $DIR/malformed-attrs.rs:133:1
163163
|
164164
LL | #[cfi_encoding]
165165
| ^^^^^^^^^^^^^^^ help: must be of the form: `#[cfi_encoding = "encoding"]`
166166

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

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

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

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

191191
error: malformed `link_ordinal` attribute input
192-
--> $DIR/malformed-attrs.rs:164:5
192+
--> $DIR/malformed-attrs.rs:166:5
193193
|
194194
LL | #[link_ordinal]
195195
| ^^^^^^^^^^^^^^^ help: must be of the form: `#[link_ordinal(ordinal)]`
196196

197197
error: malformed `ffi_const` attribute input
198-
--> $DIR/malformed-attrs.rs:168:5
198+
--> $DIR/malformed-attrs.rs:170:5
199199
|
200200
LL | #[unsafe(ffi_const = 1)]
201201
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[ffi_const]`
202202

203203
error: malformed `linkage` attribute input
204-
--> $DIR/malformed-attrs.rs:170:5
204+
--> $DIR/malformed-attrs.rs:172:5
205205
|
206206
LL | #[linkage]
207207
| ^^^^^^^^^^ help: must be of the form: `#[linkage = "external|internal|..."]`
208208

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

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

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

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

233233
error: malformed `forbid` attribute input
234-
--> $DIR/malformed-attrs.rs:183:1
234+
--> $DIR/malformed-attrs.rs:185:1
235235
|
236236
LL | #[forbid]
237237
| ^^^^^^^^^ help: must be of the form: `#[forbid(lint1, lint2, ..., /*opt*/ reason = "...")]`
238238

239239
error: malformed `debugger_visualizer` attribute input
240-
--> $DIR/malformed-attrs.rs:185:1
240+
--> $DIR/malformed-attrs.rs:187:1
241241
|
242242
LL | #[debugger_visualizer]
243243
| ^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[debugger_visualizer(natvis_file = "...", gdb_script_file = "...")]`
244244

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

251251
error: malformed `non_exhaustive` attribute input
252-
--> $DIR/malformed-attrs.rs:194:1
252+
--> $DIR/malformed-attrs.rs:196:1
253253
|
254254
LL | #[non_exhaustive = 1]
255255
| ^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[non_exhaustive]`
256256

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

263263
error: malformed `no_link` attribute input
264-
--> $DIR/malformed-attrs.rs:204:1
264+
--> $DIR/malformed-attrs.rs:206:1
265265
|
266266
LL | #[no_link()]
267267
| ^^^^^^^^^^^^ help: must be of the form: `#[no_link]`
268268

269269
error: malformed `macro_use` attribute input
270-
--> $DIR/malformed-attrs.rs:206:1
270+
--> $DIR/malformed-attrs.rs:208:1
271271
|
272272
LL | #[macro_use = 1]
273273
| ^^^^^^^^^^^^^^^^
@@ -282,7 +282,7 @@ LL + #[macro_use]
282282
|
283283

284284
error: malformed `macro_export` attribute input
285-
--> $DIR/malformed-attrs.rs:211:1
285+
--> $DIR/malformed-attrs.rs:213:1
286286
|
287287
LL | #[macro_export = 18]
288288
| ^^^^^^^^^^^^^^^^^^^^
@@ -297,7 +297,7 @@ LL + #[macro_export]
297297
|
298298

299299
error: malformed `allow_internal_unsafe` attribute input
300-
--> $DIR/malformed-attrs.rs:213:1
300+
--> $DIR/malformed-attrs.rs:215:1
301301
|
302302
LL | #[allow_internal_unsafe = 1]
303303
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[allow_internal_unsafe]`
@@ -315,13 +315,13 @@ LL | #[proc_macro_attribute = 19]
315315
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
316316

317317
error: the `#[proc_macro_derive]` attribute is only usable with crates of the `proc-macro` crate type
318-
--> $DIR/malformed-attrs.rs:120:1
318+
--> $DIR/malformed-attrs.rs:122:1
319319
|
320320
LL | #[proc_macro_derive]
321321
| ^^^^^^^^^^^^^^^^^^^^
322322

323323
error[E0658]: allow_internal_unsafe side-steps the unsafe_code lint
324-
--> $DIR/malformed-attrs.rs:213:1
324+
--> $DIR/malformed-attrs.rs:215:1
325325
|
326326
LL | #[allow_internal_unsafe = 1]
327327
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -367,7 +367,7 @@ LL | #[ignore()]
367367
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
368368

369369
error: invalid argument
370-
--> $DIR/malformed-attrs.rs:185:1
370+
--> $DIR/malformed-attrs.rs:187:1
371371
|
372372
LL | #[debugger_visualizer]
373373
| ^^^^^^^^^^^^^^^^^^^^^^
@@ -538,24 +538,41 @@ LL - #[used()]
538538
LL + #[used]
539539
|
540540

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

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

557574
warning: malformed `on_unimplemented` attribute
558-
--> $DIR/malformed-attrs.rs:137:1
575+
--> $DIR/malformed-attrs.rs:139:1
559576
|
560577
LL | #[diagnostic::on_unimplemented = 1]
561578
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid option found here
@@ -582,7 +599,7 @@ LL | #[coroutine = 63] || {}
582599
= note: expected unit type `()`
583600
found coroutine `{coroutine@$DIR/malformed-attrs.rs:110:23: 110:25}`
584601

585-
error: aborting due to 72 previous errors; 3 warnings emitted
602+
error: aborting due to 73 previous errors; 3 warnings emitted
586603

587604
Some errors have detailed explanations: E0308, E0463, E0539, E0565, E0658, E0805.
588605
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: 21 additions & 3 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,5 +150,23 @@ LL | 1.0e10suffix;
150150
|
151151
= help: valid suffixes are `f32` and `f64`
152152

153-
error: aborting due to 20 previous errors; 2 warnings emitted
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]
168+
|
169+
170+
error: aborting due to 21 previous errors; 2 warnings emitted
154171

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

0 commit comments

Comments
 (0)