Skip to content

Commit 5384ba4

Browse files
committed
Add #[may_ignore] attribute to cancel #[must_use].
1 parent a601302 commit 5384ba4

File tree

6 files changed

+36
-5
lines changed

6 files changed

+36
-5
lines changed

compiler/rustc_feature/src/active.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,9 @@ declare_features! (
610610
/// Allows unsized fn parameters.
611611
(active, unsized_fn_params, "1.49.0", Some(48055), None),
612612

613+
/// Allow `#[may_ignore]` attribute.
614+
(active, may_ignore, "1.49.0", Some(99999999), None),
615+
613616
// -------------------------------------------------------------------------
614617
// feature-group-end: actual feature gates
615618
// -------------------------------------------------------------------------

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
206206
ungated!(forbid, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)),
207207
ungated!(deny, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)),
208208
ungated!(must_use, AssumedUsed, template!(Word, NameValueStr: "reason")),
209+
gated!(may_ignore, AssumedUsed, template!(Word), experimental!(may_ignore)),
209210
// FIXME(#14407)
210211
ungated!(
211212
deprecated, Normal,

compiler/rustc_lint/src/unused.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,6 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
101101
return;
102102
}
103103

104-
let ty = cx.typeck_results().expr_ty(&expr);
105-
let type_permits_lack_of_use = check_must_use_ty(cx, ty, &expr, s.span, "", "", 1);
106-
107-
let mut fn_warned = false;
108-
let mut op_warned = false;
109104
let maybe_def_id = match expr.kind {
110105
hir::ExprKind::Call(ref callee, _) => {
111106
match callee.kind {
@@ -123,6 +118,21 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
123118
hir::ExprKind::MethodCall(..) => cx.typeck_results().type_dependent_def_id(expr.hir_id),
124119
_ => None,
125120
};
121+
122+
if let Some(def_id) = maybe_def_id {
123+
for attr in cx.tcx.get_attrs(def_id).iter() {
124+
if cx.sess().check_name(attr, sym::may_ignore) {
125+
// Don't warn if the function was marked as #[may_ignore].
126+
return;
127+
}
128+
}
129+
}
130+
131+
let ty = cx.typeck_results().expr_ty(&expr);
132+
let type_permits_lack_of_use = check_must_use_ty(cx, ty, &expr, s.span, "", "", 1);
133+
134+
let mut fn_warned = false;
135+
let mut op_warned = false;
126136
if let Some(def_id) = maybe_def_id {
127137
fn_warned = check_must_use_def(cx, def_id, s.span, "return value of ", "");
128138
} else if type_permits_lack_of_use {

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ symbols! {
674674
maxnumf32,
675675
maxnumf64,
676676
may_dangle,
677+
may_ignore,
677678
maybe_uninit,
678679
maybe_uninit_uninit,
679680
maybe_uninit_zeroed,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[may_ignore]
2+
//~^ ERROR the `#[may_ignore]` attribute is an experimental feature [E0658]
3+
fn main() {
4+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0658]: the `#[may_ignore]` attribute is an experimental feature
2+
--> $DIR/feature-gate-may_ignore.rs:1:1
3+
|
4+
LL | #[may_ignore]
5+
| ^^^^^^^^^^^^^
6+
|
7+
= note: see issue #99999999 <https://github.com/rust-lang/rust/issues/99999999> for more information
8+
= help: add `#![feature(may_ignore)]` to the crate attributes to enable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)