Skip to main content

rustc_attr_parsing/attributes/
must_use.rs

1use rustc_feature::AttributeStability;
2
3use super::prelude::*;
4
5pub(crate) struct MustUseParser;
6
7impl SingleAttributeParser for MustUseParser {
8    const PATH: &[Symbol] = &[sym::must_use];
9    const ON_DUPLICATE: OnDuplicate = OnDuplicate::WarnButFutureError;
10    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowListWarnRest(&[
11        Allow(Target::Fn),
12        Allow(Target::Enum),
13        Allow(Target::Struct),
14        Allow(Target::Union),
15        Allow(Target::Method(MethodKind::Trait { body: false })),
16        Allow(Target::Method(MethodKind::Trait { body: true })),
17        Allow(Target::Method(MethodKind::Inherent)),
18        Allow(Target::ForeignFn),
19        // `impl Trait` in return position can trip
20        // `unused_must_use` if `Trait` is marked as
21        // `#[must_use]`
22        Allow(Target::Trait),
23        Error(Target::WherePredicate),
24    ]);
25    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: true,
    list: None,
    one_of: &[],
    name_value_str: Some(&["reason"]),
    docs: Some("https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute"),
}template!(
26        Word, NameValueStr: "reason",
27        "https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute"
28    );
29    const STABILITY: AttributeStability = AttributeStability::Stable;
30
31    fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
32        Some(AttributeKind::MustUse {
33            span: cx.attr_span,
34            reason: match args {
35                ArgParser::NoArgs => None,
36                ArgParser::NameValue(name_value) => cx.expect_string_literal(name_value),
37                ArgParser::List(list) => {
38                    cx.adcx().expected_nv_or_no_args(list.span);
39                    return None;
40                }
41            },
42        })
43    }
44}