Skip to content

Commit c1c204d

Browse files
Port must_use to the new target checking
1 parent f0addd0 commit c1c204d

File tree

4 files changed

+20
-53
lines changed

4 files changed

+20
-53
lines changed

compiler/rustc_attr_parsing/src/attributes/must_use.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use rustc_errors::DiagArgValue;
22
use rustc_feature::{AttributeTemplate, template};
33
use rustc_hir::attrs::AttributeKind;
4+
use rustc_hir::{MethodKind, Target};
45
use rustc_span::{Symbol, sym};
56

67
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
7-
use crate::context::{ALL_TARGETS, AcceptContext, AllowedTargets, Stage};
8+
use crate::context::MaybeWarn::{Allow, Error};
9+
use crate::context::{AcceptContext, AllowedTargets, Stage};
810
use crate::parser::ArgParser;
911
use crate::session_diagnostics;
1012
pub(crate) struct MustUseParser;
@@ -13,7 +15,21 @@ impl<S: Stage> SingleAttributeParser<S> for MustUseParser {
1315
const PATH: &[Symbol] = &[sym::must_use];
1416
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
1517
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
16-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS); //FIXME Still checked fully in `check_attr.rs`
18+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowListWarnRest(&[
19+
Allow(Target::Fn),
20+
Allow(Target::Enum),
21+
Allow(Target::Struct),
22+
Allow(Target::Union),
23+
Allow(Target::Method(MethodKind::Trait { body: false })),
24+
Allow(Target::Method(MethodKind::Trait { body: true })),
25+
Allow(Target::Method(MethodKind::Inherent)),
26+
Allow(Target::ForeignFn),
27+
// `impl Trait` in return position can trip
28+
// `unused_must_use` if `Trait` is marked as
29+
// `#[must_use]`
30+
Allow(Target::Trait),
31+
Error(Target::WherePredicate),
32+
]);
1733
const TEMPLATE: AttributeTemplate = template!(
1834
Word, NameValueStr: "reason",
1935
"https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute"

compiler/rustc_passes/messages.ftl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,6 @@ passes_must_not_suspend =
432432
`must_not_suspend` attribute should be applied to a struct, enum, union, or trait
433433
.label = is not a struct, enum, union, or trait
434434
435-
passes_must_use_no_effect =
436-
`#[must_use]` has no effect when applied to {$target}
437-
.suggestion = remove the attribute
438-
439435
passes_no_link =
440436
attribute should be applied to an `extern crate` item
441437
.label = not an `extern crate` item

compiler/rustc_passes/src/check_attr.rs

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
194194
Attribute::Parsed(AttributeKind::MayDangle(attr_span)) => {
195195
self.check_may_dangle(hir_id, *attr_span)
196196
}
197-
Attribute::Parsed(AttributeKind::MustUse { span, .. }) => {
198-
self.check_must_use(hir_id, *span, target)
199-
}
200197
&Attribute::Parsed(AttributeKind::CustomMir(dialect, phase, attr_span)) => {
201198
self.check_custom_mir(dialect, phase, attr_span)
202199
}
@@ -249,7 +246,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
249246
| AttributeKind::Coverage (..)
250247
| AttributeKind::ShouldPanic { .. }
251248
| AttributeKind::Coroutine(..)
252-
| AttributeKind::Linkage(..),
249+
| AttributeKind::Linkage(..)
250+
| AttributeKind::MustUse { .. },
253251
) => { /* do nothing */ }
254252
Attribute::Unparsed(attr_item) => {
255253
style = Some(attr_item.style);
@@ -1260,41 +1258,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
12601258
}
12611259
}
12621260

1263-
/// Warns against some misuses of `#[must_use]`
1264-
fn check_must_use(&self, hir_id: HirId, attr_span: Span, target: Target) {
1265-
if matches!(
1266-
target,
1267-
Target::Fn
1268-
| Target::Enum
1269-
| Target::Struct
1270-
| Target::Union
1271-
| Target::Method(MethodKind::Trait { body: false } | MethodKind::Inherent)
1272-
| Target::ForeignFn
1273-
// `impl Trait` in return position can trip
1274-
// `unused_must_use` if `Trait` is marked as
1275-
// `#[must_use]`
1276-
| Target::Trait
1277-
) {
1278-
return;
1279-
}
1280-
1281-
// `#[must_use]` can be applied to a trait method definition with a default body
1282-
if let Target::Method(MethodKind::Trait { body: true }) = target
1283-
&& let parent_def_id = self.tcx.hir_get_parent_item(hir_id).def_id
1284-
&& let containing_item = self.tcx.hir_expect_item(parent_def_id)
1285-
&& let hir::ItemKind::Trait(..) = containing_item.kind
1286-
{
1287-
return;
1288-
}
1289-
1290-
self.tcx.emit_node_span_lint(
1291-
UNUSED_ATTRIBUTES,
1292-
hir_id,
1293-
attr_span,
1294-
errors::MustUseNoEffect { target: target.plural_name(), attr_span },
1295-
);
1296-
}
1297-
12981261
/// Checks if `#[must_not_suspend]` is applied to a struct, enum, union, or trait.
12991262
fn check_must_not_suspend(&self, attr: &Attribute, span: Span, target: Target) {
13001263
match target {

compiler/rustc_passes/src/errors.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -358,14 +358,6 @@ pub(crate) struct BothFfiConstAndPure {
358358
pub attr_span: Span,
359359
}
360360

361-
#[derive(LintDiagnostic)]
362-
#[diag(passes_must_use_no_effect)]
363-
pub(crate) struct MustUseNoEffect {
364-
pub target: &'static str,
365-
#[suggestion(code = "", applicability = "machine-applicable", style = "tool-only")]
366-
pub attr_span: Span,
367-
}
368-
369361
#[derive(Diagnostic)]
370362
#[diag(passes_must_not_suspend)]
371363
pub(crate) struct MustNotSuspend {

0 commit comments

Comments
 (0)