-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Port #[must_use]
to new attribute parsing infrastructure
#142780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use rustc_attr_data_structures::AttributeKind; | ||
use rustc_errors::DiagArgValue; | ||
use rustc_feature::{AttributeTemplate, template}; | ||
use rustc_span::{Symbol, sym}; | ||
|
||
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser}; | ||
use crate::context::{AcceptContext, Stage}; | ||
use crate::parser::ArgParser; | ||
use crate::session_diagnostics; | ||
|
||
pub(crate) struct MustUseParser; | ||
|
||
impl<S: Stage> SingleAttributeParser<S> for MustUseParser { | ||
const PATH: &[Symbol] = &[sym::must_use]; | ||
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast; | ||
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError; | ||
const TEMPLATE: AttributeTemplate = template!(Word, NameValueStr: "reason"); | ||
|
||
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> { | ||
Some(AttributeKind::MustUse { | ||
span: cx.attr_span, | ||
reason: match args { | ||
ArgParser::NoArgs => None, | ||
ArgParser::NameValue(name_value) => name_value.value_as_str(), | ||
ArgParser::List(_) => { | ||
let suggestions = | ||
<Self as SingleAttributeParser<S>>::TEMPLATE.suggestions(false, "must_use"); | ||
cx.emit_err(session_diagnostics::MustUseIllFormedAttributeInput { | ||
num_suggestions: suggestions.len(), | ||
suggestions: DiagArgValue::StrListSepByAnd( | ||
suggestions.into_iter().map(|s| format!("`{s}`").into()).collect(), | ||
), | ||
span: cx.attr_span, | ||
}); | ||
return None; | ||
JonathanBrouwer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}, | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,6 +163,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> { | |
Attribute::Parsed(AttributeKind::AsPtr(attr_span)) => { | ||
self.check_applied_to_fn_or_method(hir_id, *attr_span, span, target) | ||
} | ||
Attribute::Parsed(AttributeKind::MustUse { span, .. }) => { | ||
self.check_must_use(hir_id, *span, target) | ||
} | ||
Attribute::Unparsed(_) => { | ||
match attr.path().as_slice() { | ||
[sym::diagnostic, sym::do_not_recommend, ..] => { | ||
|
@@ -235,7 +238,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> { | |
| [sym::const_trait, ..] => self.check_must_be_applied_to_trait(attr, span, target), | ||
[sym::collapse_debuginfo, ..] => self.check_collapse_debuginfo(attr, span, target), | ||
[sym::must_not_suspend, ..] => self.check_must_not_suspend(attr, span, target), | ||
[sym::must_use, ..] => self.check_must_use(hir_id, attr, target), | ||
[sym::may_dangle, ..] => self.check_may_dangle(hir_id, attr), | ||
[sym::rustc_pass_by_value, ..] => self.check_pass_by_value(attr, span, target), | ||
[sym::rustc_allow_incoherent_impl, ..] => { | ||
|
@@ -688,7 +690,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> { | |
AttributeKind::Deprecation { .. } | ||
| AttributeKind::Repr { .. } | ||
| AttributeKind::Align { .. } | ||
| AttributeKind::Cold(..), | ||
| AttributeKind::Cold(..) | ||
| AttributeKind::MustUse { .. }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've just rewritten the naked attribute, so this shouldn't be necessary anymore. but dw, I'll do the rebase on there |
||
) => { | ||
continue; | ||
} | ||
|
@@ -1563,7 +1566,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { | |
} | ||
|
||
/// Warns against some misuses of `#[must_use]` | ||
fn check_must_use(&self, hir_id: HirId, attr: &Attribute, target: Target) { | ||
fn check_must_use(&self, hir_id: HirId, attr_span: Span, target: Target) { | ||
if matches!( | ||
target, | ||
Target::Fn | ||
|
@@ -1603,7 +1606,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { | |
self.tcx.emit_node_span_lint( | ||
UNUSED_ATTRIBUTES, | ||
hir_id, | ||
attr.span(), | ||
attr_span, | ||
errors::MustUseNoEffect { article, target }, | ||
); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
#![no_std] | ||
|
||
//@ is "$.index[?(@.name=='example')].attrs" '["#[must_use]"]' | ||
//@ is "$.index[?(@.name=='example')].attrs" '["#[attr = MustUse]"]' | ||
#[must_use] | ||
pub fn example() -> impl Iterator<Item = i64> {} | ||
|
||
//@ is "$.index[?(@.name=='explicit_message')].attrs" '["#[must_use = \"does nothing if you do not use it\"]"]' | ||
//@ is "$.index[?(@.name=='explicit_message')].attrs" '["#[attr = MustUse {reason: \"does nothing if you do not use it\"}]"]' | ||
#[must_use = "does nothing if you do not use it"] | ||
pub fn explicit_message() -> impl Iterator<Item = i64> {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#[must_use()] //~ ERROR valid forms for the attribute are `#[must_use = "reason"]` and `#[must_use]` | ||
struct Test; | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
error: valid forms for the attribute are `#[must_use = "reason"]` and `#[must_use]` | ||
--> $DIR/malformed-must_use.rs:1:1 | ||
| | ||
LL | #[must_use()] | ||
| ^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 1 previous error | ||
|
Uh oh!
There was an error while loading. Please reload this page.