Skip to content

Commit 83701e0

Browse files
Port must_use to the new target checking
1 parent 4de539b commit 83701e0

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
@@ -422,10 +422,6 @@ passes_must_not_suspend =
422422
`must_not_suspend` attribute should be applied to a struct, enum, union, or trait
423423
.label = is not a struct, enum, union, or trait
424424
425-
passes_must_use_no_effect =
426-
`#[must_use]` has no effect when applied to {$target}
427-
.suggestion = remove the attribute
428-
429425
passes_no_link =
430426
attribute should be applied to an `extern crate` item
431427
.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(
201198
AttributeKind::BodyStability { .. }
202199
| AttributeKind::ConstStabilityIndirect
@@ -246,7 +243,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
246243
| AttributeKind::Coverage (..)
247244
| AttributeKind::ShouldPanic { .. }
248245
| AttributeKind::Coroutine(..)
249-
| AttributeKind::Linkage(..),
246+
| AttributeKind::Linkage(..)
247+
| AttributeKind::MustUse { .. },
250248
) => { /* do nothing */ }
251249

252250
Attribute::Unparsed(attr_item) => {
@@ -1259,41 +1257,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
12591257
}
12601258
}
12611259

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

compiler/rustc_passes/src/errors.rs

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

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

0 commit comments

Comments
 (0)